Subj : atexit handler: pthread_cancel, pthread_testcancel, pthread_join problem To : comp.programming.threads From : annamalai.gurusami Date : Thu Aug 18 2005 03:38 am Hi All, I am having a problem related to pthread_join. The following short program demonstrates the problem. There are two threads of execution involved. The main program (A) and another thread (B). The thread B runs continuously (in forever loop) doing its work. Every time in the loop, it once call pthread_testcancel to check if it has to continue or not. This is its only cancellation point. The thread A, calls pthread_cancel(B) in its atexit handler. This is followed by a call to pthread_join(B, 0) (in atexit handler). This seems to create problems. Any thoughts on this much appreciated. #include #include #include pthread_t th; void* start_thread(void *) { for(;;) { std::cout << "testcancel()" << std::endl; pthread_testcancel(); } } void atexit_handler(void) { pthread_cancel(th); pthread_join(th, 0); } int main() { atexit( atexit_handler ); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&th, &attr, start_thread, 0 ); exit(0); } Some information about my system. $ uname -a Linux redhatas.mct.com 2.6.9-5.ELsmp #1 SMP Wed Jan 5 19:29:47 EST 2005 x86_64 x86_64 x86_64 GNU/Linux $ cat /etc/redhat-release Red Hat Enterprise Linux AS release 4 (Nahant) I have mailed this problem to nahant-list@redhat.com, but no response yet (more than 24 hours is not good tech. support) BTW, I tried this with both -m32 and -m64 options of g++. In 32-bit version, it is hanging, and in 64-bit version it is dumping core. The core dump looks like this. [anna@redhatas] $ gdb pj64 core.29275 GNU gdb Red Hat Linux (6.1post-1.20040607.62rh) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu"...Using host libthread_db library "/lib64/tls/libthread_db.so.1". Core was generated by `./pj64'. Program terminated with signal 6, Aborted. Reading symbols from /usr/lib64/libstdc++.so.6...done. Loaded symbols for /usr/lib64/libstdc++.so.6 Reading symbols from /lib64/tls/libm.so.6...done. Loaded symbols for /lib64/tls/libm.so.6 Reading symbols from /lib64/libgcc_s.so.1...done. Loaded symbols for /lib64/libgcc_s.so.1 Reading symbols from /lib64/tls/libpthread.so.0...done. Loaded symbols for /lib64/tls/libpthread.so.0 Reading symbols from /lib64/tls/libc.so.6...done. Loaded symbols for /lib64/tls/libc.so.6 Reading symbols from /lib64/ld-linux-x86-64.so.2...done. Loaded symbols for /lib64/ld-linux-x86-64.so.2 #0 0x00000036c712e4dd in raise () from /lib64/tls/libc.so.6 (gdb) where #0 0x00000036c712e4dd in raise () from /lib64/tls/libc.so.6 #1 0x00000036c712fc8e in abort () from /lib64/tls/libc.so.6 #2 0x00000036c7c0ac21 in unwind_cleanup () from /lib64/tls/libpthread.so.0 #3 0x00000036c9286ec1 in std::ostream::put () from /usr/lib64/libstdc++.so.6 #4 0x00000036c9286f60 in std::endl > () from /usr/lib64/libstdc++.so.6 #5 0x0000000000400cda in start_thread () at pj.cc:14 #6 0x00000036c7c0613a in start_thread () from /lib64/tls/libpthread.so.0 #7 0x00000036c71c52b3 in clone () from /lib64/tls/libc.so.6 #8 0x0000000000000000 in ?? () (gdb) Rgds, anna .