首页 > 解决方案 > 加入线程:“避免资源死锁”

问题描述

我使用一个封装了boost::asio::io_service的 c++ 类。

class IoService {
 public:
  static IoService& getInstance() {
    static IoService instance;
    return instance;
  }
  void start() {
    _ioServiceThread = std::thread(&IoService::run, this);
  }
  void stop() {
    _ioService.stop();
    _ioServiceThread.join();
  }
  void run() {
   _ioService.run();
  }

 private:
  IoService();
  ~IoService();
  IoService(const IoService& old) = delete;
  IoService(const IoService&& old) = delete;
  IoService& operator=(const IoService& old) = delete;
  IoService& operator=(const IoService&& old) = delete;

  boost::asio::io_service _ioService;
  std::thread _ioServiceThread;
};

但是当我调用 stop 方法时,程序在加入时崩溃:

terminate called after throwing an instance of 'std::system_error'
what():  Resource deadlock avoided
Aborted

你怎么看 ?

标签: boostpthreadsboost-asiodeadlock

解决方案


这就是线程尝试加入自身时出现的错误。

因此,听起来您的问题是您正在stop()从由io_service.


推荐阅读