简体   繁体   中英

thread cancellation in posix threads

i am using posix threads my question is as to whether or not a thread can cancel itself by passing its own thread id in pthread_cancel function? if yes then what are its implications

also if a main program creates two threads and one of the thread cancels the other thread then what happens to the return value and the resources of the cancelled thread and how to know from main program as to which thread was cancelled ..since main program is not cancelling any of the threads

i am using asynchronous cancellation

kindly help

Q1: Yes, a thread can cancel itself. However, doing so has all of the negative consequences of cancellation in general; you probably want to use pthread_exit instead, which is somewhat more predictable.

Q2: When a thread has been cancelled, it doesn't get to generate a return value; instead, pthread_join will put the special value PTHREAD_CANCELED in the location pointed to by its retval argument. Unfortunately, you have to know by some other means that a specific thread has definitely terminated (in some fashion) before you call pthread_join , or the calling thread will block forever. There is no portable equivalent of waitpid(..., WNOHANG) nor of waitpid(-1, ...) . (The manpage says "If you believe you need this functionality, you probably need to rethink your application design" which makes me want to punch someone in the face.)

Q2a: It depends what you mean by "resources of the thread". The thread control block and stack will be deallocated. All destructors registered with pthread_cleanup_push or pthread_key_create will be executed (on the thread, before it terminates); some runtimes also execute C++ class destructors for objects on the stack. It is the application programmer's responsibility to make sure that all resources owned by the thread are covered by one of these mechanisms. Note that some of these mechanisms have inherent race conditions; for instance, it is impossible to open a file and push a cleanup that closes it as an atomic action, so there is a window where cancellation can leak the open file. (Do not think this can be worked around by pushing the cleanup before opening the file, because a common implementation of deferred cancels is to check for them whenever a system call returns , ie exactly timed to hit the tiny gap between the OS writing the file descriptor number to the return-value register, and the calling function copying that register to the memory location where the cleanup expects it to be.)

Qi: you didn't ask this, but you should be aware that a thread with asynchronous cancellation enabled is officially not allowed to do anything other than pure computation. The behavior is undefined if it calls any library function other than pthread_cancel , pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED) , or pthread_setcancelstate(PTHREAD_CANCEL_DISABLE) .

Q1. Yes,thread can cancel itself.

Q2. If one thread cancel another thread , its resources are hang around until main thread join that thread with pthread_join() function(if the thread is joinable). And if the canceled thread is not join in main thread resources are free with program ends/terminate.

Q3. I am not sure, but main program don't know which thread was canceled.

thread can cancel any other thread (within the same process) including itself

threads do not have return values (in general way, they can have return status only), resources of the thread will be freed upon cancellation

main program can store thread's handler and test whether it valid or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM