加入线程:“避免资源死锁"; [英] joining a thread: "resource deadlock avoided"

查看:248
本文介绍了加入线程:“避免资源死锁";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的c ++类封装了 boost :: asio :: io_service .

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

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

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

您怎么看?

推荐答案

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

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

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

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.

这篇关于加入线程:“避免资源死锁";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆