为list< T>。载有()是线程调用 - C# [英] Is List<T>.Contains() a Threadsafe call - C#

查看:165
本文介绍了为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).

这篇关于为list< T>。载有()是线程调用 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆