简体   繁体   中英

Is CreateChildContainer() Thread-Safe?

Back in 2009, which may have been pre-Unity 2.0, I see a thread of discussion which suggests CreateChildContainer() is not thread safe:

Also, if you create child container with CreateChildContainer() method, they will add themself to its parent container's livetime container during creation and remove when disposing. So, disposing child containers from separate threads may lead to errors, even each child container accessed only by one thread at time.

Is that indeed (still) true for Unity 2.0? What about Unity 3.0 or beyond?

This is of particular interest if implementing Unity with WCF, as described in this article . The idea is to use a child-container to easily dispose of HierarchylifetimeManager instances. Though I'm wondering now if instead a PerRequestlifetimemanager should be used with no child-containers at all.

Based on the latest source code (Unity 3.5). It should be thread safe. It adds its self to parent's lifetime container wiht parent.lifetimeContainer.Add(this); annd the implementation of Add uses lock() around the modficiations of its private list of children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM