473,405 Members | 2,445 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,405 software developers and data experts.

volatile - small question

I am right in thinking that if I have a class that can be accessed by
more than one thread in my application then I should mark *all*
instance variables within that class with the volatile keyword.
Also is it right that if the access to the variable is within a lock
then this is not required, since all 'memory'/caches are up to date

Thanks in advance
Nick
Nov 17 '05 #1
2 947

"Nick" <ni*********@hotmail.com> wrote in message
news:a0**************************@posting.google.c om...
I am right in thinking that if I have a class that can be accessed by
more than one thread in my application then I should mark *all*
instance variables within that class with the volatile keyword.
Also is it right that if the access to the variable is within a lock
then this is not required, since all 'memory'/caches are up to date

Thanks in advance
Nick

Use one of the synchronization primitives like Monitors, they have
acquire/release semantics. Use volatile only for special cases of
synchronization, f.i when you realy need to write thread-safe lock-free
code, IMO something which is very hard to be reliably done on the CLR.

Willy.

Nov 17 '05 #2
Nick <ni*********@hotmail.com> wrote:
I am right in thinking that if I have a class that can be accessed by
more than one thread in my application then I should mark *all*
instance variables within that class with the volatile keyword.
No. I find that volatile is very rarely the way to create thread-safe
classes.
Also is it right that if the access to the variable is within a lock
then this is not required, since all 'memory'/caches are up to date


Yes. Like Willy, I'd suggest using the synchronization primitives most
of the time.

See my article on threading at
http://www.pobox.com/~skeet/csharp/threads
for more details.

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

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

Similar topics

2
by: LBJ | last post by:
I have the following C code: //pHeadLoc contains an address that can be modified externally, //therefore it is a volatile pointer to a pointer volatile int** const pHeadLoc = (int**)0x456000; ...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
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...
16
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.