如何中断正在等待的 C++0x 线程?
我正在考虑在我的应用程序中使用 C++0x 线程而不是 Boost 线程。但是,我不确定如何重新实现标准 C++0x 线程的功能,因为它们似乎没有 interrupt()
方法。
我当前的设置是:
- 管理工作的主线程;
- 几个执行master命令的工作线程。
Workers 对至少两个不同的条件变量调用 wait()
。 Master 有一个“超时”状态:在这种情况下,它告诉所有工人停止并给出他们当时得到的任何结果。使用 Boost 线程,master 只需在线程组上使用 interrupt_all()
,这会导致工作线程停止等待。如果他们现在没有等待,master 还会设置一个 bool 标志,worker 会定期检查该标志。
但是,在 C++0x std::thread
中,我没有看到任何 interrupt()
的替代品。我错过了什么吗?如果不是,我该如何实施上述方案,让工人不至于永远睡觉呢?
I'm considering to use C++0x threads in my application instead of Boost threads. However, I'm not sure how to reimplement what I have with standard C++0x threads since they don't seem to have an interrupt()
method.
My current setup is:
- a master thread that manages work;
- several worker threads that carry out master's commands.
Workers call wait()
on at least two different condition variables. Master has a "timed out" state: in this case it tells all workers to stop and give whatever result they got by then. With Boost threads master just uses interrupt_all()
on a thread group, which causes workers to stop waiting. In case they are not waiting at the moment, master also sets a bool
flag which workers check periodically.
However, in C++0x std::thread
I don't see any replacement for interrupt()
. Do I miss something? If not, how can I implement the above scheme so that workers cannot just sleep forever?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,除了轮询之外,我没有看到其他方法,而不是使用等待,而是使用定时等待和一个变量来声明中断已完成。
Interruptor 类将维护一个布尔变量,该变量受互斥锁保护,或者使用原子操作(如果有)和两个函数 Interrupt 和 check_interruption_point,如果布尔值为 true,则抛出一个 Interrupted_Exception。 Mater 线程将创建一个 Interruptor 变量,该变量将在创建时提供给相关线程。主控器有可能立即中断依赖于该中断器的所有线程。如果您想一次显式中断一个线程,您当然可以为每个线程创建一个中断程序。
由您定义定时等待的持续时间,以便您的线程能够在程序需要时立即做出反应。
Unfortunately I don't see another way than polling, instead of using wait use a timed wait and a variable to state the interruption has been done.
The Interruptor class will maintain a boolean variable protected with a mutex or using atomic operations if you have them and two functions interrupt and check_interruption_point, which with throw a interrupted_exception if the boolean is true. The Mater thread will create an Interruptor variable that will be given to the concerned threads at creation time. The master has the the possibility to interrupt at once all the threads that depends on this interruptor. You can of course create an interruptor for each thread if you want to explicitly interrupt one thread at a time.
Up to you to define the duration on the timed wait, so your threads are able to react as soon as your program require.
Wait boost 对 Interrupt() 的作用是在线程当前被阻塞的情况下发出一个 notification_all ,然后检查是否请求了中断。如果请求中断,那么它会抛出 boost::thread_interrupted。您可以编写自己的线程
基于 std::thread 的类具有该机制。
Wait boost does on the interrupt() is to emit a notify_all on the condition a thread is currently blocked and then check if an interruption was requested. If an interruption was requested then it does a throw boost::thread_interrupted. You can write a your own thread
class based on std::thread with the mechanism.
事实上,C++11 std::thread 还没有为此提供标准机制(还没有?)。因此,我支持 @doublep 暂时使用 boost::thread 的决定。
我们更喜欢使用 C++11 std:: 工具。但我们不想无缘无故地重新构建 boost 的
terminate()
设施,并使用条件变量来中断睡眠等。创建这样的基础设施很有趣,而且并不那么困难,但效率低下,而且比使用 boost 更不标准。boost::thread 和 std::thread 非常相似,这有助于我们感觉自己没有陷入困境。 (我们预计最终迁移到 std::thread 会相对容易,甚至可能微不足道。)
Indeed, C++11 std::thread does not offer a standard mechanism for this (yet?). Therefore I second @doublep's decision to use boost::thread for the time being.
We'd prefer to use C++11 std:: facilities. But we don't want to re-build boost's
terminate()
facility, complete with condition variable futzing to interrupt sleeps etc., for no reason. Creating infrastructure like that is fun, and not that difficult, but unproductive and less standard than using boost.It helps that boost::thread and std::thread are so similar that we feel we're not painting ourself into a corner. (We expect eventual migration to std::thread to be relatively easy, perhaps even trivial.)