线程安全和成员变量 [英] Thread safety and member variables

查看:106
本文介绍了线程安全和成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道线程安全和成员变量。如果我有这样的班级:


class foo {

private float m_floater = 0.0;

public void bar(){

m_floater = true;

}

}


变量m_floater线程是否安全?如果我编写一个测试程序并在VS2005中运行
,那么m_floater变量最终会出现在我的调试器中的本地窗口

中(对于每个线程)所以它看起来像这是线程

安全。我假设如果我将我的成员变量

变成存储在托管堆上的somthing,这会改变吗? (即每个线程

都有自己的堆栈,但只有一个堆可用于存储)。


感谢您帮助解决这个看似简单的问题。 />
HC

Hi all,

I am wondering about thread safety and member variables. If I have
such a class:

class foo {
private float m_floater = 0.0;
public void bar(){
m_floater = true;
}
}

Is the variable m_floater thread safe? If I write a test program and
run it in VS2005, the m_floater variable ends up in the locals window
in my debugger (for each thread) so it would seem that it is thread
safe. I am assuming this would change if I made my member variable
into somthing that was stored on the managed heap? (i.e. each thread
has its own stack but there is only one heap available for storage).

thanks for helping with this seemingly simple question.
HC

推荐答案

编辑:

m_floater = true应为m_floater = 17.7

m_floater = true should be m_floater = 17.7


2008年8月13日星期三16:49:41 -0700,<他************* @ googlemail.comwrote:
On Wed, 13 Aug 2008 16:49:41 -0700, <He*************@googlemail.comwrote:

我想知道线程安全和成员变量。如果我有这样的班级:


class foo {

private float m_floater = 0.0;

public void bar(){

m_floater = true;

}

}


变量m_floater线程是否安全?
I am wondering about thread safety and member variables. If I have
such a class:

class foo {
private float m_floater = 0.0;
public void bar(){
m_floater = true;
}
}

Is the variable m_floater thread safe?



不,完全没有。那段代码会让你觉得它会是什么?

No, not at all. What about that code makes you think it would be?


如果我写一个测试程序并且

在VS2005中运行它,那么m_floater变量最终在本地窗口

在我的调试器中(对于每个线程)所以它似乎是线程

安全。
If I write a test program and
run it in VS2005, the m_floater variable ends up in the locals window
in my debugger (for each thread) so it would seem that it is thread
safe.



首先,您不会在本地人

窗口中看到会员字段,除非是隐含的this的后代变量。它不是一个局部变量,而是这个。变量是。其次,如果只需要

使成员字段线程安全是的,好吧......一个成员字段,

然后默认情况下所有类都是线程安全的。这肯定不是

true。

Firstly, you''re not going to see a member field show up in the "Locals"
window, except as a descendant of the implicit "this" variable. It''s not
a local variable, but the "this" variable is. Second, if all it took to
make a member field "thread safe" was for it to be, well...a member field,
then all classes would be thread safe by default. That''s definitely not
true.


我假设如果我使用我的会员变量这会改变

存入托管堆上的somthing? (即每个线程

都有自己的堆栈,但只有一个堆可用于存储)。
I am assuming this would change if I made my member variable
into somthing that was stored on the managed heap? (i.e. each thread
has its own stack but there is only one heap available for storage).



你的班级foo是一个引用类型,因此始终从

堆中分配。所以任何实例字段也都存储在堆上,其中类是

(毕竟它们是类的一部分)。你不能把你的成员

变量变成存储在托管堆上的东西。它已经

了。


确实本地变量本质上是线程安全的,只有来自的才能访问
声明它们所声明的方法正在执行的线程。但这与

类中的内容无关,因为你不能在一个局部变量中存储一个类。你只能将
存储_reference_存储到局部变量中的一个类,并且该引用

将始终引用存储在堆中的数据。因此,如果没有某种类型的b $ b同步,该类的任何实例成员都不是线程安全的(任何

静态成员也不是,但这是一个单独的问题:))。


最后,为了避免你开始认为这是一个只有类的东西,那么

值类型(即结构)然后必须是线程安全的,对于仅在$ / b $ b方法中声明为局部变量的结构,只有

才为真。用作类成员类型的结构仍然会在堆上与类的其余部分分配,因此

不会继承线程安全一个结构声明为局部变量

会。


Pete

Your class "foo" is a reference type, and so is always allocated from the
heap. So any instance fields are also stored on the heap, where the class
is (they are, after all, part of the class). You can''t make your "member
variable into something that was stored on the managed heap". It already
is.

It''s true that local variables are inherently thread safe, being
accessible only from the thread in which the method where they are
declared is executing. But that has nothing to do with things inside
classes, since you can''t store a class in a local variable. You can only
store a _reference_ to a class in a local variable, and that reference
will always refer to data stored in the heap. Thus, without some sort of
synchronization, any instance member of the class is not thread safe (any
static member isn''t either, but that''s a separate question :) ).

Finally, lest you start thinking that this is a class-only thing, and that
value types (i.e. structs) then must be thread-safe, that would only be
true for structs that are only ever declared as local variables inside
methods. A struct used as the type for a member of a class would still
wind up being allocated on the heap with the rest of the class, and so
would not inherit the thread safety a struct declared as a local variable
would.

Pete


Peter Duniho写道:
Peter Duniho wrote:

2008年8月13日星期三16:49:41 -0700,<他************* @ googlemail。 comwrote:
On Wed, 13 Aug 2008 16:49:41 -0700, <He*************@googlemail.comwrote:

>我想知道线程安全和成员变量。如果我有这样的课程:

class foo {
private float m_floater = 0.0;
public void bar(){
m_floater = true;
}


变量m_floater线程是否安全?
>I am wondering about thread safety and member variables. If I have
such a class:

class foo {
private float m_floater = 0.0;
public void bar(){
m_floater = true;
}
}

Is the variable m_floater thread safe?



不,完全没有。那段代码会让你觉得它会是什么?


No, not at all. What about that code makes you think it would be?



数据本身绝不是线程安全的。数据结合

如何使用它们可以是线程安全的。


实际上上面的代码是线程安全的。


它也没用,所以好处并不大。


Arne

Data are never thread safe in themselves. Data combined with
how they are used can be thread safe or not.

Actually the above code is thread safe.

It is also useless, so the benefits are not that big.

Arne


这篇关于线程安全和成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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