简体   繁体   English

CreateChildContainer()是线程安全的吗?

[英]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: 早在2009年(可能是Unity 2.0之前的版本),我看到了一个讨论线索,线索表明CreateChildContainer()不是线程安全的:

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. 另外,如果使用CreateChildContainer()方法创建子容器,则它们将在创建过程中将其自身添加到其父容器的实时容器中,并在处置时将其删除。 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? 对于Unity 2.0确实(仍然)正确吗? What about Unity 3.0 or beyond? 那么Unity 3.0或更高版本呢?

This is of particular interest if implementing Unity with WCF, as described in this article . 本文所述,如果使用WCF实现Unity,这将特别有意义。 The idea is to use a child-container to easily dispose of HierarchylifetimeManager instances. 这个想法是使用一个子容器来轻松处置HierarchylifetimeManager实例。 Though I'm wondering now if instead a PerRequestlifetimemanager should be used with no child-containers at all. 尽管我现在想知道是否应该使用完全没有子容器的PerRequestlifetimemanager。

Based on the latest source code (Unity 3.5). 基于最新的源代码(Unity 3.5)。 It should be thread safe. 它应该是线程安全的。 It adds its self to parent's lifetime container wiht parent.lifetimeContainer.Add(this); 它通过parent.lifetimeContainer.Add(this);将其自身添加到父级的生命周期容器中parent.lifetimeContainer.Add(this); annd the implementation of Add uses lock() around the modficiations of its private list of children. annd执行Add使用lock()围绕其私人儿童名单的modficiations。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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