使用2个不同线程的列表?

Pri*_*rix 3 c# multithreading locking list

我有一个列表,其中可以更新条目,从2个不同的线程插入或删除新数据.

是否可以使用公共只读对象锁定何时用于与其他线程进行交互,以确定它何时被锁定或者在两个线程中使用此列表的正确方法是什么?

Ric*_*der 5

lock在访问不同线程上的列表时,应始终使用a .

 public class Sample
 {
     object synch = new object();
     List<Something> list = new List<Something>();

     void Add(Something something)
     {
        lock (synch) { list.Add(something); }
     }

     // Add the methods for update and delete.
  }
Run Code Online (Sandbox Code Playgroud)