【发布时间】:2015-11-11 07:25:17
【问题描述】:
我正在编写一个多线程程序。程序结构如下所示
int main()
{
printf("To Call threads\n");
thread1=pthread_create(&trd1,NULL,process1,(void *)sleepTimeForP1);
thread2=pthread_create(&trd2,NULL,process2,(void *)sleepTimeForP2);
pthread_join(trd1, NULL);
pthread_join(trd2, NULL);
return 0;
}
线程的代码是
void *process1 (void *sleepTimeForP1)
{
int *tsleepTimeForP1 = (int *)sleepTimeForP1;
while(1)
{
pthread_mutex_lock(&lock);
printf("inside thread\n");
pthread_mutex_unlock(&lock);
sleep(1);
}
}
代码工作正常,并在未完成输出重定向时打印输出。但是当给出输出重定向时,文件中没有输出。现在,如果我在每个printf() 语句之后给出明确的fflush(stdout),我将得到所需的输出。为什么我需要给出明确的 fflush() ?
【问题讨论】:
标签: c linux multithreading unix io-redirection