msdn: What is “Thread Safety”?

前端 未结 4 1057
太阳男子
太阳男子 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 11:55

    To understand this, consider the following example. In MSDN description of .net class HashSet, there is a part that says about the thread safety. In the case of HashSet Class, MSDN says “Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.” Of cause we all know the concept of race conditions and deadlocks, but what does Microsoft wants to say in simple English? If two threads add two values to an “instance” of a HashSet there are some situation where we can get its count as one. Of cause in this situation the HashSet object is corrupted since we now have two objects in the HashSet, yet its count shows only one. However, public static version of the HashSet will never face such a corruption even if two threads concurrently add values.

提交回复
热议问题