What does a "Thread Safety" class mean?

Asked

Viewed 1,307 times

6

According to the MSDN to Webapp Class (Microsoft.Owin.Hosting) is Thread Safety. What exactly does that mean?

This class specifically has a method Start that:

Start a web app....

Every request to that app would be manipulated by a Thread?

2 answers

4


The article does not say that class is "thread safety", this is a section title that says:

Any pubic static member of this class is "thread safe", that is, it can be manipulated by several tasks simultaneously.

There is also information that any instance of the class (created objects of this class) is not guaranteed to be thread safe. Class instances should not be used by multiple tasks simultaneously.

In this specific case, it means that several tasks can use the method implementations Start simultaneously, each call to the method does not cause problems in the other calls.

  • 1

    There is also information that the class is static, so there are no instances of it.

  • Well noted, the class statement defines it as static, so it is not possible to create instances of it. What’s more: in the statement in C++ there is still the flag sealed, which prevents it from being used as an ancestral class of other classes, that is, neither by making another class and inheriting it from it! I don’t remember C#anymore, but the C# statement doesn’t have this flag.

  • Because static classes are automatically sealed. And remember that that is C++/CLI, there is no pure C++ attribute.

2

It means that static members of this class can operate smoothly through multiple threads. They don’t have anything that can cause problems when running concurrently or if they have a situation that can cause problems, they already have a mechanism that doesn’t let any problems occur. So you can access these members concurrently without fear.

But the members of the bodies are not guaranteed as thread safe. What does not matter in this case, since the class is static.

If it were an unstable class you could still use the instances concurrently if you provide mechanisms that ensure the proper functioning concurrently. Or you can use parallel but not concurrent.

Of course programming using threads it’s not something simple, you still need to know what you’re doing even though everything in class is thread safe.

Static classes are not used more than once in the application. Although nothing prevents it from being done, this does not seem to be the case, it seems to be something that should be unique in the application. Therefore, in general, it is not used with threads. At least it’s my understanding reading the little documentation. Take the example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.