我如何从boost :: asio :: post获得未来? [英] How can I get a future from boost::asio::post?
问题描述
我使用的是Boost 1.66.0,其中asio内置了对与期货进行互操作的支持(并且有一段时间).我在网上看到的示例说明了在使用诸如async_read
,async_read_some
等网络功能时如何干净地实现此目的.这可以通过提供
I am using Boost 1.66.0, in which asio has built-in support for interoperating with futures (and for some time now). The examples I've seen online indicate how to achieve this cleanly when using networking functions such as async_read
, async_read_some
, etc. That is done by providing boost::asio::use_future
in place of the completion handler, which causes the initiating function to return a future
as expected.
我需要提供或包装函数的哪种对象才能从boost::asio::post
获得相同的行为?
What kind of object do I need to provide or wrap my function in to get the same behavior from boost::asio::post
?
我发布工作的目的是在一小段上下文中执行它,但是要等到工作完成,这样我才能得到我想要做的事情:
My purpose for posting the work is to execute it in the context of a strand but otherwise wait for the work to complete, so I can get the behavior I want doing:
std::packaged_task<void()> task( [] { std::cout << "Hello world\n"; } );
auto f = task.get_future();
boost::asio::post(
boost::asio::bind_executor(
strand_, std::move( task ) ) );
f.wait();
,但是根据boost::asio
文档, boost::asio::post
的推导方式与boost::asio::use_future是没有道理的,但是我们可以定义async_result
特性以获取与发布相同的行为.
but according to the boost::asio
documentation, the return type for boost::asio::post
is deduced in the same way as for functions like boost::asio::async_read
, so I feel like there has to be a nicer way that can avoid the intermediate packaged_task
. Unlike async_read
there is no "other work" to be done by post
so providing just boost::asio::use_future
doesn't makes sense, but we could define an async_result
trait to get the same behavior for post.
是否有包装或定义了必要特征的东西才能获得我想要的行为,或者我需要自己定义它?
Is there a wrapper or something that has the necessary traits defined to get the behavior I want or do I need to define it myself?
推荐答案
我需要提供或包装函数的哪种对象才能从
boost::asio::post
获得相同的行为?
What kind of object do I need to provide or wrap my function in to get the same behavior from
boost::asio::post
?
不能. post
是无效操作.因此,使用post
实现此目标的唯一选择实际上是使用打包任务.
You can't. post
is a void operation. So the only option to achieve it with post
is to use a packaged-task, really.
它隐藏在如何获得相同的行为"部分中(并非来自post
):
It was hidden in the part "how to get the same behaviour" (just not from post
):
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token)
{
using result_type = typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(std::forward<Token>(token));
result_type result(handler);
if (success)
handler(error_code{}, 42);
else
handler(asio::error::operation_aborted, 0);
return result.get ();
}
您可以在将来使用它:
std::future<int> f = async_meaning_of_life(true, asio::use_future);
std::cout << f.get() << "\n";
或者您也可以使用处理程序:
Or you can just use a handler:
async_meaning_of_life(true, [](error_code ec, int i) {
std::cout << i << " (" << ec.message() << ")\n";
});
简单演示: 在Coliru上直播
相同的机制扩展到支持协程(有或没有例外). async_result
与Asio增强版1.66.0的舞曲略有不同.
The same mechanism extends to supporting coroutines (with or without exceptions). There's a slightly different dance with async_result
for Asio pre-boost 1.66.0.
在这里一起查看所有不同的形式:
See all the different forms together here:
#define BOOST_COROUTINES_NO_DEPRECATION_WARNING
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/use_future.hpp>
using boost::system::error_code;
namespace asio = boost::asio;
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token)
{
#if BOOST_VERSION >= 106600
using result_type = typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type
handler(std::forward<Token>(token));
asio::async_result<decltype (handler)> result (handler);
#endif
if (success)
handler(error_code{}, 42);
else
handler(asio::error::operation_aborted, 0);
return result.get ();
}
void using_yield_ec(asio::yield_context yield) {
for (bool success : { true, false }) {
boost::system::error_code ec;
auto answer = async_meaning_of_life(success, yield[ec]);
std::cout << __FUNCTION__ << ": Result: " << ec.message() << "\n";
std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
}
}
void using_yield_catch(asio::yield_context yield) {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, yield);
std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
} catch(boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "\n";
}
}
void using_future() {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch(boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "\n";
}
}
void using_handler() {
for (bool success : { true, false })
async_meaning_of_life(success, [](error_code ec, int answer) {
std::cout << "using_handler: Result: " << ec.message() << "\n";
std::cout << "using_handler: Answer: " << answer << "\n";
});
}
int main() {
asio::io_service svc;
spawn(svc, using_yield_ec);
spawn(svc, using_yield_catch);
std::thread work([] {
using_future();
using_handler();
});
svc.run();
work.join();
}
打印
using_yield_ec: Result: Success
using_yield_ec: Answer: 42
using_yield_ec: Result: Operation canceled
using_yield_ec: Answer: 0
using_yield_catch: Answer: 42
using_future: Answer: 42
using_yield_catch: Caught: Operation canceled
using_future: Answer: using_future: Caught: Operation canceled
using_handler: Result: Success
using_handler: Answer: 42
using_handler: Result: Operation canceled
using_handler: Answer: 0
这篇关于我如何从boost :: asio :: post获得未来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!