asio use_future而不是yield [ec] [英] asio use_future instead of yield[ec]

查看:141
本文介绍了asio use_future而不是yield [ec]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作期货容器,每个期货都是任务的无效结果,因此我可以在容器上使用wait_for_any,每个任务都是协程,我目前使用yield_context实现,并且在此协程中有返回ec的初始化函数和结果,其中使用ec分析结果.然后另一个协程称为传递相同yield_context.
我想知道如何进行此设计.
并且,如果我将使用use_future,除非将其抛出,否则除非将其抛出,否则如何将错误代码传递给ec而不抛出它,在这种情况下,我将尝试捕获异步启动函数.
所有这些任务将在asio io_service上发布,生成....
这是我的主要代码部分:
这是任务的产生

i want to make container of futures ,each future is void result of a task so that i could use wait_for_any on the container ,each task is coroutine which i currently implement using yield_context,and inside this coroutine there initiating function which returns ec and result where i use ec to analyze result.and then another coroutine is called passes same yield_context .
i want to know how to make this design.
and if i ll use use_future ,how can i pass error code to ec not throwing it unless there is no way except throwing it ,in this case i ll put try and catch around async initiating functions.
all these tasks will be posted ,spawned ... on asio io_service .
this is my main parts of code:
this is the spawn of task

boost::asio::spawn(GetServiceReference(), boost::bind(&HTTPRequest::Execute, boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map()));

这是使用yield_context的协程

and this is the coroutine using 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进行分析

and inside Execute we use ec to analyze

if (ec == boost::system::errc::errc_t::success){}

在这里,我们开始另一个传递相同yield_context的协程

and here we start another coroutine passing same yield_context

SendRequest(yield_r);
}

我想更改此设置,因此我拥有所有衍生的Execute的期货容器,我不在乎Execute的结果,因为我将它们放入成员类Response中.
但是我将来需要结果,以便可以在容器上使用wait_any.

i want to change this so i have container of futures for all spawned Execute,i do not care about results of Execute because i put them to member class Response.
But i need result in future so that i can use wait_any on the container .

推荐答案

如果可以更改实现,请使用async_result模式.

If you can change your implementation, use the async_result pattern.

这使得您可以将您的方法与任何方法(补全处理程序,yield上下文或use_future)一起使用.

This makes it so you can use your method with any of the approaches (completion handler, yield context or use_future).

我从此处中复制了独立的示例,以寻求启发:

I reproduce the self-contained example from here for inspiration:

显示如何与

  • coro和产量[ec]
  • coro和yield +例外
  • std :: future
  • 完成处理程序

在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_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

这篇关于asio use_future而不是yield [ec]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆