SingletonStrategy Thread safety

Topics: CodePlexContainer, ObjectBuilder
Sep 14, 2007 at 1: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?

Scenario:

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.
Coordinator
Sep 15, 2007 at 12: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.
Coordinator
Sep 15, 2007 at 10:57 PM
I checked in a fix for this in the ObjectBuilder project in the CodePlex Container sample.

http://www.codeplex.com/ObjectBuilder/SourceControl/ListDownloadableCommits.aspx
http://www.codeplex.com/ObjectBuilder/SourceControl/DirectoryView.aspx?SourcePath=&changeSetId=26889