[thread/condition_variable] crashes in condition variable

Hi all! I have a problem with the condition variable class. I wrote a sync object class (something which two threads have a reference to, the one is waiting on in in wait() the other will signal it with signal(). This is some pseudo code which illustrates my problem: function doSomething() { // the sync object is to keep this thread // waiting until the other one is done CPGSUSyncObject cSyncObject; // give a pointer of the sync object to another thread ... // at one time in the future the other thread will call // its signal routine which calls condition_variable::notify_all // wait until the other one has done its tasks cSyncObject.wait(); } My problem (as far as I can see it) is the following: condition_variable::notify_all() calls wake_waiters() but does further tasks after this call (I assume that wake_waiters() is doing the real signalling by the os?). But if I have the task switch fast enough, it may happen that my doSomething() method runs out of scope deleting cSyncObject which is itself deleting the condition_variable. So the vector iteration code in condition_variable::notify_all() crashes because the object is already destroyed... Any help or good hints are appreciated... Regards Dietmar

On 7/21/2010 3:45 AM, Dietmar Hummel wrote:
condition_variable::notify_all() calls wake_waiters() but does further tasks after this call (I assume that wake_waiters() is doing the real signalling by the os?). But if I have the task switch fast enough, it may happen that my doSomething() method runs out of scope deleting cSyncObject which is itself deleting the condition_variable. So the vector iteration code in condition_variable::notify_all() crashes because the object is already destroyed...
Any help or good hints are appreciated...
Yup, I've gotten burned by exactly that before. It was a bitch to find. You can either 1) Set up your sync object outside both functions. 2) Used shared_ptr to pass a pointer to the sync object to the second function. When I first discovered the problem, I will warn you: I spent a couple of hours trying to engineer a solution in which case (2) worked. It's doable, but things got messy. Just do (1) in your setup code.

On 7/21/2010 3:45 AM, Dietmar Hummel wrote:
condition_variable::notify_all() calls wake_waiters() but does further tasks after this call (I assume that wake_waiters() is doing the real signalling by the os?). But if I have the task switch fast enough, it may happen that my doSomething() method runs out of scope deleting cSyncObject which is itself deleting the condition_variable. So the vector iteration code in condition_variable::notify_all() crashes because the object is already destroyed...
Any help or good hints are appreciated...
There's also a choice (3) which I forgot: In your second thread, if you call "signal" while the mutex is locked, this problem will go away. There are tons of articles around on whether or not you should do this (having to do with context switching and performance), but it will solve your problem.
participants (2)
-
Dietmar Hummel
-
Eric J. Holtman