C ++ 0x线程中断 [英] C++0x thread interruption

查看:107
本文介绍了C ++ 0x线程中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C ++ 0x最终草案,没有办法请求线程终止。也就是说,如果需要,我们需要实现一个自己动手的解决方案。

According to the C++0x final draft, there's no way to request a thread to terminate. That said, if required we need to implement a do-it-yourself solution.

另一方面,boost :: thread提供了一个机制, em>安全方式。

On the other hand boost::thread provides a mechanism to interrupt a thread in a safe manner.

在你看来,最好的解决方案是什么?设计自己的合作中断机制还是原生?

In your opinion, what's the best solution? Designing your own cooperative 'interruption mechanism' or going native?

推荐答案

所有的语言规范说支持不是内置的语言。
boost :: thread :: interrupt 需要线程函数的一些支持:

All the language specification says that the support isn't built into the language. boost::thread::interrupt needs some support from the thread function, too:

em>当被中断的线程下一次执行指定的中断点之一(或者如果它在执行一个中断点时被阻塞)

When the interrupted thread next executes one of the specified interruption points (or if it is currently blocked whilst executing one)

ie当线程函数不给调用者一个机会中断,你仍然卡住。

i.e. when the thread function doesn't give the caller a chance to interrupt, you are still stuck.

我不知道你是什么意思与去本地 - 没有本机支持,除非你spellbound到 boost:threads

I'm not sure what you mean with "going native" - there is no native support, unless you are spellbound to boost:threads.

仍然,我会使用显式机制。你必须考虑有足够的中断点,为什么不让他们明确?额外的代码在我的经验中通常是边缘的,虽然你可能需要改变从单对象到多对象的一些等待,这取决于你的库 - 可能看起来更丑陋。

Still, I'd use an explicit mechanism. You have to think about having enough interruption points anyway, why not make them explicit? The extra code is usually marginal in my experience, though you may need to change some waits from single-object to multiple-objects, which - depending on your library - may look uglier.

也可以拉动不要使用控制流异常,但与线程,这只是一个指南。

One could also pull the "don't use exceptions for control flow", but compared to messing around with threads, this is just a guideline.

这篇关于C ++ 0x线程中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆