.NET线程中的所有静态/共享方法都是安全的吗? [英] Are All the Static/Shared Methods in .NET Thread Safe?

查看:63
本文介绍了.NET线程中的所有静态/共享方法都是安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在MSDN中阅读.NET Framework类库文档时,我经常注意到Thread Safety声明。

声明通常被描述为''此类型的任何公共静态(在Visual Basic中共享)成员都是线程安全的。任何实例成员都不保证是线程安全的。''

所以,这是否意味着所有用.NET兼容编程语言编写的静态/共享方法,如C#,VB.NET,都是有保证的要同步和线程安全吗?或者,我们仍然需要实现我们的自定义代码以确保静态方法的线程安全性?

I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN.
The declaration is usually described as ''Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.''
So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be synchronized and thread safe? Or else, we still need to implement our custom code to ensure the thread-safty for static methods?

推荐答案

" Laser Lu" < la ****** @ 163.com在留言中写道

新闻:OO ************** @ TK2MSFTNGP02.phx.gbl ...
"Laser Lu" <la******@163.comwrote in message
news:OO**************@TK2MSFTNGP02.phx.gbl...

[...]声明通常被描述为

''任何公共静态(在Visual Basic中共享)成员
这种类型的
是线程安全的。任何实例成员

都不保证是线程安全的。''

所以,这是否意味着所有写入的静态/共享方法

in .NET兼容的编程语言,如C#,

VB.NET,保证同步和线程

安全吗?或者,我们仍然需要实现我们的自定义

代码以确保静态方法的线程安全性?
[...] The declaration is usually described as
''Any public static (Shared in Visual Basic) members
of this type are thread safe. Any instance members
are not guaranteed to be thread safe.''
So, does this mean All the static/shared methods written
in .NET compatible programming language, such as C#,
VB.NET, are guaranteed to be synchronized and thread
safe? Or else, we still need to implement our custom
code to ensure the thread-safty for static methods?



Microsoft的编程指南建议您将所有

公共静态方法设置为线程安全的,实际上他们已经遵循了这些

他们在框架中提供的类中的指南。

但是,这不是自动的。只是将方法标记为静态不会确保它是踩踏板安全的。你仍然需要检查你的代码

并查看其中是否有可能受到影响的内容,例如可由两个线程访问的共享

变量,以及应用足够的锁定

机制以确保其安全。

The programming guidelines from Microsoft recommend that you make all
your public static methods thread-safe, and indeed they have followed those
guidelines in the classes that they have provided with the Framework.
However, this is not automatic. Just marking a method as static does not
guarantee that it will be tread-safe. you still have to examine your code
and see if there is something in it that could be affected, such as a shared
variable that could be accessed by two threads, and apply adequate locking
mechanisms to ensure its safety.


声明通常被描述为''任何公共静态(共享)

Visual Basic)这种类型的成员是线程安全的。任何实例成员都不能保证是线程安全的。''

所以,这是否意味着所有用.NET编写的静态/共享方法兼容

编程语言,如C#,VB.NET,保证同步

和线程安全?


是的。我从来没有听说过静态(共享)方法*没有*是

线程安全。


静态/共享方法始终是线程的原因 - 安全是因为

没有一个类的实例,因此,没有成员变量为

线程以相互矛盾的方式踩踏。


静态/共享方法中的任何局部变量都存在于调用线程上调用代码的

特定堆栈帧中,因此,

将永远不会与多个线程冲突同时击中

与本地变量相同的方法。

-


Peace&快乐的计算,


Mike Labosh,MCSD MCT

所有者,vbSensei.Com


" Escriba coda ergo 。总和" - vbSensei
The declaration is usually described as ''Any public static (Shared in
Visual Basic) members of this type are thread safe. Any instance members are
not guaranteed to be thread safe.''
So, does this mean All the static/shared methods written in .NET compatible
programming language, such as C#, VB.NET, are guaranteed to be synchronized
and thread safe?

Yes. I have never heard of a static (Shared) method *not* being
thread-safe.

The reason that static / Shared methods are always thread-safe is because
there is no instance of a class, and therefore, no member variables for
threads to stomp on in contradictory ways.

Any local variables inside a static / Shared method exist locally within the
particular stack frame of the calling code on the calling thread, and
therefore, will never conflict with multiple threads simultaneously hitting
the same method with local variables.
--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei


不一定是真的。如果静态/共享方法包含它在调用中维护的内部状态

变量,那么如果

实现者没有确保内部线程安全,则它可能不是线程安全的。也就是说,我不认为
相信框架中有任何非线程安全的静态/共享方法

本身,但考虑到框架的大小,我不要认为你可以保证

任何给定的方法都是线程安全的而不参考文档。


Mike Ober。


" Mike Labosh" < mlabosh_at_hotmail_dot_comwrote in message

news:eD ************** @ TK2MSFTNGP06.phx.gbl ...
Not necessarily true. If the static/shared method contains internal state
variables that it maintains across calls, it may not be thread safe if the
implementer didn''t ensure internal thread safety. That said, I don''t
believe there are any non-thread safe static/shared methods in the framework
itself, but given the size of the framework, I don''t think you can guarantee
that any given method is thread safe without referring to the documentation.

Mike Ober.

"Mike Labosh" <mlabosh_at_hotmail_dot_comwrote in message
news:eD**************@TK2MSFTNGP06.phx.gbl...

>声明通常被描述为''任何公共静态(在
中共享
>The declaration is usually described as ''Any public static (Shared in



$ Visual Basic)成员这种类型是线程安全的。任何实例成员



不保证是线程安全的。''

所以,这是否意味着所有写入的静态/共享方法.NET

兼容

编程语言,如C#,VB.NET,保证同步



和线程安全吗?


是的。我从来没有听说过静态(共享)方法*没有*是

线程安全。


静态/共享方法始终是线程的原因 - 安全是因为

没有一个类的实例,因此,没有成员变量为

线程以相互矛盾的方式踩踏。


静态/共享方法中的任何局部变量都存在于调用线程上调用代码的



特定堆栈帧中,并且

因此,永远不会与多个线程冲突

点击

与本地变量相同的方法。

-


Peace&快乐的计算,


Mike Labosh,MCSD MCT

所有者,vbSensei.Com


" Escriba coda ergo 。总和" - vbSensei

Visual Basic) members of this type are thread safe. Any instance members
are
not guaranteed to be thread safe.''
So, does this mean All the static/shared methods written in .NET
compatible
programming language, such as C#, VB.NET, are guaranteed to be
synchronized
and thread safe?

Yes. I have never heard of a static (Shared) method *not* being
thread-safe.

The reason that static / Shared methods are always thread-safe is because
there is no instance of a class, and therefore, no member variables for
threads to stomp on in contradictory ways.

Any local variables inside a static / Shared method exist locally within
the
particular stack frame of the calling code on the calling thread, and
therefore, will never conflict with multiple threads simultaneously
hitting
the same method with local variables.
--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei



这篇关于.NET线程中的所有静态/共享方法都是安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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