简单的程序,线程不启动
- C/C++ code
#include "errno.h" #include "stdio.h" #include "stdlib.h" #include <pthread.h>#include "unistd.h" pthread_mutex_t hMutex = PTHREAD_MUTEX_INITIALIZER; void *threadFunc1(void *arg) { while(1) { sleep(10); (void)printf("aaa"); pthread_yield(); } return 0; } void *threadFunc2(void *arg) { while(1) { sleep(5); (void)printf("bbb"); pthread_yield(); } return 0; } int main(void) { pthread_t tid1, tid2; pthread_create(&tid1, NULL, threadFunc1, NULL); pthread_create(&tid2, NULL, threadFunc2, NULL); fflush(NULL); while(1) { sleep(1000); fflush(NULL); } return 0; }
这个小程序 没有看到aaa 跟bbb输出 什么问题呢?
而起在threadFunc1跟threadFunc2里面加上fflush(NULL);也不好使 在主线程里面加上pthread_yield();
也还是不好使 不理解哪里出问题了???
[解决办法]
嗯,windows 是 Sleep
[解决办法]
嗯!是的!我是说你为什么老是要睡这么久呢?呵呵!
[解决办法]
对了,你可以用usleep()函数啊!