473,466 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Some Questions about Volatile

I have some questions about the volatile keyword...

1) If I use the volatile keyword with a reference type such as a class like
so:

public volatile UserTotals totals = new UserTotals();

Are the fields of the class also protected? Or is it just the 4-8 byte
reference to the object that is protected?

2) Why doesn't volatile support 64bit and 128bit data types such as ulong or
decimal?

3) In the MSDN documentation, it says that all static members are
thread-safe. So why do code examples in the MSDN documentation use the
volatile keyword with static members?

Thanks


Nov 16 '05 #1
1 1607
Eric <ej********@yahoo.com> wrote:
I have some questions about the volatile keyword...

1) If I use the volatile keyword with a reference type such as a class like
so:

public volatile UserTotals totals = new UserTotals();

Are the fields of the class also protected? Or is it just the 4-8 byte
reference to the object that is protected?
It's only the reference, although volatile reads and writes involve
memory barriers which do have an effect on thread-safety in general.
Of course, if you do:

UserTotals foo = totals;
foo.x = 1;
foo.y = 2;

etc

then only one volatile read is involved.
2) Why doesn't volatile support 64bit and 128bit data types such as ulong or
decimal?
Presumably because this makes the CLR implementation significantly
harder or less efficient.
3) In the MSDN documentation, it says that all static members are
thread-safe. So why do code examples in the MSDN documentation use the
volatile keyword with static members?


Static members aren't *inherently* thread-safe - the MSDN docs are
saying that the class they're talking about has been designed to *be*
thread-safe. Using volatile variables is one of the tools you can use
to achieve thread safety.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

5
by: Harold Howe | last post by:
Howdy, 1- Are property reads in effect volatile? Or is the compiler allowed to optimize property reads away? ie class foo { public int bar { get { return m_bar;}
2
by: Jean-Marc Blaise | last post by:
Hi Folks, 1) Why do we have to commit so that statistics are taken into account : Insert into DGTT call ADMIN_CMD ('runstats ...') <<- If we don't commit here, explain plans show that stats...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
15
by: Marc Thrun | last post by:
Hello, I've got a few questions: 1) Given the two structs struct A { int x; }; and
17
by: djc | last post by:
I got great info on related question previously. This link <http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian Gideon was especially informative. 1) the lock statement does...
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...
9
by: aarklon | last post by:
1) why this program gives o/p as variable = 123 value = 0 does it mean that if a variable is not known the pre processor treats it as zero #include<stdlib.h> #include<stdio.h> #if var == 0...
2
by: Nick Keighley | last post by:
On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote: this is untrue. A conforming C compiler is not permitted to "optimize away" this code. It must translate it to something that has the...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.