lur*_*her 6 c++ multithreading boost future c++11
我刚刚阅读了文章' Futures Done Right ',c ++ 11承诺的主要缺点似乎是从现有的创造复合期货
我现在正在查看boost :: wait_for_any的文档
但请考虑以下示例:
int calculate_the_answer_to_life_the_universe_and_everything()
{
return 42;
}
int calculate_the_answer_to_death_and_anything_in_between()
{
return 121;
}
boost::packaged_task<int> pt(calculate_the_answer_to_life_the_universe_and_everything);
boost:: future<int> fi=pt.get_future();
boost::packaged_task<int> pt2(calculate_the_answer_to_death_and_anything_in_between);
boost:: future<int> fi2=pt2.get_future();
....
int calculate_the_oscillation_of_barzoom(boost::future<int>& a, boost::future<int>& b)
{
boost::wait_for_all(a,b);
return a.get() + b.get();
}
boost::packaged_task<int> pt_composite(boost::bind(calculate_the_oscillation_of_barzoom, fi , fi2));
boost:: future<int> fi_composite=pt_composite.get_future();
Run Code Online (Sandbox Code Playgroud)
这种可组合性方法有什么问题?这是实现可组合性的有效方法吗?我们需要一些优雅的语法功能吗?
when_any
并且when_all
是构成期货的完美有效方式.它们都对应于并行组合,其中复合操作等待一个或所有组合操作.
我们还需要顺序组合(不在Boost.Thread中).例如,这可以是一个future<T>::then
函数,它允许您排队使用未来值的操作,并在未来准备就绪时运行.可以自己实现这一点,但需要进行效率权衡.Herb Sutter在最近的Channel9视频中谈到了这一点.
N3428是一个将这些功能(以及更多)添加到C++标准库的提案草案.它们都是库功能,不会为该语言添加任何新语法.此外,N3328是一个为内部使用的可恢复函数(如使用async
/ await
在C#中)添加语法的提议future<T>::then
.
归档时间: |
|
查看次数: |
2990 次 |
最近记录: |