[concurrency-interest] DCL clarification Immutable singelton

Hans Boehm boehm at acm.org
Wed Apr 9 13:44:49 EDT 2014


This code is wrong as written, for a different reason, unless _instance is
volatile.  The two reads of _instance in getInstance() can be performed out
of order and getInstance() can return null if another thread initializes it
concurrently.  If you really need to do this, there should be exactly one
read of _instance outslde the critical section. If this code is not so
performance critical that you absolutely need to do this, don't, and use
the volatile declaration.  The next person changing your code may also miss
problems like this, and it may take years to track down the resulting
intermittent problem.

Hans


On Wed, Apr 9, 2014 at 6:49 AM, vikas <vikas.vksingh at gmail.com> wrote:

> Thanks guys very much for clarification.
>
>
>
> --
> View this message in context:
> http://jsr166-concurrency.10961.n7.nabble.com/DCL-clarification-Immutable-singelton-tp10862p10868.html
> Sent from the JSR166 Concurrency mailing list archive at Nabble.com.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140409/38c266b2/attachment.html>


More information about the Concurrency-interest mailing list