gpt4 book ai didi

c++ - 我怎样才能从 boost::asio::post 获得 future ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:19 24 4
gpt4 key购买 nike

我正在使用 Boost 1.66.0,其中 asio 内置支持与 futures 的互操作(现在已经有一段时间了)。我在网上看到的示例说明了如何在使用 async_readasync_read_some 等网络功能时干净地实现这一点。这是通过提供 boost::asio::use_future 来完成的。代替完成处理程序,这会导致启动函数按预期返回 future

我需要提供什么样的对象或将我的函数包装在其中才能从 boost::asio::post 获得相同的行为?

我发布工作的目的是在链的上下文中执行它,否则等待工作完成,这样我就可以得到我想做的行为:

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::async_read 等函数相同的方式推导出来,所以我觉得必须有更好的方法来避免中间的 packaged_task。与 async_read 不同,post 没有“其他工作”要完成,因此仅提供 boost::asio::use_future 没有意义,但我们可以定义一个 async_result 特征来获得相同的 post 行为。

是否有包装器或定义了必要特征的东西来获得我想要的行为,还是我需要自己定义它?

最佳答案

更新:随着最近的 boost ,使用这个 much simpler answer


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 实现它的唯一选择是使用打包任务,真的。

真正的问题

它隐藏在“如何获得相同行为”部分(只是不是来自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 ();
}

您可以将它与 future 一起使用:

std::future<int> f = async_meaning_of_life(true, asio::use_future);
std::cout << f.get() << "\n";

或者您可以只使用处理程序:

async_meaning_of_life(true, [](error_code ec, int i) {
std::cout << i << " (" << ec.message() << ")\n";
});

简单演示: Live On Coliru

扩展演示

相同的机制扩展到支持协程(有或没有异常(exception))。 Asio pre-boost 1.66.0 的 async_result 略有不同。

在这里查看所有不同的形式:

Live On Coliru

#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

关于c++ - 我怎样才能从 boost::asio::post 获得 future ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49950679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com