Boost logo

Boost Users :

From: David A. Greene (greened_at_[hidden])
Date: 2006-02-09 20:27:10


Andrew Smith wrote:
> I haven't looked at the code very closely, but it looks like there's a
> potential for deadlock in the Consumer's wait_for_data and push
> methods. Here is the scenario (in hypothetical deadlock time order.)
>
> 1. consumer thread in wait_for_data(): acquires full_mutex, calls empty()
> 2. producer thread in push(): called by Producer, acquires mutex
> 3. consumer thread in wait_for_data()'s call to empty(): attempts to
> acquire mutex, blocks on producer thread
> 4. producer thread in push(): attempts to acquire full_mutex, blocks on
> consumer thread
> 5. neither thread can advance

Oops! Yep you are correct. I believe that moving the acquire of
full_mutex outside the inner scope fixes the problem because the
producer doesn't hold two mutexes at the same time.

> As a general rule, interleaved acquistions like this of two or more
> resources is fraught with peril. If you want an unbounded queue (as it
> appears in your code) it can be done with a single mutex and condition
> variable.

Yes, I see that now thanks to Delfin. The real application is trickier
but I think I can apply the same principle. This is definitely better
than what I had.

                                  -Dave


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