如何使用“挥发性”对于Int64值? [英] How to use "volatile" for Int64 value?

查看:88
本文介绍了如何使用“挥发性”对于Int64值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我真的需要在.NET v1.1程序中使用volatile System.Int64
C#中的
。但是编译器抱怨一个不稳定的字段不能是类型

long。如何解决它?或者还有其他方法来获得类似的Int64类型的

效果吗?

另一个问题不那么敦促的问题是,为什么长变量不能

可以用作volatile吗?我明白在32位拱门中。 64位操作不是原子的,但似乎可能存在锁定等等,而且对于大多数程序员而言,只要它有效,他们就不会关心。 (因此会将其从

错误更改为警告volatile不是那么易变)


感谢任何输入。 :)


问候,

刘雷畅

解决方案

刘磊Cheong< le **** @ yehoo.com.hk>写道:

我真的需要在C#中使用易变的System.Int64来实现.NET v1.1程序。


我怀疑你这样做。你可能需要和volatile一样的效果,但是

这不是一回事。

但是编译器抱怨一个volatile字段不能是
long。如何解决它?或者还有其他方法可以获得Int64类型的类似效果吗?


这取决于你想要实现的目标。你能给

更多信息吗?通常在每次访问或更改

变量时锁定都足够了。

另一个问题不那么敦促的问题是,为什么长变量不能被使用像波动?我明白在32位拱门中。 64位操作不是原子的,但似乎可能存在锁定或应用,对于大多数程序员来说,只要它有效,他们就不会在意。 (因此会将其从
错误更改为警告挥发性不是那么易变)




我对于一个人不会希望编译器能够更改我的代码以启动

自动添加锁定语句。自己添加它们并不困难

,它会使代码更清晰。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件




Lau Lei Cheong <乐**** @ yehoo.com.hk>在消息中写道

新闻:uJ ************* @ TK2MSFTNGP10.phx.gbl ...

你好,

我真的需要在C#中使用volatile的System.Int64来实现.NET v1.1程序。但编译器抱怨一个不稳定的字段不能是长的类型。如何解决它?或者还有其他方法可以获得Int64类型的类似效果吗?


你已经进一步回答了你自己的问题。

另一个问题不那么迫切的问题是,为什么多长的变量不能被用作挥发性?我明白在32位拱门中。 64位操作
不是原子的


正确。

,但似乎可能存在锁定或应用,并且对于大多数




volatile对性能没有显着影响,不影响线程

安排,而锁定。


如果你想要一个锁,只需使用自己的锁。


object lockObj = new object();


lock(lockObj)

{

//做Int64东西
}


如果编译器做到这一点,它必须创建一个对象作为每个变量的锁

它会有在访问时间内获取并释放

的锁定。这种精细的锁定粒度确实非常糟糕。


感谢Jon和Nick。


所以我想我会使用锁来实现。我心里想要在不影响工作线程性能的情况下嗅出价值,但是一个想法也告诉我它是'b $ b'。不可能不停地获得价值

工人一段时间。我将如何尝试以更大的时间间隔更新

以便最大限度地减少影响。


再次感谢。 :)


" Nick Hounsome" < NH *** @ nickhounsome.me.uk> ????????????? D:zi ******************* @ fe1.news.blue yonder.co.uk ... < blockquote class =post_quotes>
刘雷畅 <乐**** @ yehoo.com.hk>在消息中写道
新闻:uJ ************* @ TK2MSFTNGP10.phx.gbl ...

你好,

我真的需要在C#中使用volatile的System.Int64来实现.NET v1.1程序。但编译器抱怨一个不稳定的字段不能是长的类型。如何解决它?或者还有其他方法可以获得Int64类型的类似效果吗?



你已经进一步回答了你自己的问题。

另一个提出问题的问题是,为什么长变量不能用作波动?我明白在32位拱门中。 64位操作
不是原子的



正确。

,但似乎可能存在锁定等应用,以及对于大多数程序员来说,只要它有效,他们就不会在意。 (因此会将其从错误更改为警告volatile不是那么易变)



volatile对性能没有显着影响且不影响线程
安排,而锁定。

如果你想要一个锁只需使用自己的。

对象lockObj = new object();
lock(lockObj)
{做Int64的东西
}
如果编译器做了它,就必须创建一个对象来充当
锁定每个变量,它必须获取并释放锁定
仅用于访问时间。这种精细的锁定粒度非常糟糕。



Hello,

I really need to use volatile System.Int64 for a .NET v1.1 program
in C#. But the compiler complains "a volatile field can not be of type
long". How to work around it? Or is there any other way to get similar
effect for Int64 type?

Another question less urging question is, why long variables can''t
be used as volatile? I understand that in 32-bit arch. 64-bit operations are
not atomic, but seems that there could be locks or so applied, and for most
programmers they won''t care as long as it works. (Hence would change it from
error to warning that the "volatile" is not so "volatile")

Thanks for any inputs. :)

Regards,
Lau Lei Cheong

解决方案

Lau Lei Cheong <le****@yehoo.com.hk> wrote:

I really need to use volatile System.Int64 for a .NET v1.1 program
in C#.
I doubt that you do. You may need the same effects as volatile, but
that''s not the same thing.
But the compiler complains "a volatile field can not be of type
long". How to work around it? Or is there any other way to get similar
effect for Int64 type?
That depends on exactly what you''re trying to achieve. Could you give
more information? Usually locking every time you access or change the
variable is good enough.
Another question less urging question is, why long variables can''t
be used as volatile? I understand that in 32-bit arch. 64-bit operations are
not atomic, but seems that there could be locks or so applied, and for most
programmers they won''t care as long as it works. (Hence would change it from
error to warning that the "volatile" is not so "volatile")



I for one wouldn''t want the compiler to be changing my code to start
automatically adding lock statements in. It''s not hard to add them
yourself, and it would make the code clearer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



"Lau Lei Cheong" <le****@yehoo.com.hk> wrote in message
news:uJ*************@TK2MSFTNGP10.phx.gbl...

Hello,

I really need to use volatile System.Int64 for a .NET v1.1 program
in C#. But the compiler complains "a volatile field can not be of type
long". How to work around it? Or is there any other way to get similar
effect for Int64 type?
You ahve answered your own question further on.
Another question less urging question is, why long variables can''t
be used as volatile? I understand that in 32-bit arch. 64-bit operations
are not atomic
Correct.
, but seems that there could be locks or so applied, and for most
programmers they won''t care as long as it works. (Hence would change it
from error to warning that the "volatile" is not so "volatile")



volatile has no significant performance impact and do not affect thread
sheduling whereas locks do.

If you want a lock just use your own.

object lockObj = new object();

lock(lockObj)
{
// do Int64 stuff
}

If the compiler did it it would have to create an object to act as the lock
for every variable and it would have to acquire and release the lock for
just the time of access. This fine granularity of locking performs really
badly.


Thanks Jon and Nick.

So I guess I''ll implement using lock. I was asking in the mind to "sniff"
the value out without impacting the worker threads'' performance, but a
second thought also told me it''s not possible to get the value without stop
the worker for a while. How I''ll try to update at a larger time interval in
order to minimize the impact.

Thanks again. :)

"Nick Hounsome" <nh***@nickhounsome.me.uk> ???g?ó?l¥ó·s?D:zi*******************@fe1.news.blue yonder.co.uk...


"Lau Lei Cheong" <le****@yehoo.com.hk> wrote in message
news:uJ*************@TK2MSFTNGP10.phx.gbl...

Hello,

I really need to use volatile System.Int64 for a .NET v1.1 program
in C#. But the compiler complains "a volatile field can not be of type
long". How to work around it? Or is there any other way to get similar
effect for Int64 type?



You ahve answered your own question further on.

Another question less urging question is, why long variables can''t
be used as volatile? I understand that in 32-bit arch. 64-bit operations
are not atomic



Correct.

, but seems that there could be locks or so applied, and for most
programmers they won''t care as long as it works. (Hence would change it
from error to warning that the "volatile" is not so "volatile")



volatile has no significant performance impact and do not affect thread
sheduling whereas locks do.

If you want a lock just use your own.

object lockObj = new object();

lock(lockObj)
{
// do Int64 stuff
}

If the compiler did it it would have to create an object to act as the
lock for every variable and it would have to acquire and release the lock
for just the time of access. This fine granularity of locking performs
really badly.



这篇关于如何使用“挥发性”对于Int64值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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