使用volatile关键字 [英] use of volatile keyword

查看:73
本文介绍了使用volatile关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧


i知道我不应该交叉发帖,

但是我不确定哪个小组发布这个问题。

2关于volatile的问题:


1.我在2个线程访问同一个变量时使用volatile

这是正确使用volatile吗?


2.当声明一个long类型的变量时,我得到一个编译错误

不能长期使用volatile!

这意味着所有long'都会自动挥发?

i假设因为它们是64位,编译器永远不会将它们放在CPU中。


非常感谢任何帮助或想法。


assaf

解决方案

你好Assaf,

我认为你使用c#。

1.当2个线程访问同一个变量时,我使用volatile 是的,这是使用* volatile *修饰符的地方之一。但是

你不希望只使用* volatile *来保证线程安全,不是你的b $ b。即使使用volatile,你也可以访问共享资源,所以应该使用锁或其他同步技术。

2.当声明long类型的变量时,我得到一个编译错误
不能长期使用volatile!
这是否意味着所有long'都会自动挥发?
我假设因为它们是64位,编译器永远不会
将它们放在CPU中。



我找不到任何信息为什么* long *变量不能*易变*而我

相信它们不是而不是它们总是。

对于一个易变量的变量是不够的,不要在CPU中缓存

寄存器。它会影响c#编译器和JIT编译器的优化。


HTH

B \ rgds

100


" Assaf" <关于************************** @ hotmail.com>在消息中写道

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


< blockquote> Stoitcho Goutsev(100)[C#MVP]写道:

嗨Assaf,
我认为你使用c#。

1. i当2个线程访问同一个变量时使用volatile
这是正确使用volatile吗?是的,这是使用* volatile *修饰符的地方之一。但是,你不希望只使用* volatile *来保证线程安全,



不是你。即使使用volatile,您也可以访问共享资源,因此应该使用锁
或其他同步技术。




易失性用于导致CF中的构建错误。在你想太多的事情之前让它实际编译



2.声明变量时类型很长,我得到一个编译错误
不能长期使用volatile!
这是否意味着所有long'都会自动挥发?
我假设因为它们是64位,编译器永远不会


放在CPU中。



我找不到任何信息为什么* long *变量不能*易变*我相信它们并非总是如此。




可能因为加载/存储long不是原子操作并且

因此不安全。


希尔顿



我找不到任何信息为什么* long *变量不能* volatile *而我相信他们并非总是如此。



可能因为加载/存储长时间不是原子操作而且因此不安全。




是的,它只对原子类型有用(64位整数

肯定不是)。


-

Miha Markic [MVP C#] - RightHand .NET咨询&软件开发

miha at rthand com
www.rthand.com


hi all

i know that i should not cross-post,
but i am not sure to which group to post this question.
2 quesions about volatile:

1. i use volatile when 2 threads access the same variable
is this the proper use of volatile?

2. when declaring a variable of type long, i get a compilation error
cannot use volatile with long!
does this mean that all long''s are automatically volatile?
i am assuming that since they are 64 bit, the compiler will never place
them in the CPU.

any help or thoughts is very much appreciated.

assaf

解决方案

Hi Assaf,
I take it you use c#.

1. i use volatile when 2 threads access the same variable
is this the proper use of volatile? Yes, this is one of the places where *volatile* modifier is used. However
you are not expecting to have thread safety by using *volatile* only, aren''t
you. Even with volatile you will have access to shared resource so locks or
other sync techniques should be used.

2. when declaring a variable of type long, i get a compilation error
cannot use volatile with long!
does this mean that all long''s are automatically volatile?
i am assuming that since they are 64 bit, the compiler will never place them in the CPU.


I couldn''t find any info why *long* variables cannot be *volatile* and I
believe they are not rather than they are always.
For one variable to be volatile is not enough not to be cached int the CPU
registers. It affect optimization done by the c# compiler and JIT compiler.

HTH
B\rgds
100

"Assaf" <as**************************@hotmail.com> wrote in message
news:eS**************@TK2MSFTNGP10.phx.gbl...


Stoitcho Goutsev (100) [C# MVP] wrote:

Hi Assaf,
I take it you use c#.

1. i use volatile when 2 threads access the same variable
is this the proper use of volatile? Yes, this is one of the places where *volatile* modifier is used. However
you are not expecting to have thread safety by using *volatile* only,


aren''t you. Even with volatile you will have access to shared resource so locks or other sync techniques should be used.



Volatile used to cause a build error in the CF. Make it actually compiles
before you think things through too much.

2. when declaring a variable of type long, i get a compilation error
cannot use volatile with long!
does this mean that all long''s are automatically volatile?
i am assuming that since they are 64 bit, the compiler will never


place

them in the CPU.


I couldn''t find any info why *long* variables cannot be *volatile* and I
believe they are not rather than they are always.



Probably because loading/storing a long is not an atomic operation and
therfore isn''t safe.

Hilton



I couldn''t find any info why *long* variables cannot be *volatile* and I
believe they are not rather than they are always.



Probably because loading/storing a long is not an atomic operation and
therfore isn''t safe.



Yes, it volatile is useful only for atomic types which (64bit integer
certainly isn''t).

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com


这篇关于使用volatile关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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