asio::io_service::run 在 boost::asio::io_service::work 被销毁后不会返回

问题描述

正如标题所说,当我不明白为什么这个线程时,我遇到了这个问题

enter image description here

即使在 Client 的析构函数销毁 asio::io_service::work 变量后也不会停止运行

enter image description here

当我运行这个程序时,输出总是这样

enter image description here

有人看到我在这里遗漏了什么吗?

#include <boost/asio.hpp>
#include <thread>
#include <atomic>
#include <memory>

using namespace boost;


class Client{
public:

    Client(const Client& other) = delete;

    Client()
    {
        m_work.reset(new boost::asio::io_service::work(m_ios));

        m_thread.reset(new std::thread([this]()
        {
            m_ios.run();
        }));
    }

    ~Client(){ close(); }

private:

    void close();

    asio::io_service m_ios;
    std::unique_ptr<boost::asio::io_service::work> m_work;
    std::unique_ptr<std::thread> m_thread;

};

void Client::close()
{
    m_work.reset(nullptr);

    if(m_thread->joinable())
    {
        std::cout << "before joining thread" << std::endl;
        m_thread->join();
        std::cout << "after joining thread" << std::endl;
    }
}



int main()
{
    {
         Client client;
    }

    return 0;
}

编辑


StPiere 评论之后,我将代码更改为此,它起作用了 :)

class Client{
public:

    Client(const Client& other) = delete;

    Client()
    {
        m_work.reset(new boost::asio::executor_work_guard<boost::asio::io_context::executor_type>(boost::asio::make_work_guard(m_ios)));
        m_thread.reset(new std::thread([this]()
        {
            m_ios.run();
        }));
    }

    ~Client(){ close(); }

private:

    void close();

    asio::io_context m_ios;

    std::unique_ptr<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>> m_work;
    std::unique_ptr<std::thread> m_thread;

};

解决方法

我无法在任一编译器上重现该错误。 这里 example 用于 gcc 9.3 和 boost 1.73

通常情况下,工作析构函数会在 Windows 上使用类似 InterLockedDecrement 之类的东西来减少未完成工作的数量。

看起来像是一些编译器或 io_service/work 实现问题。

如评论中所述,io_serviceio_service::workio_contextexecutor_work_guard 方面被弃用