日期:2014-05-16  浏览次数:20617 次

简单的程序,线程不启动
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()函数啊!