gcc 4.6.0 c89
我正在尝试使用pthread_exit和pthread_join。
我唯一注意到pthread_exit它在main返回之前没有显示打印消息。但是,pthread_join正是如此。
我原本以为应该显示print语句。如果不是这意味着main在使用pthread_exit时没有正确终止?
非常感谢任何建议,
我的源代码片段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;
}
源代码片段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;
}
答案 0 :(得分:1)
您所看到的是预期的。 pthread_exit
从不返回。它会立即停止调用它的线程(然后运行清理处理程序,如果有的话,然后运行特定于线程的数据析构函数)。
main
之后pthread_exit
中的任何内容都无效。