There are several ways to terminate threads. You can use pthread_cancel() to raise a request to them and they would be canceled at some cancellation points, or use pthread_kill() to send a signal to them asynchronously. However, I think they are both not practical.
By using pthread_cancel(), we can be sure that the thread would be canceled at some specific POSIX functions. But what if there are other third-party libraries used and there is no way to make sure what POSIX functions are used by them?
By using pthread_kill(), it is even worse. We might affect the original read()/write() behaviors when receiving SIGINT. Even we construct safe read()/write() and other functions safe for handling signals, we still have no idea what to cleanup in signal handlers. Signal handling is complex in process, not to mention how to do it well in thread environment which is much younger than the signal mechanism.
So how to terminate a thread with minimum effort?
It seems only one way: atomically set a flag to raise the request and let the threads to detect it atomically, then the thread exits by itself.
One might wonder what if the thread holds some resources, like mutex, and not release it well? And we can not control the time threads would take. Hey, these issues remain when we use other ways, right? Since threads are created by us, they are our responsibilities to make sure they behave well. :-)
By using pthread_cancel(), we can be sure that the thread would be canceled at some specific POSIX functions. But what if there are other third-party libraries used and there is no way to make sure what POSIX functions are used by them?
By using pthread_kill(), it is even worse. We might affect the original read()/write() behaviors when receiving SIGINT. Even we construct safe read()/write() and other functions safe for handling signals, we still have no idea what to cleanup in signal handlers. Signal handling is complex in process, not to mention how to do it well in thread environment which is much younger than the signal mechanism.
So how to terminate a thread with minimum effort?
It seems only one way: atomically set a flag to raise the request and let the threads to detect it atomically, then the thread exits by itself.
One might wonder what if the thread holds some resources, like mutex, and not release it well? And we can not control the time threads would take. Hey, these issues remain when we use other ways, right? Since threads are created by us, they are our responsibilities to make sure they behave well. :-)
Hi Mars~
回覆刪除I am James ^^
I met this issue before.
I wanted to terminate a thread but failed when it works in while loop.
(even it has a cancellation point (i.e. msgsnd()) inside the loop.)
By the book (i.e. The Linux Programming Interface) you recommended before,
chapter 32.4 shows a api "pthread_testcancel(void)" to be a cancellation point.
And my function works well so far when I send a pthread_cancel() from other side.
So, is "pthread_testcancel(void)" one kind of "flag" you said?
Or there are others good/convenient way to be a flag?
Thanks ^^
Regards.
I mean using a flag variable(this should be atomic int or something like that) to indicate the request raised by main thread. Nerver use pthread_cancel() nor pthread_kill() if you are not sure.
回覆刪除