SingletonStrategy Thread safety

Topics: CodePlexContainer, ObjectBuilder
Sep 14, 2007 at 2:54 PM
I saw the implementation of what looks to me like a Thread-Safe SingletonStrategy in the CodePlexContainer's new ObjectBuilder

However, from what I see, I'm not sure it's thread-safe because the lock is not kept until after the item is added to the locator. Was the intent to be thread-safe?


1. Thread 1 enters the strategy, No singleton found, Builds up a new instance
2. Thread 2 enters the strategy, Time-slices Thread1 before T1 has time to add the singleton to the locator, No singleton found, Builds up an instance as well
3. Both T1 and T2 will try to add their own instance to the locator afterwards.

I think the lock should be kept longer if singleton was found.
Sep 15, 2007 at 1:26 PM
You're right, that locking should either be removed or extended to cover the whole build-up cycle. I tend to lean towards the former, personally, and let containers decide how to deal with build serialization.
Sep 15, 2007 at 11:57 PM
I checked in a fix for this in the ObjectBuilder project in the CodePlex Container sample.