我std::mutex
在多线程应用程序中使用A来通过多个线程访问相同的资源。这很好。但是在代码的某些时候,我必须使用终止线程TerminateThread(...)
。当我再次使用(_beginthreadex
)开始线程时,如果其中一个线程在终止时处于互斥锁,则会出现异常:
并发::在位置0x03B4F3D0处的不正确锁定。
这在rtlocks.cpp中的第1184行(在中bool critical_section::_Acquire_lock
)中引发:
LockQueueNode * pPrevious = reinterpret_cast<LockQueueNode *>(InterlockedExchangePointer(&_M_pTail, pNewNode));
我想,如果我可以“重置”互斥锁,我会得到一个例外,对吗?我怎样才能做到这一点?
(我用<process.h>
)
编辑
我尝试了以下方法:
std::mutex dataStorage_lock;
mutexHandler[0] = &dataStorage_lock; //storing all mutexs in an array
//when trying to delete:
delete mutexHandler[0];
但我得到:
调试断言失败!
表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)
这是伪代码,但是您应该了解一下:
错误的:
function thread_proc():
for_ever:
do_things
function stop_my_thread():
TerminateThread(...)
正确的:
function thread_proc():
while(not terminate_signal):
do_things
function stop_my_thread():
terminate_signal.set(); // signals the thread to terminate
thread.join(); // waits for the thread to terminate
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句