【发布时间】:2011-04-24 08:58:12
【问题描述】:
gcc 4.6.0 c89
我只是在尝试使用 pthread_exit 和 pthread_join。
我注意到 pthread_exit 的唯一一件事是它在 main 返回之前没有显示打印消息。然而,pthread_join 正是这样做的。
我原以为应该显示打印语句。如果不是,这是否意味着 main 在使用 pthread_exit 时没有正确终止?
非常感谢您的任何建议,
我的源代码sn -p source.c文件:
void* process_events(void)
{
app_running = TRUE;
int counter = 0;
while(app_running) {
#define TIMEOUT 3000000
printf("Sleeping.....\n");
usleep(TIMEOUT);
if(counter++ == 2) {
app_running = FALSE;
}
}
printf("Finished process events\n");
return NULL;
}
源码sn -p main.c文件:
int main(void)
{
pthread_t th_id = 0;
int th_rc = 0;
th_rc = pthread_create(&th_id, NULL, (void*)process_events, NULL);
if(th_rc == -1) {
fprintf(stderr, "Cannot create thread [ %s ]\n", strerror(errno));
return -1;
}
/*
* Test with pthread_exit and pthread_join
*/
/* pthread_exit(NULL); */
if(pthread_join(th_id, NULL) == -1) {
fprintf(stderr, "Failed to join thread [ %s ]", strerror(errno));
return -1;
}
printf("Program Terminated\n");
return 0;
}
【问题讨论】: