开发 C++具有“futures”的并发库或类似的范式
我正在开发一个 C++ 项目,需要在线程池中运行许多作业。这些作业很容易失败,这意味着我需要知道每个作业完成后如何终止。作为一名 Java 程序员,我喜欢使用“futures”或类似范例的想法,类似于 Java util.concurrent 包中的各种类。
我有两个问题:第一,C++ 中是否已经存在类似的东西(我在 Boost 中没有找到任何东西,但也许我寻找得不够努力);其次,这对于 C++ 来说是一个明智的想法吗?
我在这里找到了一个我想要完成的简单示例:
http://www.boostcookbook.com /Recipe:/1234841
这种方法有意义吗?
I'm working on a C++ project that needs to run many jobs in a threadpool. The jobs are failure-prone, which means that I need to know how each job terminated after it completes. Being a Java programmer for the most part, I like the idea of using "futures" or a similar paradigm, akin to the various classes in Java's util.concurrent package.
I have two questions: first, does something like this already exist for C++ (I haven't found anything in Boost, but maybe I'm not looking hard enough); and second, is this even a sane idea for C++?
I found a brief example of what I'm trying to accomplish here:
http://www.boostcookbook.com/Recipe:/1234841
Does this approach make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Future 都存在于即将推出的标准 (C++0x) 和内部 boost 中。请注意,虽然主名称
future
是相同的,但您需要阅读文档才能找到其他类型并理解语义。我不了解 Java future,所以我无法告诉您它们的不同之处(如果有的话)。boost 中的库是由 Anthony Williams 编写的,我相信他也参与了该部分的定义标准。他还撰写了 C++ 并发实践,其中包括对 future 的精彩描述、任务、承诺和相关对象。如果您有兴趣,他的公司还销售完整且最新的 C++0x 线程库实现。
Futures are both present in the upcoming standard (C++0x) and inside boost. Note that while the main name
future
is the same, you will need to read into the documentation to locate other types and to understand the semantics. I don't know Java futures, so I cannot tell you where they differ, if they do.The library in boost was written by Anthony Williams, that I believe was also involved in the definition of that part of the standard. He has also written C++ Concurrency in Action, that includes a good description of futures, tasks, promises and related objects. His company also sells a complete and up to implementation of the C++0x threading libraries, if you are interested.
Boost 实现了 future 和其他线程工具。
请注意,当您在
boost::unique_future
上调用get()
方法时,它将重新抛出异步执行期间可能存储在其中的任何异常。我建议你做类似的事情:
Boost has futures and other threading tools implemented.
Note that when you call the
get()
method on aboost::unique_future
it will re-throw any exception that might have been stored inside it during asynchronous execution.I would suggest you do something like:
C++ 模板比 Java 泛型限制更少,因此“Future”可以轻松地与它们和线程同步原语一起移植。至于支持这种机制的现有库,希望其他人知道。
C++ templates are less restrictive than Java Generics so 'Future's could readily be ported with them and thread synchronization primitives. As for existing libraries which support such a mechanism, hopefully someone else knows of one.