为list< T>。载有()是线程调用 - C#

为list< T>。载有()是线程调用 -  C#

问题描述:

我的理解是,如果你正在使用C#泛型列表(列表),它可以支持多个并发读取器,但只有一个作家。而当你引入一个作家到混合,你还必须提供同步结构,以使操作线程安全的。

My understanding is that if you are using a generic list (List) in C#, that it can support multiple concurrent readers but only one writer. And when you introduce a writer into the mix, you must also provide synchronization constructs to make the operations thread safe.

时List.Contains认为读操作?换句话说,如果我调用这个方法,我需要担心一个作家可以在同一时间被写入这个名单?

Is List.Contains considered a read operation? In other words, if I am calling this method, do I need to worry that a writer may be writing to this List at the same time?

是的,你应该。基本上我都会同步进行的任何的,如果可以用于在同一时间写列表操作。

Yes, you should. Basically I would synchronize for any operation if the list might be used for writing at the same time.

一般来说,我觉得收藏品分为两类 - 这是创建,初始化,然后就再也没有再次改变(线程安全的)的人,和突变随着时间的人(不是线程安全的,所有访问锁)

Generally I find collections fall into two categories - ones which are created, initialized and then never changed again (thread-safe), and ones which are mutated over time (not thread-safe, lock for all access).