在使用std :: future的实现某种线程池时,我遇到了一个奇怪的问题。
我有宣布为类成员的执行者名单:
std::vector<std::future<void>> _executers;
我这样初始化:
for (auto& executer : _executers)
{
executer = std::async(std::launch::async, [this]()
{
try
{
for (;;)
{
if (_is_stopping.load())
{
return;
}
// some long running routine
}
}
catch (std::exception& ex)
{
}
catch (...)
{
}
});
}
在dtor类中,我只是调用:
_is_stopping.store(true);
应该停止for(;;)循环并让以后的例程返回。
但是不幸的是,在调用类dtor并尝试清理std :: vector时,我陷入了困境。回溯:
ntdll.dll!NtWaitForAlertByThreadId() + 20 bytes Unknown
ntdll.dll!RtlSleepConditionVariableSRW() + 250 bytes Unknown
KernelBase.dll!SleepConditionVariableSRW() + 50 bytes Unknown
msvcp140.dll!std::_Syserror_map() + 552 bytes Unknown
msvcp140.dll!_Cnd_timedwait() + 84 bytes Unknown
DBase.dll!std::_Associated_state<int>::_Wait() Line 306 C++
DBase.dll!std::_Task_async_state<void,0>::~_Task_async_state<void,0>() Line 914 C++
DBase.dll!std::_Task_async_state<void,0>::`scalar deleting destructor'(unsigned int) C++
DBase.dll!std::_Destroy_range<std::allocator<std::future<void> >,std::future<void> * __ptr64>(std::future<void> * _First, std::future<void> * _Last, std::_Wrap_alloc<std::allocator<std::future<void> > > & _Al) Line 1118 C++>
DBase.dll!DSplunkMsgDevice::Impl::~Impl() Line 191 C++
DBase.dll!std::_Ref_count<DSplunkMsgDevice>::_Destroy() Line 153 C++
DBase.dll!boost::fusion::vector_data4<...>() C++
ucrtbase.dll!qsort_s() + 1267 bytes Unknown
ucrtbase.dll!_register_onexit_function() + 231 bytes Unknown
ucrtbase.dll!_execute_onexit_table() + 62 bytes Unknown
DBase.dll!dllmain_crt_process_detach(const bool is_terminating) Line 109 C++
DBase.dll!dllmain_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 207 C++
ntdll.dll!RtlDeactivateActivationContextUnsafeFast() + 435 bytes Unknown
ntdll.dll!LdrShutdownProcess() + 293 bytes Unknown
ntdll.dll!RtlExitUserProcess() + 180 bytes Unknown
kernel32.dll!ExitProcess() + 10 bytes Unknown
ucrtbase.dll!exit() + 244 bytes Unknown
ucrtbase.dll!exit() + 123 bytes Unknown
DIAControlService.exe!__scrt_common_main_seh() Line 262 C++
kernel32.dll!BaseThreadInitThunk() + 20 bytes Unknown
ntdll.dll!RtlUserThreadStart() + 33 bytes Unknown
编译器/工具集:VS2015,14.0.25431.01更新3
据我了解,std :: future lamdba应该在_is_loading.store(true)
将std :: future的状态更改为std :: future_status :: ready之后退出。因此,应该返回由std :: future dtor调用的wait()调用。但这不会发生。
此外,我不明白为什么只有1个线程(主线程)在运行,而std :: future在等待某些东西。
我真的很抱歉,但是我不能给出更多代码。如果有人能提示我std :: future的工作原理,我将不胜感激。