更多静态+线程安全 [英] more static + thread safety

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

问题描述

说我有这个班级


公共课简单

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}


是否线程安全?多线程当然可以访问他们自己的简单没有问题的
实例...

但是如果我有另一个具有静态引用的类怎么办呢?简单:


公共类StaticReference

{

private static简单simple = new Simple();


public static简单

{

get {return simple; }

}

}

现在,如果我有多个线程访问StaticReference,那么他们可以互相打扰
改变简单的名称属性实例

他们分享,对吗? 静态参考也是如此。线程安全?


谢谢,

彼得

解决方案

< blockquote> 8月9日上午8:13,Peter K< xdz ... @ hotmail.comwrote:


说我有这个班级


公共类简单

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}


}


是否线程安全?



否 - 不是说这本身就是一件坏事。大多数类

不需要是线程安全的,即使它们用于多线程的b
环境。相反,使用它们的类需要考虑多线程性质的



多个线程当然可以访问自己的实例简单

没有问题...



是的,但这并不是什么让线程安全。


但是,如果我有另一个具有静态引用的类,该怎么办?简单:


公共类StaticReference

{

private static简单simple = new Simple();


public static简单

{

get {return simple; }

}


}


现在,如果我有多个线程访问StaticReference,那么他们可以

通过改变简单的名称属性来互相打乱。实例

他们分享,对吗? 静态参考也是如此。线程安全的?



StaticReference本身是线程安全的,但是简单不是 - 类

使用StaticReference.Simple需要采取适当的行动

确保它们始终可以看到Name的最新值。这通常是通过锁定对共享数据的任何访问来完成的。


参见 http://pobox.com/~skeet/csharp/threads 了解更多信息。


Jon


Peter K写道:


说我有这个班级


公共类简单

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}


是否线程安全?多个线程当然可以访问他们自己的简单没有问题的
实例...



以防万一Jon的回复是'已经足够清楚:


只要每个线程都有自己的Simple实例,那么

都没有问题(因为你已经指出了)出)。但是线程安全意味着

,即使在不同的线程之间共享一个实例,也可以确保
的数据完整性(请注意,这与

保证不同线程正在合作:))。


从你对Jon的回复中,我觉得你现在已经明白了这个,但是我认为确保没有伤害。


Pete


Peter K写道:


说我有这个班级


公共班级简单

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}


是否线程安全?多个线程当然可以访问他们自己的简单没有问题的
实例...



以防万一Jon的回复是'已经足够清楚:


只要每个线程都有自己的Simple实例,那么

都没有问题(因为你已经指出了)出)。但是线程安全意味着

,即使在不同的线程之间共享一个实例,也可以确保
的数据完整性(请注意,这与

保证不同线程正在合作:))。


从你对Jon的回复中,我觉得你现在已经明白了这个,但是我认为确保没有伤害。


Pete


Say I have this class

public class Simple
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}

Is it "thread safe"? Multiple threads can certainly access their own
instances of Simple without problems...
But what if I have another class which has a "static reference" to Simple:

public class StaticReference
{
private static Simple simple = new Simple();

public static Simple
{
get { return simple; }
}
}
Now if I have multiple threads accessing StaticReference then they can
upset each other by altering the Name property on the "simple" instance
they share, correct? So is "StaticReference" thread-safe?


Thanks,
Peter

解决方案

On Aug 9, 8:13 am, Peter K <xdz...@hotmail.comwrote:

Say I have this class

public class Simple
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

}

Is it "thread safe"?

No - not that that''s necessarily a bad thing in itself. Most classes
don''t need to be thread-safe, even if they''re used in multi-threaded
environments. Instead, the classes using them need to take account of
the multi-threaded nature.

Multiple threads can certainly access their own instances of Simple
without problems...

Yes, but that''s not what makes something thread-safe.

But what if I have another class which has a "static reference" to Simple:

public class StaticReference
{
private static Simple simple = new Simple();

public static Simple
{
get { return simple; }
}

}

Now if I have multiple threads accessing StaticReference then they can
upset each other by altering the Name property on the "simple" instance
they share, correct? So is "StaticReference" thread-safe?

StaticReference itself is thread-safe, but Simple isn''t - classes
using StaticReference.Simple would need to take appropriate action to
ensure that they always see the most recent value of Name. This is
usually done through locking around any access to shared data.

See http://pobox.com/~skeet/csharp/threads for more information.

Jon


Peter K wrote:

Say I have this class

public class Simple
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}

Is it "thread safe"? Multiple threads can certainly access their own
instances of Simple without problems...

And just in case Jon''s reply wasn''t already clear enough:

As long as each thread has its own instance of Simple to use, then there
are no problems (as you already point out). But "thread-safe" means
that even if a single instance was shared between different threads,
data integrity would be ensured (note that this is different from
ensuring that the threads are cooperating :) ).

From your reply to Jon, it appears to me that you already understand
this now, but I figure it doesn''t hurt to make sure.

Pete


Peter K wrote:

Say I have this class

public class Simple
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}

Is it "thread safe"? Multiple threads can certainly access their own
instances of Simple without problems...

And just in case Jon''s reply wasn''t already clear enough:

As long as each thread has its own instance of Simple to use, then there
are no problems (as you already point out). But "thread-safe" means
that even if a single instance was shared between different threads,
data integrity would be ensured (note that this is different from
ensuring that the threads are cooperating :) ).

From your reply to Jon, it appears to me that you already understand
this now, but I figure it doesn''t hurt to make sure.

Pete


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

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