簡體   English   中英

asio 使用_future 而不是 yield[ec]

[英]asio use_future instead of yield[ec]

我想制作期貨容器,每個未來都是任務的無效結果,以便我可以在容器上使用wait_for_any,每個任務都是我目前使用yield_context實現的協程,並且在這個協程內部啟動function,它返回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);
}

我想改變這一點,所以我有所有生成的 Execute 的期貨容器,我不關心 Execute 的結果,因為我把它們放到了成員 class 響應中。
但我需要將來的結果,以便我可以在容器上使用 wait_any。

如果您可以更改您的實現,請使用 async_result 模式。

這使得您可以將您的方法與任何方法(完成處理程序、yield context 或use_future )一起使用。

我從這里復制了這個獨立的例子以獲得靈感:

綜合演示

展示如何使用 with

  • coro's 和產量[ec]
  • coro 和 yield + 異常
  • 標准::未來
  • 完成處理程序

住在科利魯

#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

注意:為簡單起見,我沒有添加 output 同步,因此 output 可以根據運行時執行順序混合

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM