I came across this article discussing why the double-check locking paradigm is broken in Java. Is the paradigm valid for .NET (in particular, C#), if variables are declared volatile
?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- How to let a thread communicate with another activ
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
The singleton using boolean does not work. The order of operations as seen between different threads is not guaranteed unless you go through a memory barrier. In other words, as seen from a second thread,
created = true
may be executed beforeinstance= new Singleton();
Implementing the Singleton Pattern in C# talks about this problem in the third version.
It says:
The author seems to imply that double locking is less likely to work than other strategies and thus should not be used.