Is CreateChildContainer() Thread-Safe?

扶醉桌前 提交于 2019-12-24 09:38:31

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/19687750/is-createchildcontainer-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!