且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在.NET中不同的线程之间共享列表

更新时间:2022-05-16 09:02:36

您肯定要执行或者通过的锁定或通过如ReaderWriterLockSlim.

You definitely have to implement thread-safe access either via lock or via e.g. ReaderWriterLockSlim.

变量本身(名单<>)是线程安全的,如果你不是第一次分配后修改它(通过新的),但元素访问必须是线程安全的(因为你说你添加/删除从列表中的元素,因此改变其状态)

Variable itself (List<>) is thread-safe if you not modifies it after first allocation (via new) but element access have to be thread-safe (since you say you adding/removing elements from list hence changing its state)

顺便说一句,如果线程修改了自己的项目,你为什么要分享?所以,你已经实现了?为什么组织这样?也许,如果你表现出的细节有更好的提醒。

BTW, if threads modifies its own items why you want to share them? So what you have implemented? Why you organize this way? Maybe there are better advise if you show details.