W
William Stacey [MVP]
My bad Bruce. The second thread will indeed create another instance as you
said. However, only the first will be used and the other will be GCd as you
guys said. The pattern still works as long as it does not matter if a
second instance is created and not used.
--
William Stacey [MVP]
| How very odd. That means that CompareExchange is not a method call,
| then. It's something else?
|
| If it's just a method like any other method, then according to the
| documentation at
|
| http://msdn2.microsoft.com/en-us/library/h7etff8w.aspx
|
| "The compare and exchange operations are performed as an atomic
| operation." Fair enough. That makes sense. However, the "new
| Singleton()" occurs _before_ the call to CompareExchange. That
| expression must be evaluated in order to make the call in the first
| place. Once inside the method, CompareExchange makes certain guarantees
| about the conditions under which the _instance variable will be set,
| but I see no such guarantee about the evaluation of method arguments
| before the method is invoked, nor do I understand how it would be
| possible for a method to make those sorts of guarantees.
|
| Then again, what I don't know about threading and locking would fill a
| thick book.
|
said. However, only the first will be used and the other will be GCd as you
guys said. The pattern still works as long as it does not matter if a
second instance is created and not used.
--
William Stacey [MVP]
| How very odd. That means that CompareExchange is not a method call,
| then. It's something else?
|
| If it's just a method like any other method, then according to the
| documentation at
|
| http://msdn2.microsoft.com/en-us/library/h7etff8w.aspx
|
| "The compare and exchange operations are performed as an atomic
| operation." Fair enough. That makes sense. However, the "new
| Singleton()" occurs _before_ the call to CompareExchange. That
| expression must be evaluated in order to make the call in the first
| place. Once inside the method, CompareExchange makes certain guarantees
| about the conditions under which the _instance variable will be set,
| but I see no such guarantee about the evaluation of method arguments
| before the method is invoked, nor do I understand how it would be
| possible for a method to make those sorts of guarantees.
|
| Then again, what I don't know about threading and locking would fill a
| thick book.
|