Best C# solution for multithreaded threadsafe read/write locking?

前端 未结 8 818
无人共我
无人共我 2021-01-01 05:08

What is the safest (and shortest) way do lock read/write access to static members in a multithreaded environment in C#?

Is it possible to do the thread

8条回答
  •  一向
    一向 (楼主)
    2021-01-01 06:09

    You should lock/unlock on each static member access, within the static accessor, as needed.

    Keep a private object to use for locking, and lock as required. This keeps the locking as fine-grained as possible, which is very important. It also keeps the locking internal to the static class members. If you locked at the class level, your callers would become responsible for the locking, which would hurt usability.

提交回复
热议问题