hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.14k stars 188 forks source link

Make registrations thread-safe #73

Closed jeffery812 closed 3 years ago

jeffery812 commented 4 years ago

The PR would fix the issue #70

hmlongco commented 3 years ago

See discussion on threading in documentation: https://github.com/hmlongco/Resolver/blob/master/Documentation/Threads.md

I'm not sure I want to add a performance hit on all accesses to registrations. We can discuss.

hmlongco commented 3 years ago

This prevents race conditions on the registrations variable, but doesn't really help with thread safety insofar as ensuring multiple registrations aren't occurring simultaneously or that you're not attempting to register or resolve on a registration that's currently in the process of modifying a given registration container. It really just kicks the can a little further down the road.

I'm looking at adding a mutex on the registration lookup and add functions which in theory should better manage access to those variables. (See develop branch.) That said, if your code is structured such that you were getting race conditions between registrations and resolutions then you've got another potential problem as it would be possible to get into a state where you're attempting to resolve a registration that hadn't yet occurred.

hmlongco commented 3 years ago

The locking strategies and mechanisms have been completely overhauled in Resolver version 1.4. See the develop branch.