如何在long类型的字段上使用Volatile? [英] How to use Volatile on field of type long ?

查看:132
本文介绍了如何在long类型的字段上使用Volatile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在文档中读到c#Volatile关键字不能在很长的

类型字段中使用。这个字段是由多个线程使用但我不想要锁(obj)的

性能开销。


怎么办?


谢谢。

Hi,

i read in documentation that the c# Volatile keyword cann''t be use on long
type fields. this field is use by multiple threads but i don''t want the
performance overhead of lock(obj) .

What to do ?

Thanks.

推荐答案

yaron写道:
我在读c#Volatile关键字不能用于长
类型字段的文档。这个字段是由多个线程使用但我不想要锁(obj)的性能开销。

怎么办?
i read in documentation that the c# Volatile keyword cann''t be use on long
type fields. this field is use by multiple threads but i don''t want the
performance overhead of lock(obj) .

What to do ?



您是否真的测量过锁定的性能开销?在我的

体验中,如果锁定通常是无争议的,很少有应用程序实际会注意到任何重大的

差异。


我不喜欢在这种情况下,我相信*有*任何替代品。


Jon



Have you actually measured the performance overhead of locking? In my
experience, very few applications will actually notice any significant
difference if the lock is usually uncontested.

I don''t believe there *are* any alternatives in this case.

Jon





在32位系统上,长时间(64位)的操作不能保证

atomic。 Interlock类具有递增/递减的重载,这需要

长参数相对于彼此只是原子的(也就是说,只要增量和减量为
,因此增量和减量仅为2您想要的操作

对它们执行);否则,你需要一个锁。

解决方案:

- 考虑使用int而不是long if if posible

- 同步访问var - not如果争论很多开销

不高


Thi - http://thith.blogspot.com

Hi,

On 32bit systems, an operation on a long (64 bit) is not guaranteed
atomic. Interlock class has overloads of Increment/Decrement that take
long param are only atomic with respect to each other (that is, as long
as Increment and Decrement are the 2 only operations you ever want to
perform on them); otherwise, you need a lock.
Solution:
- consider using int instead of long if posible
- synchronize access to the var - not much overhead if the contention
is not high

Thi - http://thith.blogspot.com


大家好,


感谢您的快速转发。

请让我问你另一个问题:


班级玩家

{

private bool enable;

public bool启用

{

get {retturn enable;}
$ b在玩家实例的多线程场景中,$ b设置{enable = value;}

}

}


,做我需要在Enable属性中设置一个锁定

语句,或者我可以放松并相信这是一个

原子操作。

what很好吗?


谢谢大家。

" Truong Hong T喜"写道:
Hi All,

thanks for your quick resposne.
please let me ask you another question :

Class Player
{
private bool enable;
public bool Enable
{
get {retturn enable;}
set {enable=value;}
}
}

in a multi-threading scenario on Player instance , do i need to put a lock
statement in the Enable property or can i be relax and trust that this is an
atomic operation.
what is good to do ?

Thanks you all.
"Truong Hong Thi" wrote:


在32位系统上,长(64位)的操作不能保证原子。 Interlock类具有递增/递减的重载,其中
long param只相对于彼此是原子的(也就是说,增量和减量只是你想要的两个操作对他们表演);否则,你需要一个锁。
解决方案:
- 考虑使用int而不是long if if posible
- 同步访问var - 如果争用不高则没有多少开销

Thi - http://thith.blogspot.com
Hi,

On 32bit systems, an operation on a long (64 bit) is not guaranteed
atomic. Interlock class has overloads of Increment/Decrement that take
long param are only atomic with respect to each other (that is, as long
as Increment and Decrement are the 2 only operations you ever want to
perform on them); otherwise, you need a lock.
Solution:
- consider using int instead of long if posible
- synchronize access to the var - not much overhead if the contention
is not high

Thi - http://thith.blogspot.com



这篇关于如何在long类型的字段上使用Volatile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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