我想制作 future 的容器,每个 future 都是一个任务的无效结果,这样我就可以在容器上使用 wait_for_any,每个任务都是我目前使用 yield_context 实现的协程,并且在这个协程中有返回 ec 的启动函数和我使用 ec 分析结果的结果。然后调用另一个协程传递相同的 yield_context 。
我想知道如何制作这个设计。
如果我将使用 use_future,我如何将错误代码传递给 ec 而不是抛出它,除非除了抛出它别无他法,在这种情况下,我将尝试并捕获异步启动函数。
所有这些任务都将发布、生成……在 asio io_service 上。
这是我的主要代码部分:
这是任务的产物
boost::asio::spawn(GetServiceReference(), boost::bind(&HTTPRequest::Execute, boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map()));
这是使用 yield_context 的协程
void HTTPRequest::Execute(boost::asio::yield_context yield_r, std::string request_name, std::map<std::string, boost::shared_ptr<HTTPResponse>>& mHTTPClient_Responses_Map)
{
resolver_iterator iterator_connect = boost::asio::async_connect(mSock, iterator_resolve, yield_r[ec]);
}
在Execute里面我们用ec来分析
if (ec == boost::system::errc::errc_t::success){}
在这里我们启动另一个传递相同 yield_context 的协程
SendRequest(yield_r);
}
我想改变这个,所以我有所有衍生执行的 future 容器,我不关心执行的结果,因为我把它们放在成员类 Response 中。
但是我将来需要结果,以便我可以在容器上使用 wait_any。
最佳答案
如果您可以更改您的实现,请使用 async_result 模式。
这使得您可以将您的方法与任何方法(完成处理程序、yield 上下文或 use_future
)一起使用。
我重现了 here 中的独立示例灵感:
综合演示
显示如何使用它
- coro 和产量[ec]
- coro 和 yield + 异常
- 标准:: future
- 完成处理程序
#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_future: Answer: 42
using_yield_catch: Answer: 42
using_yield_catch: Caught: Operation canceled
using_future: Caught: Operation canceled
using_handler: Result: Success
using_handler: Answer: 42
using_handler: Result: Operation canceled
using_handler: Answer: 0
Note: for simplicity I have not added output synchronization, so the output can become intermingled depending on runtime execution order
关于boost-asio - asio use_future 而不是 yield[ec],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61026135/