Thread Saftey

Jan 7, 2011 at 3:47 PM

I ran across this project when evaluating IOC frameworks.  I like the claims of speed but thought I'd inspect the code to since I haven't heard much about the project to make sure it really seems stable.  I ran across something, I'm not seeing any thread synchronization or locking code to ensure that only one singleton is actually created when using the ContainerLifetime.  The way I'm seeing it you could easily have multiple instances created in a heavily multithreaded app (ie ASP.NET) though the earlier ones would only be used for a single call to resolve, once you have one set subsequent requests would use it.  Is that by design to avoid the performance hit that comes from thread locking with the thinking being it is pretty much a singleton or is this an oversight?

Just to be clear

public object CreateInstance(ContainerCaching containerCache)
            if (containerCache == ContainerCaching.InstanceCachedInContainer)
                if (Instance == null)
                    Instance = Factory(Container);
                return Instance;
                return Factory(Container);

If two threads hit the If (Instance == null) at the same time both may call the Factory and set Instance.  They may then return Instance at different points ie Thread A may go first and end up with Instance A while thread B goes second and ends up with Instance B.  Later Thread C would end up with Instance B.

At least that is how I'm seeing it.  Am I missing something?


Bryan Call

Feb 13, 2011 at 7:51 PM

Thank you for pointing this out.  I have gone throught the code and made it thread safe (I hope) :)

Check the latest source.  I am working on some documentation before a release.