c++ - 将 Boost.Coroutine 与 Boost.ASIO 一起使用会导致断言

标签 c++ boost boost-asio boost-coroutine

我有在多个线程中运行的 boost::asio::io_context(通过 ctx.run()),我有几个在 boost.coroutines 中使用的异步对象,它们通过 boost::asio::运行产卵。我有以下断言:

Assertion failed: ! is_running(), file C:\boost\boost_1_68_0\msvc_x86\include\boost-1_68\boost\coroutine\detail\push_coroutine_impl.hpp, line 258

我提供了一个导致相同错误的最小示例。请帮助我:我做错了什么?

boost 版本为1.68

[更新]:代码中有一个修复(忘记锁定 io_context),但它不影响断言。

#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>

template<class CompletionToken>
BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(boost::system::error_code))
asyncDo(boost::asio::io_context& ctx, CompletionToken&& token)
{
    using CompletionType = boost::asio::async_completion<CompletionToken, void(boost::system::error_code)>;
    CompletionType completion{ token };
    ctx.post(
        [handler{ completion.completion_handler }](){
            using boost::asio::asio_handler_invoke;
            asio_handler_invoke(std::bind(handler, boost::system::error_code()), &handler);
            return;
        });
    return completion.result.get();
}

void coroFunc(boost::asio::io_context& ctx, boost::asio::yield_context yield)
{
    for (;;) {
        std::cerr << std::this_thread::get_id() << '\n';
        asyncDo(ctx, yield);
        std::cerr << std::this_thread::get_id() << '\n';
    }
}

int main(int, char* [])
{
    boost::asio::io_context ctx;
    boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work(ctx.get_executor());

    boost::asio::spawn(ctx, std::bind(coroFunc, std::ref(ctx), std::placeholders::_1));

    std::thread([&ctx]() { ctx.run(); }).detach();
    std::thread([&ctx]() { ctx.run(); }).detach();

    std::cin.get();
    work.reset();

    return 0;
}

最佳答案

经过几个小时的谷歌搜索和尝试,我找到了适合我的解决方案(至少在我的测试中)。主要思想是将发布到 ctx 替换为发布到 associated executor:

    auto executor = boost::asio::get_associated_executor(completion.completion_handler, ctx);
    auto allocator = boost::asio::get_associated_allocator(completion.completion_handler);

    executor.post(
        [handler = HandlerType{ std::move(completion.completion_handler) }](){
            using boost::asio::asio_handler_invoke;
            asio_handler_invoke(std::bind(handler, boost::system::error_code()), &handler);
            return;
        },
        allocator);

完整代码:

#include <iostream>
#include <stdexcept>
#include <thread>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>


template<class CompletionToken>
BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(boost::system::error_code))
asyncDo(boost::asio::io_context& ctx, CompletionToken&& token)
{
    using CompletionType = boost::asio::async_completion<CompletionToken, void(boost::system::error_code)>;
    using HandlerType = typename CompletionType::completion_handler_type;
    CompletionType completion{ token };

    auto executor = boost::asio::get_associated_executor(completion.completion_handler, ctx);
    auto allocator = boost::asio::get_associated_allocator(completion.completion_handler);

    executor.post(
        [handler = HandlerType{ std::move(completion.completion_handler) }](){
            using boost::asio::asio_handler_invoke;
            asio_handler_invoke(std::bind(handler, boost::system::error_code()), &handler);
            return;
        },
        allocator);

    return completion.result.get();
}

void coroFunc(boost::asio::io_context& ctx, boost::asio::yield_context yield)
{
    for (;;) {
        try {
            std::cerr << "(0): " << std::this_thread::get_id() << '\n';
            asyncDo(ctx, yield);
            std::cerr << "(1): " << std::this_thread::get_id() << '\n';
        } catch (std::exception const& e) {
            std::cerr << "e: " << e.what() << '\n';
        }
    }
}

int main(int, char* [])
{
    boost::asio::io_context ctx;
    boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work(ctx.get_executor());

    boost::asio::spawn(ctx, std::bind(coroFunc, std::ref(ctx), std::placeholders::_1));

    std::thread([&ctx]() {
        for (;;) {
            try {
                ctx.run();
                break;
            } catch (std::exception const& e) {
                std::cerr << "e: " << e.what() << '\n';
            }
        }
    }).detach();
    std::thread([&ctx]() {
        for (;;) {
            try {
                ctx.run();
                break;
            } catch (std::exception const& e) {
                std::cerr << "e: " << e.what() << '\n';
            }
        }
    }).detach();

    std::cin.get();
    work.reset();

    return 0;
}

关于c++ - 将 Boost.Coroutine 与 Boost.ASIO 一起使用会导致断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57667531/

相关文章:

C++、COM 和传递字符串

c++ - boost::lockfree 中的 "wait-free"到底是什么意思?

c++ - 使用 boost::asio 处理 "reset by peer"场景

c++ - 使用 bitset 代替手写位操作代码?

c++ - Boost Asio - 消息内容传输错误

c++ - 增长 managed_shared_memory 段后出现段错误

c++ - Boost Asio,异步 UDP 客户端 - 关机时崩溃

c++ - shared_from_this() 在 this 指向的对象被销毁后调用 : C++ ASIO

c++ - 编译器对 new 返回值的行为

c++ - 将 boost::tokenizer 与 boost::iterator_range 结合使用