// // bug.c // // Author: Tom R. Dial // // This simple program demonstrates what I believe to be a problem // with the 2.6.24-rc7 kernel. thread_func() starts by calling // pthread_detach(), which according to POSIX (as I read it) is // *supposed* to ensure that thread resources are cleaned up when // the thread terminates. This seemed to work in 2.6.23, but when // I applied the patch for rc7, I noticed that main() seems to // terminate immediately. The sample program sleeps and prints // a message from the spawned thread. The call to pthread_join() // in main should ensure that this message gets printed. If the // call to pthread_detach() is commented out, the program functions // as expected and prints the "got here" message. If the call // to pthread_detach() is made, the program exits immediately // without printing the error message. // // gcc -Wall -lpthread -o bug bug.c #include #include #include #include void* thread_func(void* param) { int ptd = 0; ptd = pthread_detach( pthread_self() ); //printf("pthread_detach() returned %d, errno=%d\n", ptd, errno ); sleep( 10 ); printf("got here\n"); return 0; } int main(int argc, char** argv) { pthread_t thread = 0; int status = 0; status = pthread_create( &thread, 0, thread_func, 0 ); if (status < 0) { printf( "pthread_create() returned %d, errno=%d\n", status, errno ); return 0; } pthread_join( thread, 0 ); return 0; }