[concurrency-interest] fast semaphore

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Tue Apr 10 14:59:54 EDT 2007

On 07/04/07, Joseph Seigh <jseigh_cp00 at xemaps.com> wrote:

> main benefit
> is from being lock-free on the fast path.  No thread will block and be
> suspended
> waiting for another thread holding a lock.  Adding thread suspend/resume
> overhead
> for locking can add considerable delay.

I am afraid there is a misbelief about this in this lock-free fever.

There are two kinds of scheduling: short term and long term ones. Now,
suspend/resume is more appropriate for the latter and there is no
locking there. In case of a traditional critical section we can speak
of short term scheduling because the section is used by the threads
for a short time interval. Consequently, threads do not have to wait
for it indefinitely long. It depends on the JVM how it schedules for
the short term. On a single processor, it might mean that the
pre-emption is prevented inside the critical section. On a
multi-processor system, on the other hand, critical section can be
guarded by a spin lock or an adaptive version of the spin lock.

The lock-free version has some advantage in the case when there is no
simultaneous attempt of the threads to work on the "critical section."
In this case there is no overhead in the lock-free version whereas the
traditional synchronized version checks the lock anyway and that is an

On the other hand, the lock-free version has major disadvantage when
the critical section is heavily visited simultaneously. In this case
the lock-free version can turn into a busy waiting loop and considerably
loads the system degrading the overall performance.

So, it looks like the lock-free solution is a hand optimisation for
the case of not heavily interacting threads.

Best Regards,

More information about the Concurrency-interest mailing list