ReaderWriterLock vs lock{}

后端 未结 7 2180
死守一世寂寞
死守一世寂寞 2020-11-30 21:38

Please explain what are the main differences and when should I use what.
The focus on web multi-threaded applications.

7条回答
  •  无人及你
    2020-11-30 22:13

    Monitor and the underlying "syncblock" that can be associated with any reference object—the underlying mechanism under C#'s lock—support exclusive execution. Only one thread can ever have the lock. This is simple and efficient.

    ReaderWriterLock (or, in V3.5, the better ReaderWriterLockSlim) provide a more complex model. Avoid unless you know it will be more efficient (i.e. have performance measurements to support yourself).

    The best kind of locking is the locking you don't need (i.e. don't share data between threads).

提交回复
热议问题