连接一根线:“连接一根线”;“避免资源死锁”; < P>我使用C++类封装了 Boo::Asio::IOIOService >/P> 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; };

连接一根线:“连接一根线”;“避免资源死锁”; < P>我使用C++类封装了 Boo::Asio::IOIOService >/P> 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; };,boost,pthreads,boost-asio,deadlock,Boost,Pthreads,Boost Asio,Deadlock,但是当我调用stop方法时,程序正在连接上崩溃: terminate called after throwing an instance of 'std::system_error' what(): Resource deadlock avoided Aborted 你怎么看?这就是线程试图加入自身时所遇到的错误 因此,听起来您的问题是,您正在从由io\u服务调用的处理程序函数调用stop()方法,这是线程尝试加入自身时出现的错误 因此,听起来您的问题是您正在从处理程序函数调用stop()方

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

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

你怎么看?

这就是线程试图加入自身时所遇到的错误


因此,听起来您的问题是,您正在从由
io\u服务调用的处理程序函数调用
stop()
方法,这是线程尝试加入自身时出现的错误


因此,听起来您的问题是您正在从处理程序函数调用
stop()
方法,该处理程序函数是由
io\u服务调用的

Provide MCVE,io\u服务没有
join
方法。显示创建IoService的代码以及如何调用start/stop。IoService析构函数的主体在哪里?我的错,它是一个输入错误。我在std线程上呼叫join!提供MCVE,io_服务没有
join
方法。显示创建IoService的代码以及如何调用start/stop。IoService析构函数的主体在哪里?我的错,它是一个输入错误。我在std线程上呼叫join!我不太明白。。。当我调用_ioService.stop()方法时,看起来我没有退出_ioService.run();方法。这是预期的吗?也许这就是连接不起作用的原因…?问题在于调用
IoService::stop()
的线程上下文。您正在从处理程序函数调用它,这意味着它正由运行
io\u service::run()
的线程运行,该线程与您尝试加入的线程相同。您需要从另一个线程调用
::stop()
函数。为了更清楚地了解如何使用IoService类,我还有另外两个类a和B,它们有一个私有的
IoService&\u IoService并在A的描述器中调用_ioService.stop()。坏吗?重要的是哪个线程运行它-那么,哪个线程最终运行A的析构函数?我不确定是否理解。。。当我调用_ioService.stop()方法时,看起来我没有退出_ioService.run();方法。这是预期的吗?也许这就是连接不起作用的原因…?问题在于调用
IoService::stop()
的线程上下文。您正在从处理程序函数调用它,这意味着它正由运行
io\u service::run()
的线程运行,该线程与您尝试加入的线程相同。您需要从另一个线程调用
::stop()
函数。为了更清楚地了解如何使用IoService类,我还有另外两个类a和B,它们有一个私有的
IoService&\u IoService并在A的描述器中调用_ioService.stop()。坏吗?重要的是哪个线程运行它-那么,哪个线程最终运行A的析构函数?