c# - is "Double-Checked Locking is Broken" a java-only thing? -


the page @ http://www.cs.umd.edu/~pugh/java/memorymodel/doublecheckedlocking.html says double-checked locking flawed in java. i'm wondering apply other languages (c#, vb, c++, etc)

i've read double checked locking pattern: broken or not?, is broken double checked locking?, how solve "double-checked locking broken" declaration in java? truthful don't know common consensus is. yes broken others no.

anyway, question apply other languages (c#, vb, c++, etc)

double checked locking safe in java, provided that:

  1. the instance variable declared volatile, and
  2. the jvm correctly implements jsr-133 specification; i.e. compliant java 5 , later.

my source jsr-133 (java memory model) faq - jeremy manson , brian goetz, february 2004. confirmed goetz in number of other places.

however, goetz says, idiom time has passed. uncontended synchronization in java fast, recommends declare getinstance() method synchronized if need lazy initialization. (and imagine applies other languages ...)

besides, things being equal, bad idea write code works in java 5 unreliable in older jvms.


ok, other languages? well, depends on how idiom implemented, , on platform.

  • c# - according https://stackoverflow.com/a/1964832/139985, platform dependent whether instance variable needs volatile. however, wikipedia says if use volatile or explicit memory barriers, idiom can implemented safely.

  • vb - according wikipedia idiom can implemented safely using explicit memory barriers.

  • c++ - according wikipedia idiom can implemented safely using volatile in visual c++ 2005. other sources in general c++ language specification doesn't provide sufficient guarantees volatile sure. double-checked locking can implemented in context of c++ 2011 language revision - https://stackoverflow.com/a/6099828/139985.

(note: i'm summarizing sources found seem me recent ... , sound. i'm not c++, c# or vb expert. please read linked pages , make own judgements.)


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -