杀死一个等待条件变量的pthread [英] Killing a pthread waiting on a condition variable
问题描述
我有一个pthread等待条件变量使用pthread_cond_wait。它正在等待由另一个线程填充的队列结构中的数据。我想杀死这个线程,最好没有pthread_kill。在Linux和WinPthreads做一个pthread_cancel(); pthread_join()就足以杀死它了。然而,在OS X它挂起在pthread_join调用。任何建议?
I have a pthread waiting on a condition variable using pthread_cond_wait. It's waiting for data from a queue structure that is filled by another thread. I want to kill this thread, preferably without pthread_kill. On Linux and WinPthreads doing a pthread_cancel(); pthread_join() is enough to kill it. However, on OS X it hangs on the pthread_join call. Any suggestions?
推荐答案
您是否可以访问队列以及控制已排队项目的对象模式?如果是这样,请定义一个队列对象类型,在退出队列时指示正在处理该项的线程正常退出。
Do you have access to the queue, and control of the object schema for enqueued items? If so, define a queue object type that when de-queued, instructs the thread that is processing the item to exit gracefully.
现在,关闭这些线程将多个这些退出对象发送到与队列服务的线程数相对应的队列的 HEAD ,并在线程上加入。
Now, to shut down these threads, simply post a number of these "quit" objects to the HEAD of the queue that corresponds to the number of threads that are servicing the queue, and join on the threads.
这看起来比pthread_cancel / kill的nuclear选项更清洁。
This seems much cleaner than the "nuclear option" of pthread_cancel/kill.
这篇关于杀死一个等待条件变量的pthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!