using pthread_exit and pthread_join. pthread_exit doesn't terminate the calling function
gcc 4.6.0 c89
I am just experimenting with using pthread_exit and pthread_join.
The only thing I notice with the pthread_exit it that it didn't display the print message before main returned. However, pthread_join did exactly that.
I would have thought the print statement should have been displayed. If not does that mean that main has not terminated correctly in using the pthread_exit?
Many thanks for any suggestions,
My source code snippet source.c file:
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;
}
Source code snippet main.c file:
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) {
开发者_如何学Gofprintf(stderr, "Failed to join thread [ %s ]", strerror(errno));
return -1;
}
printf("Program Terminated\n");
return 0;
}
What you're seeing is expected. pthread_exit
never returns. It stops the thread that calls it immediately (and then runs the clean-up handlers if any, and then potentially thread-specific data destructors).
Nothing in main
after pthread_exit
will ever run.
精彩评论