What I want to accomplish is that a main thread tries a normal deferred cancel first on a worker thread (executing code that is for my purposes a black box), and then if the thread is still running after a timeout (pthread_timedjoin_np()), I want to do an asynchronous cancel. The problem I’m having is that pthread_setcanceltype() is only for the calling thread. Is there some workaround or hack that will let me do this? I want to avoid using signals as at least under Linux it seems that an asynchronous cancel will still execute C++ destructors of the thread’s objects, which is important for me.
What I want to accomplish is that a main thread tries a normal deferred
Share
There are some cases, when
pthread_setcanceltype()must actually do a cancel (see source below). So, this is a reason, why there is nopthread_setcanceltype_for_thread(). The actual cancel type is the field in pthread struct, which must be changed atomically.ftp://sources.redhat.com/pub/glibc/snapshots/glibc-latest.tar.bz2/glibc-20090518/nptl/pthread_setcanceltype.c
If you have a big need to change canceltype externally, you can hack the library and set the field directly.
PS: for NPTL (current implementation of pthreads in glibc on Linux)
the easiest way to see how to get
struct pthreadfrom int pthread_t is … pthread_join: