You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you try and read a container from multiple threads, even if you've called .synchronize(), you will run into a dead lock issue. The following code is an example:
letcontainer=Container()
_ = container.register(Foo.self){ _ inThread.sleep(forTimeInterval: 100.0)returnFoo()}.inObjectScope(.container)
_ = container.register(Bar.self){ _ inreturnBar()}.inObjectScope(.container)DispatchQueue.global().async{
_ = container.synchronize().resolve(Foo.self)}// Allow the dispatch to happenThread.sleep(forTimeInterval: 0.5)// This will block for 100 seconds
_ = container.synchronize().resolve(Bar.self)
The problem stems from the use of NSRecursiveLock. This lock allows multiple read acquisitions on the same thread, but will block across different threads.
The text was updated successfully, but these errors were encountered:
dalemyers
changed the title
Synchronized containers are prone to deadlock when reading from multiple threads
Synchronized containers deadlock when reading from multiple threads
Mar 5, 2024
If you try and read a container from multiple threads, even if you've called
.synchronize()
, you will run into a dead lock issue. The following code is an example:The problem stems from the use of
NSRecursiveLock
. This lock allows multiple read acquisitions on the same thread, but will block across different threads.The text was updated successfully, but these errors were encountered: