加入线程:“避免资源死锁”

时间:2019-05-06 14:40:01

标签: boost pthreads boost-asio deadlock

我使用一个封装了 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

您怎么看?

1 个答案:

答案 0 :(得分:0)

这是线程尝试加入自身时遇到的错误。

所以听起来您的问题是您正在从stop()调用的处理函数中调用io_service方法。