By mswatcher
via codeproject.com
Published: Jul 30 2010 / 02:35
Everything there is to know about Singletons and thread safety.
![]() | |
| BIRT 3.7 | |
| Written by: Michael Williams | |
| Featured Refcardz: Top Refcardz: | |
| 150+ Refcardz Available · Get them all | |
By mswatcher
via codeproject.com
Published: Jul 30 2010 / 02:35
Comments
iirekm replied ago:
One thing you probably forgot: add 'volatile' keyword to the variable that holds singleton. Otherwise some compilers could cache the value in a CPU registry and you may get unpredictable results.
The same 'double-checking' pattern works also in Java (since 1.5).
The problem with singleton is that if you have lots of singleton objects, this leads to lots of ugly boilerplate code. Better use a dependency injection framework, like Spring in Java.
Voters For This Link (9)
Voters Against This Link (1)