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

kla*_*aus 1 boost deadlock pthreads boost-asio

我使用封装了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;
};
Run Code Online (Sandbox Code Playgroud)

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

terminate called after throwing an instance of 'std::system_error'
what():  Resource deadlock avoided
Aborted
Run Code Online (Sandbox Code Playgroud)

你怎么认为 ?

caf*_*caf 5

这就是当一个线程试图加入自己时你得到的错误。

所以听起来你的问题是你stop()从一个由io_service.