枚举期间添加和删除ConcurrentBag的元素 [英] Adding and removing elements of a ConcurrentBag during enumeration

查看:49
本文介绍了枚举期间添加和删除ConcurrentBag的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个线程在另一个线程枚举这个包时添加或删除 ConcurrentBag< T> 的元素时会发生什么?新元素还会显示在枚举中,而删除的元素也不会显示吗?

What happens when a thread is adding or removing elements of a ConcurrentBag<T> while another thread is enumerating this bag? Will the new elements also show up in the enumeration and will the removed elements not show up?

推荐答案

一个可以阅读精美的手册以发现:

ConcurrentBag< T..GetEnumerator 方法

枚举表示袋子内容的即时快照.在调用GetEnumerator之后,它不反映对集合的任何更新.枚举器可以安全地与袋子进行读写操作.

The enumeration represents a moment-in-time snapshot of the contents of the bag. It does not reflect any updates to the collection after GetEnumerator was called. The enumerator is safe to use concurrently with reads from and writes to the bag.

强调我的.

这篇关于枚举期间添加和删除ConcurrentBag的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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