[concurrency-interest] CAS and synchronized

David Holmes davidcholmes at aapt.net.au
Thu Dec 8 19:49:58 EST 2011


Mohan,

>           I have read that CAS uses the processor's instruction set to
> update memory values. Why is this more efficient than using
> synchronized ? What is the locking overhead when we use synchronized ?

In simplified terms ...

Acquiring a monitor requires a CAS internally. To that you add all the
overhead pertaining to blocking, queuing, setting owner fields and recursion
counts etc. In the fastest case, with an uncontended lock, this can reduce
to little more than the CAS itself. But you still have to release the lock,
which in a good implementation does not require a CAS, but still involves
some computation. Once you introduce low contention then locks introduce
blocking and context-switching. As contention increases it can be better to
block threads rather than have a lot of retries in a CAS-loop.

HTH

David Holmes

>
> Seems it is a basic doubt again.
>
> Thanks,
> Mohan
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list