In MSDN some .NET classes described like this:
"This type is thread safe."
or
"Public static (Shared in Visual Basic) members of this type are thread safe. Instance members are not guaranteed to be thread-safe.".
My question is which features make a class to be thread-safe?
Is there any standard, recommendation or guidelines for thread-safety programming?
When I use lock(C#) keyword, it means my class is thread-safe or not?
How to I evaluate thread-safety of a class? Is there any TESTS to be sure that a class is 100% thread safe?
Example:
public class MyClass
{
public void Method()
{
lock (this)
{
// Now, is my class 100% thread-safe like Microsoft classes?
开发者_如何转开发 }
}
type m_member1;
type m_member2;
}
thanks
Is there any standard, recommendation or guidelines for thread-safety programming?
The most important standard is to ensure that all static members are thread-safe. You will see that all well written APIs including the .NET base class library makes this guarantee across the board. There is a really good reason for this. Since static members are shared across an AppDomain they could be used by many different threads without you even realizing it. It would be awkward at best to provide your own synchronization for every single static member access. Imagine what it would be like if Console.WriteLine
were not thread-safe.
As far as recommendations and guidelines there are plenty of well established patterns for doing concurrent programming. The patterns that are out there cover a wide variety of programming problems and use many different synchronization mechanisms. The producer-consumer pattern is one of many well known patterns which happens to solve a large percentage of concurrent programming problems.
Read Threading in C# by Joseph Albahari. It is one of the best and most vetted resources available.
When I use lock(C#) keyword, it means my class is thread-safe or not?
Nope! There is no magic bullet that can make a class thread-safe. The lock
keyword is but one of many different tools that can be used to make a class safe for simultaneous access by multiple threads. But, just using a lock
will not guarantee anything. It is the correct use of synchronization mechanisms that makes code thread-safe. There are plenty ways to use these mechanisms incorrectly.
How to I evaluate thread-safety of a class? Is there any TESTS to be sure that a class is 100% thread safe?
This is the million dollar question! It is incredibly difficult to test multithreaded code. The CHESS tool provided by Microsoft Research is one attempt at making life easier for concurrent programmers.
A class is generally considered thread-safe if its methods can be invoked by multiple threads concurrently without corrupting the state of the class or causing unexpected side-effects. There are many reasons why a class may not be thread safe, although some common reasons are that it contains some state that would be corrupted on concurrent access.
There are a number of ways to make a class thread-safe:
- Make it immutable, if a class contains no state it is safe to use concurrently from multiple threads.
- Employ locking to reduce concurrency. However, this is no guarantee of thread safety, it just ensures that a block of code will not be executed concurrently by multiple threads. If state is stored between method invocations this might still become inconsistent.
How you create a thread-safe class really depends on what you want to do with the class in question.
You also need to ask yourself, do I need to make my class threadsafe? a common model of most UI frameworks is that there is a single UI thread. For example in WinForms, WPF and Silverlight the majority of your code will be executed from the UI thread which means you do not have to build thread-safety into your classes.
First of all, don't use lock(this).
This can cause deadlocks. Because other code can lock that same object from outside the class' scope. You should create a local Object and use it as the class' lock.
Second, thread safety is a complicated issue. There's tons of material about this on the web.
As a rule of thumb, all public methods should be locked and thread safe for the class to be thread-safe.
A Class is considered thread safe if only one thread at a time can modify the state of the objects created from the class OR the class provide such functionality that multiple threads can call various methods of the class at same time.
When I use lock(C#) keyword, it means my class is thread-safe or not?
When you use lock it means that the portion of code inside the lock {}
is thread safe. It doesn't guarantee that your class is thread safe. And as Yochai Timmer said it is not a good idea to lock(this)
How to I evaluate thread-safety of a class? Is there any TESTS to be sure that a class is 100% thread safe? I am not sure there are any tests because it is always possible in multi-threading that you are by chance getting correct results. So in order to be sure you can go through the code of class to see how it is making sure it is thread safe
Very simple explanation: Thread safe type means you don't need any additional synchronization mechanisms when using your type. Say you can create an instance pass a reference to another thread (or multiple threads) and use methods/properties from both threads without any additional overhead for thread safety.
精彩评论