[concurrency-interest] Enforcing ordered execution of critical sections?

Doug Lea dl at cs.oswego.edu
Mon Dec 22 12:11:22 EST 2014


On 12/22/2014 11:46 AM, Aleksey Shipilev wrote:
> On 12/22/2014 05:36 PM, Doug Lea wrote:
>> On 12/21/2014 07:25 PM, Joe Bowbeer wrote:
>>> Below is the most concise implementation I can imagine, using a single
>>> Semaphore, which is legal AFAICT according to the javadoc, but which
>>> deadlocks
>>> in my tests.

>> The acquire(n) spec should probably be clarified that it promises
>> no more than the equivalent of
>>    for (int i = 0; i < n; ++i) acquire();

Sorry, this was too extreme a way of saying it.

>
> Wait, what. If fairness only applies to individual acquire() calls, that
> definition would mean that even with fair Semaphore two threads can
> deadlock in:

Suppose you have thread 1 acquire(7), and then thread 2: acquire(5).
With fairness, thread 2 waits for thread 1 to succeed before
trying. Without fairness, they could be in any order, but still
*some* order. So the net effect is not quite the same as
the above loop, but closer than implying that the semaphore
somehow sorts waiters by #permits, which it doesn't/can't.

-Doug



More information about the Concurrency-interest mailing list