小编use*_*470的帖子

更新对不可变对象的引用的首选方法是什么?

如果我们有一个像ImmutableList()这样的不可变对象.在多线程环境中使用此对象的首选方法是什么?

例如

public class MutableListOfObjects()
{
   private volatile ImmutableList objList;

   public class MutableListOfObjects()
   {
       objList = new ImmutableList();
   }

   void Add(object o)
   {
      // Adding a new object to a list will create a new list to ensure immutability of lists.

      // Is declaring the object as volatile enough or do we want to
      // use other threading concepts?
      objList = objList.Add(o);
   }

   // Will objList always use that lest version of the list
   bool Exist(object o)
   {
      return objList.Exist(o); …
Run Code Online (Sandbox Code Playgroud)

c# multithreading synchronization immutability

3
推荐指数
1
解决办法
459
查看次数