加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 未来的可组合性,以及boost :: wait_for_all

发布时间:2020-12-16 03:38:48 所属栏目:百科 来源:网络整理
导读:我刚刚阅读了文章’ Futures Done Right‘,c 11承诺的主要缺点似乎是从现有的创造复合期货 我现在正在查看boost::wait_for_any的文档 但请考虑以下示例: int calculate_the_answer_to_life_the_universe_and_everything(){ return 42;}int calculate_the_an
我刚刚阅读了文章’ 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();

这种可组合性方法有什么问题?这是实现可组合性的有效方法吗?我们需要一些优雅的语法功能吗?

解决方法

when_any和when_all是构成期货的完美有效方式.它们都对应于并行组合,其中复合操作等待一个或所有组合操作.

我们还需要顺序组合(不在Boost.Thread中).例如,这可能是一个未来的< T> :: then函数,它允许您排队使用未来值的操作,并在未来准备就绪时运行.可以自己实现这一点,但需要进行效率权衡. Herb Sutter在他的recent Channel9 video中谈到了这一点.

N3428是将这些功能(以及更多)添加到C标准库的提案草案.它们都是库功能,不会为该语言添加任何新语法.另外,N3328是一个为可恢复函数添加语法的提议(比如在C#中使用async / await),它将在内部使用future< T> :: then.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读