joining a thread: “resource deadlock avoided”

帅比萌擦擦* 提交于 2020-07-27 03:34:59

问题


I use a c++ class that encapsulates a boost::asio::io_service.

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;
};

But when I am calling the stop method, the program is crashing on the join:

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

What do you think ?


回答1:


That's the error that you get when a thread tries to join itself.

So it sounds like your problem is that you're calling the stop() method from a handler function that was invoked by the io_service.



来源:https://stackoverflow.com/questions/56007421/joining-a-thread-resource-deadlock-avoided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!