emulate std::async with std::launch::sync?
emulate std::async with std::launch::sync?
我想实现这样的方法:
boost::unique_future<int> foo()
{
return defer([=] // call this lambda on boost::unique_future<int>::get();
{
return prime(10);
});
}
我知道boost::promise
、boost::packaged_task
和set_wait_callback
,但是,由于返回的future引用了其中一个,这将不起作用吗?
我知道有std::async
和std::launch::sync
,但如何使用boost和vs2010来模拟它?
std::unique_future<int> foo()
{
return std::async(std::launch::sync, [=]
{
return prime(10);
});
}
您可以使用set_wait_callback
,您只需要动态分配promise,并在设置值后在回调中删除它:
#include <boost/thread.hpp>
#include <iostream>
void callback(boost::promise<int>&p) {
std::cout<<"callback"<<std::endl;
p.set_value(42);
delete &p;
}
boost::unique_future<int> f()
{
boost::promise<int> *p=new boost::promise<int>;
p->set_wait_callback(callback);
return p->get_future();
}
int main()
{
boost::unique_future<int> uf(f());
std::cout<<"main()"<<std::endl;
int val=uf.get();
std::cout<<"val="<<val<<std::endl;
}
您可以使用此票证的代码:https://svn.boost.org/trac/boost/ticket/4710
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 什么时候应该使用std::async和sync作为策略
- emulate std::async with std::launch::sync?