Boost::this_thread::get_id在exit()调用期间不返回id
boost::this_thread::get_id not returning id during exit() call
我有一个相当简单的c++应用程序,其中有两个线程——主线程和一个工作线程。工作线程等待通过网络发送的数据包,当接收到断开连接时,调用exit(0)。
这会导致负责线程的类的析构函数运行。这个析构函数通常从主线程运行,并导致工作线程(如果正在运行)停止,然后调用join来等待它停止。下面是析构函数的代码:
if( m_thread.get_id() == boost::this_thread::get_id() )
{
return;
}
if ( m_thread.joinable() )
{
m_runThread = false;
m_thread.join();
}
我的问题是
这行if( m_thread.get_id() == boost::this_thread::get_id() )
返回false,尽管我可以从调试中看出这段代码肯定是在m_thread引用的线程上运行的。
在对此进行了更多的研究之后,boost::this_thread::get_id()此时返回的id为0。然而,visual studio调试器和win32 API函数GetCurrentThreadId仍然能够正确识别线程id。
有人能解释一下吗?在应用程序关闭期间是否不支持boost::this_thread,可能是因为它在此时已经被销毁了?
你不应该在线程中调用exit
!这个函数退出整个进程,而不仅仅是线程。从线程函数返回。
exit(0)
强制所有线程退出。当您调用m_thread.get_id()
时,m_thread
引用的线程可能已经被破坏。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 通过 id 访问 QML 插槽中的嵌套对象(从 c++ 调用)
- 如何使用方法ID从Powerbuilder调用PBNI方法
- 从 WindowsAPI 调用 NetLocalGroupAddMembers 时,安全 ID 结构无效错误
- 获取每个类的 int Id,使用类名及其对象调用
- C++:父对象在不调用方法的情况下更改其id属性
- 通过指针对基函数进行限定 id 调用
- 从MFC中的DoDataExchange确定调用方ID
- 函数未在类型id中调用
- Boost::this_thread::get_id在exit()调用期间不返回id
- 通过IDispatch公开COM函数的服务器如何区分调用函数而不传递任何ID参数的不同客户端?
- 通过FindItem SOAP调用获取RecurringMaster ID