Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c# - Which features make a class to be thread-safe?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...