在多线程.NET应用程序中,
假设第一个线程正在写入列表
第二个线程正在清除其中的所有项目。
第三线程正在从列表中读取。
如果第二个线程和第三个线程在CLR级别“真正”同时访问同一列表对象,会发生什么情况。我不是说.NET同步对象和锁定机制。
我的意思是,当CLR从引用(通过第3线程)访问列表项时,如果引用所指向的列表发生变化(通过第2线程)会怎样?
最佳答案
不好的东西。
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
A
List<T>
can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
关于.net - .NET CLR-对象引用同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7123249/