简体   繁体   English

C#复杂的线程同步

[英]C# complex thread synchronization

I have quite complex threads synchronization case. 我有相当复杂的线程同步情况。 I have a collection which is accessed from different threads. 我有一个从不同的线程访问的集合。 One thread, let's name it 'Control thread', updates a collection. 一个线程,我们将其命名为“控制线程”,更新集合。 Other threads read the collection ('Reading threads'). 其他线程读取集合('读取线程')。 The logic I want to implement is the following: 我想要实现的逻辑如下:

  1. Reading threads can read the collection simultaneously. 读取线程可以同时读取集合。
  2. When Control thread need to update collection: 当Control线程需要更新集合时:
    • Control thread should not start the update until all reading threads that are currently working with collection complete their work. 在所有正在使用集合的读取线程完成其工作之前,控制线程不应启动更新。
    • But at the time the Control thread is waiting for finishing reading threads, other reading threads that have not started to work with collection yet, should not start - they should wait until Control thread update the collection. 但是当Control线程正在等待读取线程时,其他尚未开始使用集合的读取线程不应该启动 - 它们应该等到Control thread更新集合。

What threads synchronization technique should I use? 我应该使用什么线程同步技术?

just take a look at ReaderWriterLock Class which defines a lock that supports single writers and multiple readers or you can use ReaderWriterLockSlim (supported in .Net 4.0) which is more convenient if you are starting a new development(for example it avoids many cases of potential deadlock) 只需看看ReaderWriterLock类,它定义了一个支持单个编写器和多个读取器的锁,或者你可以使用ReaderWriterLockSlim (在.Net 4.0中支持),如果你正在开始一个新的开发,它会更方便(例如它避免了很多潜在的情况)僵局)

from MSDN : 来自MSDN

ReaderWriterLockSlim is similar to ReaderWriterLock, but it has simplified rules for recursion and for upgrading and downgrading lock state. ReaderWriterLockSlim类似于ReaderWriterLock,但它简化了递归规则以及升级和降级锁定状态的规则。 ReaderWriterLockSlim avoids many cases of potential deadlock. ReaderWriterLockSlim避免了许多潜在的死锁案例。 In addition, the performance of ReaderWriterLockSlim is significantly better than ReaderWriterLock. 此外,ReaderWriterLockSlim的性能明显优于ReaderWriterLock。 ReaderWriterLockSlim is recommended for all new development.* ReaderWriterLockSlim推荐用于所有新开发。*

Use the ReaderWriterLockSlim Class: 使用ReaderWriterLockSlim类:

Represents a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing. 表示用于管理对资源的访问的锁,允许多个线程进行读取或独占访问以进行写入。

Use ReaderWriterLockSlim to protect a resource that is read by multiple threads and written to by one thread at a time. 使用ReaderWriterLockSlim来保护由多个线程读取并由一个线程一次写入的资源。 ReaderWriterLockSlim allows multiple threads to be in read mode, allows one thread to be in write mode with exclusive ownership of the lock, and allows one thread that has read access to be in upgradeable read mode, from which the thread can upgrade to write mode without having to relinquish its read access to the resource. ReaderWriterLockSlim允许多个线程处于读取模式,允许一个线程处于具有锁独占所有权的写模式,并允许一个具有读访问权限的线程处于可升级读模式,线程可以从该模式升级到写模式必须放弃对资源的读访问权。

Joe Albahari's Threading in C# is a great resource. Joe Albahari 在C#中线程是一个很好的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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