I use a std::mutex in my multi-threaded application for accessing the same resource by multiple threads. This works fine. But at some point of my code, I have to terminate the threads using TerminateThread(...). When I begin the thread again (using _beginthreadex), I get an exception if one of the threads was at the mutex lock when it was terminated:
Concurrency::improper_lock bei Speicherort 0x03B4F3D0.
This is thrown in rtlocks.cpp, line 1184 (in bool critical_section::_Acquire_lock):
LockQueueNode * pPrevious = reinterpret_cast<LockQueueNode *>(InterlockedExchangePointer(&_M_pTail, pNewNode));
I guess, if I could "reset" the mutex, I would get an exception, right? How can I do this?
(I use <process.h>)
edit
I tried the following:
std::mutex dataStorage_lock;
mutexHandler[0] = &dataStorage_lock; //storing all mutexs in an array
//when trying to delete:
delete mutexHandler[0];
But I get:
Debug Assertion Failed!
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
this is pseudocode, but you ought to get the idea:
wrong:
function thread_proc():
for_ever:
do_things
function stop_my_thread():
TerminateThread(...)
right:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With