将 Boost strand 与 std::future 结合使用
Use Boost strand in conjunction with std::future
我遇到了一个用例,我想将 Boost 链与 std::future 结合使用。
为了减少代码重复,我编写了一个通用函数,它将 post 一个任务发送到 boost strand 和 return 未来。
// Some definitions first...
typedef boost::asio::io_service::strand cb_strand;
typedef std::shared_ptr< cb_strand > cb_strand_ptr;
代码类似于:
//////////////////////////////////////////////////////////////////////////
template <class Task>
auto post_future_to_strand(cb_strand_ptr apStrand, Task task)
{
using return_type = decltype(task());
auto promise = std::make_shared<std::promise<return_type>>();
auto future = promise->get_future();
apStrand->wrap
(
[promise, task]()
{
try
{
promise->set_value(task());
}
catch (...)
{
// LOG ERROR ...
// NOTE: Exceptions can be thrown when setting the exception!
try
{
promise->set_exception(std::current_exception());
}
catch (...)
{
//LOG ERROR ...
}
}
}
);
return future;
};
然后我希望 post 链的未来,如下例所示:
std::future<int> f = post_future_to_strand(m_apStrand, std::bind(&foo::bar, this))
std::cout << "foo::bar() -> int is " << f.get() << std::endl;
不幸的是,我遇到运行时异常:
terminate called after throwing an instance of 'std::future_error'
what(): std::future_error: Broken promise
Signal: SIGABRT (Aborted)
阅读文档后,我想我明白什么是失信以及这种情况是如何发生的;但是,我觉得我正在捕捉 lambda 中的承诺,所以一切都会好起来的。我是这个lambdas世界的新手,所以我的理解可能有误。
- Ubuntu 热情
- GCC 6.3(使用 cmake 为 C++14 配置)
你包装了任务,但你从来没有post它。因此,包装的任务会立即销毁,并随之销毁 promise。
还有一个陷阱,只有当你 运行 io_service 在一个不同的线程上而不是为未来阻塞的线程上时,事情才有效......否则你已经创建了一个死锁:
现在您有多个线程,您需要首先避免在任务 posted 之前服务退出的竞争条件。
Bonus:
I'd suggest a far simpler take on the wrapper:
template <typename Task>
auto post_future_to_strand(cb_strand_ptr apStrand, Task task)
{
auto package = std::make_shared<std::packaged_task<decltype(task())()> >(task);
auto future = package->get_future();
apStrand->post([package] { (*package)(); });
return future;
}
完整演示
#include <boost/asio.hpp>
#include <future>
#include <iostream>
using cb_strand_ptr = boost::asio::strand*;
//////////////////////////////////////////////////////////////////////////
template <typename Task>
auto post_future_to_strand(cb_strand_ptr apStrand, Task task)
{
auto package = std::make_shared<std::packaged_task<decltype(task())()> >(task);
auto future = package->get_future();
apStrand->post([package] { (*package)(); });
return future;
}
struct Foo {
boost::asio::strand s;
cb_strand_ptr m_apStrand = &s;
Foo(boost::asio::io_service& svc) : s{svc} {}
void do_it() {
std::future<int> f = post_future_to_strand(m_apStrand, std::bind(&Foo::bar, this));
std::cout << "foo::bar() -> int is " << f.get() << std::endl;
}
int bar() {
return 42;
}
};
int main() {
boost::asio::io_service svc;
auto lock = std::make_unique<boost::asio::io_service::work>(svc); // prevent premature exit
std::thread th([&]{ svc.run(); });
Foo foo(svc);
foo.do_it();
lock.reset(); // allow service to exit
th.join();
}
版画
foo::bar() -> int is 42
我遇到了一个用例,我想将 Boost 链与 std::future 结合使用。
为了减少代码重复,我编写了一个通用函数,它将 post 一个任务发送到 boost strand 和 return 未来。
// Some definitions first...
typedef boost::asio::io_service::strand cb_strand;
typedef std::shared_ptr< cb_strand > cb_strand_ptr;
代码类似于:
//////////////////////////////////////////////////////////////////////////
template <class Task>
auto post_future_to_strand(cb_strand_ptr apStrand, Task task)
{
using return_type = decltype(task());
auto promise = std::make_shared<std::promise<return_type>>();
auto future = promise->get_future();
apStrand->wrap
(
[promise, task]()
{
try
{
promise->set_value(task());
}
catch (...)
{
// LOG ERROR ...
// NOTE: Exceptions can be thrown when setting the exception!
try
{
promise->set_exception(std::current_exception());
}
catch (...)
{
//LOG ERROR ...
}
}
}
);
return future;
};
然后我希望 post 链的未来,如下例所示:
std::future<int> f = post_future_to_strand(m_apStrand, std::bind(&foo::bar, this))
std::cout << "foo::bar() -> int is " << f.get() << std::endl;
不幸的是,我遇到运行时异常:
terminate called after throwing an instance of 'std::future_error'
what(): std::future_error: Broken promise
Signal: SIGABRT (Aborted)
阅读文档后,我想我明白什么是失信以及这种情况是如何发生的;但是,我觉得我正在捕捉 lambda 中的承诺,所以一切都会好起来的。我是这个lambdas世界的新手,所以我的理解可能有误。
- Ubuntu 热情
- GCC 6.3(使用 cmake 为 C++14 配置)
你包装了任务,但你从来没有post它。因此,包装的任务会立即销毁,并随之销毁 promise。
还有一个陷阱,只有当你 运行 io_service 在一个不同的线程上而不是为未来阻塞的线程上时,事情才有效......否则你已经创建了一个死锁:
现在您有多个线程,您需要首先避免在任务 posted 之前服务退出的竞争条件。
Bonus:
I'd suggest a far simpler take on the wrapper:
template <typename Task> auto post_future_to_strand(cb_strand_ptr apStrand, Task task) { auto package = std::make_shared<std::packaged_task<decltype(task())()> >(task); auto future = package->get_future(); apStrand->post([package] { (*package)(); }); return future; }
完整演示
#include <boost/asio.hpp>
#include <future>
#include <iostream>
using cb_strand_ptr = boost::asio::strand*;
//////////////////////////////////////////////////////////////////////////
template <typename Task>
auto post_future_to_strand(cb_strand_ptr apStrand, Task task)
{
auto package = std::make_shared<std::packaged_task<decltype(task())()> >(task);
auto future = package->get_future();
apStrand->post([package] { (*package)(); });
return future;
}
struct Foo {
boost::asio::strand s;
cb_strand_ptr m_apStrand = &s;
Foo(boost::asio::io_service& svc) : s{svc} {}
void do_it() {
std::future<int> f = post_future_to_strand(m_apStrand, std::bind(&Foo::bar, this));
std::cout << "foo::bar() -> int is " << f.get() << std::endl;
}
int bar() {
return 42;
}
};
int main() {
boost::asio::io_service svc;
auto lock = std::make_unique<boost::asio::io_service::work>(svc); // prevent premature exit
std::thread th([&]{ svc.run(); });
Foo foo(svc);
foo.do_it();
lock.reset(); // allow service to exit
th.join();
}
版画
foo::bar() -> int is 42