简体   繁体   English

用于封送并等待多个相互依赖的线程的算法,模式或最佳实践?

[英]Algorithm, pattern or best-practice for marshaling and waiting on multiple interdependent threads?

I am developing a (C++) program that during execution of a query may use multiple threads, some of which will be dependent on other threads started by itself or another thread within this query-process. 我正在开发一个(C ++)程序,该程序在执行查询过程中可能会使用多个线程,其中某些线程将依赖于由其自身启动的其他线程或此查询过程中的另一个线程。 Determining if a specific thread exists depends on specific parameters passed to the thread and this is part of thread-data in a list of active threads. 确定是否存在特定线程取决于传递给该线程的特定参数,这是活动线程列表中线程数据的一部分。 Individual threads may complete quickly while others may take a long time. 各个线程可能很快完成,而其他线程可能需要很长时间。

To prevent trying to reinvent the wheel, is there some algorithm, pattern or best-practice to handle marshaling and waiting on multiple interdependent threads? 为防止尝试重新发明轮子,是否有某种算法,模式或最佳实践来处理封送处理并在多个相互依赖的线程上等待?

You might want to have a look at futures and promises, and at the async wrapper interface in C++11. 您可能希望了解一下Future和Promise,以及C ++ 11中的异步包装器接口。

Promises allow you to deliver a result. 承诺使您能够交付结果。

Futures allow you to wait for a result. 期货让您等待结果。

In this way you can express a whole lot of dependencies of computation among threads, and since every consumer waits properly for its producer (which may themselves be consumers again) for a lot of cases the dependencies work out themselves kind of automatically, if you take care. 这样,您可以表示线程之间的许多计算依赖性,并且由于每个使用者在许多情况下都在适当地等待其生产者(它们本身又可能是使用者),因此在很多情况下,依赖性会自动自动产生关心。

More info: 更多信息:
A question about futures & promises 有关期货和承诺的问题
A very good explanation 一个很好的解释
A blog-entry about "broken promises" (- Bartosz Milewski) 关于违约”的博客条目(-Bartosz Milewski)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM