哪些功能使类成为线程安全的?
在 MSDN 中,一些 .NET 类的描述如下:
“此类型是线程安全的。”
或
“此类型的公共静态(在 Visual Basic 中共享)成员是线程安全的。实例成员不是保证是线程安全的。”。
我的问题是哪些功能使类成为线程安全的?
线程安全编程有什么标准、建议或指南吗?
当我使用lock(C#)关键字时,这意味着我的类是否是线程安全的?
如何评估类的线程安全性?是否有任何测试可以确保类 100% 线程安全?
示例:
public class MyClass
{
public void Method()
{
lock (this)
{
// Now, is my class 100% thread-safe like Microsoft classes?
}
}
type m_member1;
type m_member2;
}
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最重要的标准是确保所有静态成员都是线程安全的。您将看到所有编写良好的 API(包括 .NET 基类库)都全面保证了这一点。这是有充分理由的。由于静态成员在 AppDomain 中共享,因此它们可以被许多不同的线程使用,而您甚至没有意识到。为每个静态成员访问提供您自己的同步是很尴尬的。想象一下,如果Console.WriteLine 不是线程安全的,会是什么样子。
就建议和指南而言,有许多行之有效的并发编程模式。现有的模式涵盖了各种各样的编程问题,并使用了许多不同的同步机制。生产者-消费者模式是许多众所周知的模式之一,它恰好解决了大部分并发编程问题。
阅读 Joseph Albahari 的 C# 线程。它是最好、最受审查的可用资源之一。
没有!没有什么灵丹妙药可以使类成为线程安全的。
lock
关键字只是可用于使类安全地供多个线程同时访问的许多不同工具之一。但是,仅仅使用锁
并不能保证任何事情。 正确使用同步机制才能使代码线程安全。有很多方法可以错误地使用这些机制。这是价值百万美元的问题!测试多线程代码极其困难。 Microsoft Research 提供的 CHESS 工具是一种让并发的生活变得更轻松的尝试。程序员。
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.
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 alock
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.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.
如果一个类的方法可以由多个线程同时调用,而不会破坏类的状态或导致意外的副作用,则通常认为该类是线程安全的。类可能不是线程安全的原因有很多,尽管一些常见原因是它包含一些在并发访问时会被破坏的状态。
有多种方法可以使类成为线程安全的:
如何创建线程安全类实际上取决于您想要对相关类执行什么操作。
您还需要问自己,我需要使我的类线程安全吗?大多数 UI 框架的共同模型是只有一个 UI 线程。例如,在 WinForms、WPF 和 Silverlight 中,大部分代码将从 UI 线程执行,这意味着您不必在类中构建线程安全性。
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:
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.
首先,不要使用lock(this)。
这可能会导致死锁。因为其他代码可以从类范围之外锁定同一对象。您应该创建一个本地对象并将其用作类的锁。
其次,线程安全是一个复杂的问题。网络上有大量关于此的材料。
根据经验,所有公共方法都应该被锁定并且线程安全,以使类成为线程安全的。
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.
当我使用lock(C#)关键字时,这意味着我的类是否是线程安全的?
当您使用锁时,这意味着锁
{}
内的代码部分是线程安全的。它不能保证您的类是线程安全的。正如 Yochai Timmer 所说,lock(this)
不是一个好主意如何评估类的线程安全性?是否有任何测试可以确保类是 100% 线程安全的?
我不确定是否有任何测试,因为在多线程中您总是有可能偶然获得正确的结果。因此,为了确保您可以浏览类的代码,看看它是如何确保它是线程安全的
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 tolock(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.