dimanche 22 février 2015

Multiple threads waiting on the same event handle in C++


Summary


If multiple threads are concurrently waiting on the same event handle, as in:



WaitForSingleObject(theHandle, INFINITE);


and the event is initialized to be manual-reset, as in:



// manual-reset and initial-state set to true
theHandle = CreateEvent(nullptr, true, true, nullptr);


then once the event is set, will all threads resume?


A little more detail


This example seems to indicate that they would all resume, without actually coming out and saying either way:


http://ift.tt/1B8necF


In my particular case, I have a small block of code where the event is manually reset, and in short order set again. i.e.:



ResetEvent(theHandle)

// Do some things that should not lock at all let alone deadlock

SetEvent(theHandle)


Then in practice I observe that some of the calls to wait for the event to be set will wait there indefinitely:



WaitForSingleObject(theHandle, INFINITE); // Hello, deadlock


The only things I can think of are that events don't work the way I think they should, hence the question, or that my "safe, lockless" code referred to above, isn't actually so safe and lockless.




Aucun commentaire:

Enregistrer un commentaire