|
Boost Users : |
Subject: Re: [Boost-users] [thread/condition_variable] crashes in condition variable
From: Eric J. Holtman (eric_at_[hidden])
Date: 2010-07-21 07:24:58
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.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net