473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to use "volatile" for Int64 value?

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
Mar 6 '06 #1
10 2870
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
Mar 6 '06 #2

"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.
Mar 6 '06 #3
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.

Mar 6 '06 #4
The interlocked methods may be of use to you.
Look at System.Threading.Interlocked.*

HTH,

Adam.
===========

"Lau Lei Cheong" <le****@yehoo.com.hk> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.


Mar 7 '06 #5
Note that Interlocked methods using 64 bit values are not atomic on 32 bit
OS, so they are only thread safe on 64 bit systems.

Willy.

"Adam Benson" <Ad*********@NOSPAMMYSPAM.omnibus.co.uk> wrote in message
news:el**************@TK2MSFTNGP15.phx.gbl...
| The interlocked methods may be of use to you.
| Look at System.Threading.Interlocked.*
|
| HTH,
|
| Adam.
| ===========
|
| "Lau Lei Cheong" <le****@yehoo.com.hk> wrote in message
| news:%2****************@TK2MSFTNGP15.phx.gbl...
| > 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.
| >>
| >>
| >
| >
|
|
Mar 7 '06 #6
Yes, I've already know about those "interlock*" APIs. But if 64-bit
arithmatic operations are not atomic, I doubt parameter passing are atomic
too.

If parameter passing can be atomic, I guess I'll just wrap the whole routine
in a function and pass the values as parameter.

"Willy Denoyette [MVP]" <wi*************@telenet.be> ¼¶¼g©ó¶l¥ó·s»D:O6**************@TK2MSFTNGP15.phx.g bl...
Note that Interlocked methods using 64 bit values are not atomic on 32 bit
OS, so they are only thread safe on 64 bit systems.

Willy.

"Adam Benson" <Ad*********@NOSPAMMYSPAM.omnibus.co.uk> wrote in message
news:el**************@TK2MSFTNGP15.phx.gbl...
| The interlocked methods may be of use to you.
| Look at System.Threading.Interlocked.*
|
| HTH,
|
| Adam.
| ===========
|
| "Lau Lei Cheong" <le****@yehoo.com.hk> wrote in message
| news:%2****************@TK2MSFTNGP15.phx.gbl...
| > 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.
| >>
| >>
| >
| >
|
|

Mar 13 '06 #7

"Lau Lei Cheong" <le****@yehoo.com.hk> wrote in message
news:OT****************@TK2MSFTNGP12.phx.gbl...
Yes, I've already know about those "interlock*" APIs. But if 64-bit
arithmatic operations are not atomic, I doubt parameter passing are atomic
too.

If parameter passing can be atomic, I guess I'll just wrap the whole
routine in a function and pass the values as parameter.


Now you've completely confused everyone.
What have parameters got to do with anything?
Parameters and threads have nothing to do with each other because a
parameter is on the stack which is not shared between threads.
Mar 13 '06 #8
Nick Hounsome wrote:
Yes, I've already know about those "interlock*" APIs. But if 64-bit
arithmatic operations are not atomic, I doubt parameter passing are atomic
too.

If parameter passing can be atomic, I guess I'll just wrap the whole
routine in a function and pass the values as parameter.


Now you've completely confused everyone.
What have parameters got to do with anything?
Parameters and threads have nothing to do with each other because a
parameter is on the stack which is not shared between threads.


But if you use the interlocked APIs, you need to pass the variables you
want to increment/exchange/whatever by reference as parameters. I very
much suspect that that's what the previous post was referring to.

(For what it's worth though, I would expect it to work, just because
it's pointless to have the API available if it's not fit for purpose.)

Jon

Mar 13 '06 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Nick Hounsome wrote:
> Yes, I've already know about those "interlock*" APIs. But if 64-bit
> arithmatic operations are not atomic, I doubt parameter passing are
> atomic
> too.
>
> If parameter passing can be atomic, I guess I'll just wrap the whole
> routine in a function and pass the values as parameter.


Now you've completely confused everyone.
What have parameters got to do with anything?
Parameters and threads have nothing to do with each other because a
parameter is on the stack which is not shared between threads.


But if you use the interlocked APIs, you need to pass the variables you
want to increment/exchange/whatever by reference as parameters. I very
much suspect that that's what the previous post was referring to.

(For what it's worth though, I would expect it to work, just because
it's pointless to have the API available if it's not fit for purpose.)


But reference = pointer in the underlying implementation so there is no
problem.
Mar 13 '06 #10

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
| Nick Hounsome wrote:
| > > Yes, I've already know about those "interlock*" APIs. But if 64-bit
| > > arithmatic operations are not atomic, I doubt parameter passing are
atomic
| > > too.
| > >
| > > If parameter passing can be atomic, I guess I'll just wrap the whole
| > > routine in a function and pass the values as parameter.
| >
| > Now you've completely confused everyone.
| > What have parameters got to do with anything?
| > Parameters and threads have nothing to do with each other because a
| > parameter is on the stack which is not shared between threads.
|
| But if you use the interlocked APIs, you need to pass the variables you
| want to increment/exchange/whatever by reference as parameters. I very
| much suspect that that's what the previous post was referring to.
|
| (For what it's worth though, I would expect it to work, just because
| it's pointless to have the API available if it's not fit for purpose.)
|
| Jon
|
The API's available don't make the destinction between the OS versions you
are running your code on. That means that the operation is only guaranteed
to be atomic on 64 bit windows (64 bit CPU), also, you must properly align
the value on it's natural boundary or the functions will behave
unpredictably on MP systems, note that the latter is taken care of by the
..NET runtime.

Willy.
Mar 15 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.