简体   繁体   中英

.NET Dictionary: is only enumerating thread safe?

Is simply enumerating a .NET Dictionary from multiple threads safe?

No modification of the Dictionary takes place at all.

是的,在这种情况下,枚举是线程安全的,因为在foreach循环开始时,枚举器的新实例是通过对GetEnumerator的隐式调用创建的。

Yes ... Well almost:

A Dictionary<(Of <(TKey, TValue>)>) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

( ref )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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