调用boost :: asio :: io_service ::从std :: thread运行

jre*_*ady 4 c++ boost boost-asio

我有一个处理我的连接的类,它有一个boost :: asio :: io_service成员.我想从std :: thread调用io_service :: run(),但是我遇到了编译错误.

std::thread run_thread(&boost::asio::io_service, std::ref(m_io_service));
Run Code Online (Sandbox Code Playgroud)

不行.我看到使用boost :: thread做这个的各种例子,但是我想坚持使用std :: thread.有什么建议?

谢谢

cbe*_*bel 11

我知道有两种方法,一种是用lambda创建std :: thread.

std::thread run_thread([&]{ m_io_service.run(); });
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用boost :: bind创建boost :: thread

boost::thread run_thread(boost::bind(&boost::asio::io_service::run, boost::ref(m_io_service)));
Run Code Online (Sandbox Code Playgroud)