i am making a small project which will be incorporated into larger project. basically what it does is keeps track of threads that are created by way of adding them to a main struct which keeps track of what the thread does (its main function) and its pthread_t id. the other struct keeps track of the data to be passed to the function and the element number of where the pthread_t id is stored inside threads[]. its a bit micky mouse and it jumps around a bit but it all works besides when it is time to kill the thread. i get no segfaults and no errors and the program finishes fine, but the thread does not get killed when pthread_kill() is called (the function returns 0 meaning no error and it worked) although the thread continues to run until the main application returns.
i am making a small project which will be incorporated into larger project. basically
Share
pthread_kill()will not kill a thread. The only difference withkill()is that the signal is handled by the designated thread and not handled while that thread has the signal masked (seepthread_sigmask()). A signal likeSIGTERMwill by default still terminate the entire process.If you are considering to call
pthread_exit()from a signal handler, you should probably usepthread_cancel()instead.Cancellation is safe if all code that may be cancelled cooperates (or the code that calls it disables cancellation for the time). Most libraries do not care about this, though.
A safer method is to ask the thread to exit without any force, such as by sending a special message to it (if the thread normally processes messages).
Alternatively, don’t bother to kill any threads and just call
_exit(),_Exit()orquick_exit().