MSDN:什么是"线程安全"? [英] msdn: What is "Thread Safety"?

查看:169
本文介绍了MSDN:什么是"线程安全"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在很多MSDN文档,这是线程安全的标题下写成的;

In many MSDN documents, this is written under the Thread Safety heading;

任何公共static这种类型的成员(在Visual Basic中的Shared)都是线程安全的。所有实例成员都不能保证是线程安全的。

"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."

例如, <一href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx">here

有人可以解释它,请在一个相当简单的方法? 谢谢:)

can someone explain it please in a rather simple way? Thank you :)

推荐答案

埃里克利珀具有优良的<一个href="http://blogs.msdn.com/b/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx">blog帖子这个问题。基本上它是对自己有点意思。

Eric Lippert has an excellent blog post about this. Basically it's somewhat meaningless on its own.

我个人不相信MSDN太多在这方面,当我看到锅炉板。这并不意味着它说什么。例如,它说,大约编码同样的事情 - 尽管从多个线程,我们都使用编码所有的地方

Personally I don't trust MSDN too much on this front, when I see that boiler-plate. It doesn't always mean what it says. For example, it says the same thing about Encoding - despite the fact that we all use encodings from multiple threads all over the place.

除非我有什么理由相信,否则(这是我做的编码)我认为我可以调用任何静态成员从任何线程与全局状态是否没有被破坏。如果我想使用的实例的来自不同线程对同一对象的成员,我认为没关系,如果我保证 - 通过锁定 - 只有一个线程将使用在同一时间的对象。 (这是当然的情况并非总是如此,有些对象具有的螺纹亲和力的积极不喜欢从多个线程被使用,即使带锁定在适当位置。用户界面控制是明显的例子。)

Unless I have any reason to believe otherwise (which I do with Encoding) I assume that I can call any static member from any thread with no corruption of global state. If I want to use instance members of the same object from different threads, I assume that's okay if I ensure - via locking - that only one thread will use the object at a time. (That's not always the case, of course. Some objects have thread affinity and actively dislike being used from multiple threads, even with locking in place. UI controls are the obvious example.)

当然,它变得非常棘手,如果正在共享对象unobviously - 如果我有每股引用了三分之一,那么我可能会最终使用了前两个对象分别来自不同线程的两个对象,所有的正确锁紧 - 但最终仍然腐蚀着第三个目的

Of course, it becomes tricky if objects are being shared unobviously - if I have two objects which each share a reference to a third, then I may end up using the first two objects independently from different threads, with all the proper locking - but still end up corrupting the third object.

如果一个类型的确实的宣传自己,是线程安全的,我希望它会给一些关于它的细节。这很容易,如果它是不可变的 - 你可以只使用实例,但是你无需担心他们。它的部分或全部线程安全的类型这是可变的,其中的细节问题很大。

If a type does advertise itself to be thread safe, I'd hope that it would give some details about it. It's easy if it's immutable - you can just use instances however you like without worrying about them. It's partially or wholly "thread-safe" types which are mutable where the details matter greatly.

这篇关于MSDN:什么是&QUOT;线程安全&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆