调用boost::asio::io_service::从std::线程中运行
Calling boost::asio::io_service::run from a std::thread
我有一个类来处理我的连接,它有一个boost::asio::io_service成员。我想从std::线程调用io_service::run(),但是我遇到了编译错误。
std::thread run_thread(&boost::asio::io_service, std::ref(m_io_service));
不起作用。我看到了很多使用boost::thread来做这件事的例子,但是我想坚持使用std::thread。有什么建议吗?
谢谢
我知道有两种方法,一种是通过lambda创建std::thread。
std::thread run_thread([&]{ m_io_service.run(); });
另一种方法是用boost::bind
创建boost::线程boost::thread run_thread(boost::bind(&boost::asio::io_service::run, boost::ref(m_io_service)));
只是对@cbel的回答进行了一点扩展。如果您(无论出于何种原因)想要避免boost::thread和lambdas:
,还有另一种方法std::thread run_thread(
std::bind(static_cast<size_t(boost::asio::io_service::*)()>(
&boost::asio::io_service::run), std::ref(m_io_service)));
对于我来说,标记为解决方案的答案的两个选项都导致例外。
对我起作用的是:
boost::thread run_thread([&] { m_io_service.run(); });
不是std::thread run_thread([&]{ m_io_service.run(); });
相关文章:
- 即发即弃 std::线程对象清理自身
- std::线程导致程序中止
- 在std::线程中使用已分配的结构数据
- 使用-static libstdc++时std::线程弱,因此在运行时会导致崩溃
- std::线程与 pthread_setschedparam 与 C++ 11.
- 运行 std::线程不在构造函数中
- C++ std::线程调用方法,从对象原因到调用此类的析构函数
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- 错误:静态断言失败:std ::线程参数必须在转换为rvalues后不可行
- C STD ::线程中的种族状况或内存损坏
- 在 Valgrind 输出中仍然可以访问 std 线程向量的块
- 将类Cotaining std ::线程添加到向量
- 如何使用mingw编译C STD ::线程代码
- 从其他std ::线程更新QT GUI
- STD ::线程不会退出
- 处理 std::线程包装器类的立即销毁
- C++ std 线程并列出分段错误(核心转储)
- 杀死后的STD ::线程如何处理
- 干净取消在输入/输出调用中被阻止的 std::线程
- std ::线程最大并发线程