Subject: [boost] [Interprocess::Semaphore] Deadlock on more producers - one consumer problem
From: Cosimo Calabrese (cosimo.calabrese_at_[hidden])
Date: 2009-06-17 11:58:18
Hi to all,
I've a buffer of 10 positions; 4 producers put elements on the buffer
and only 1 consumer pick them; the producers generate elements very
fastly, but the consumer picks the element very slowly. I've decided to
synchronize the producers and the consumer with a boost::interprocess
semaphore initialized to 10.
Before to put an element in the buffer, the producer execute a wait() in
the semaphore; the 4 producers put quickly 10 elements in the buffer, so
they all block on the wait(). When the consumer picks an element from
the buffer, it performs a post() on the semaphore.
Here is the problem: the post() unlocks only one of the 4 producers.
Consider this scenario:
- the producers are all in wait state; the semaphore counter is 0;
- the consumer picks an element and perform a post(), that unlocked the
producer_1; the semaphore counter is 1;
- the producer_1 is unlocked but it doesn't produce any element; the
other producers are locked;
- the consumer picks the other 9 elements in the buffer and perform the
9 post() on the semaphore, but the semaphore counter becomes 2, 3, 4,
... 10, and so the semaphore doesn't signal the other producers, that
remains in lock state(). --> DEADLOCK.
Is there a way to unlock all the producers with the
boost::interprocess::semaphore? Or I have mistaken the approach of the
Thanks to all,
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk