1
GCC 4.6.0的C89使用pthread_exit和pthread_join。了pthread_exit不會終止調用函數
我只是使用了pthread_exit和pthread_join函數試驗。
我只注意到pthread_exit它在main返回之前沒有顯示打印消息。但是,pthread_join正是如此。
我會認爲應該顯示打印語句。如果不是這意味着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;
}
Hello Mat,謝謝你的回答。我猜如果你想運行任何任務,他們應該在調用pthread_exit之前完成。 – ant2009 2011-04-24 09:45:38
'pthread_exit'不會運行退出處理程序,除非它導致整個進程退出(由於終止最後一個線程)。它確實運行取消清除處理程序和線程特定數據析構函數。 – 2011-04-24 19:53:31