are static classes shared among different threads in C#

前端 未结 5 1625
野性不改
野性不改 2021-02-14 11:44

I need to share a value between threads without exceeding it\'s boundary. Does a static variable do this?

5条回答
  •  终归单人心
    2021-02-14 12:04

    Static variables are shared across multiple threads within an AppDomain. All threads will see, and act, upon the same instance of a static variable. As such, if you're using static, you will likely want to use some form of synchronization to protect the access of that variable.

    If you want to have a thread-local variable, the ThreadLocal class makes this easy. It provides a means of generating and using data that is unique per thread.

提交回复
热议问题