msdn: What is “Thread Safety”?

前端 未结 4 1058
太阳男子
太阳男子 2021-02-14 11:31

In many MSDN documents, this is written under the Thread Safety heading;

\"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instanc

4条回答
  •  臣服心动
    2021-02-14 12:08

    An object being "thread safe" means that if two threads are using it at (or very near, on single-CPU systems) the exact same time, there's no chance of it being corrupted by said access. That's usually achieved by acquiring and releasing locks, which can cause bottlenecks, so "thread safe" can also mean "slow" if it's done when it doesn't need to be.

    Public static members are pretty much expected to be shared between threads (Note, VB even calls it "Shared"), so public statics are generally made in such a way that they can be used safely.

    Instance members aren't usually thread-safe, because in the general case it'd slow things down. If you have an object you want to share between threads, therefore, you'll need to do your own synchronization/locking.

提交回复
热议问题