volatile variable

Hello,

Suppose I have a shared data:

volatile int shared;

By using “volatile” I am supressing compiler
optimization to keep “shared” in a register and
potentially become stale across multiple threads of
execution. I should use volatile because the compiler
knows nothing about separate threads of execution. Now
I am starting to have doubts about multi-threaded
applications that I have written and that others have
written using say pthreads because “volatile” was not
used for cases like variable “shared” above. I must
use “volatile” for such kind of shared data as
“shared” above in a Pthread application, yes?

Regards, Vasili


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Galchin Vasili wrote:

Hello,

Suppose I have a shared data:

volatile int shared;

By using “volatile” I am supressing compiler
optimization to keep “shared” in a register and
potentially become stale across multiple threads of
execution. I should use volatile because the compiler
knows nothing about separate threads of execution. Now
I am starting to have doubts about multi-threaded
applications that I have written and that others have
written using say pthreads because “volatile” was not
used for cases like variable “shared” above. I must
use “volatile” for such kind of shared data as
“shared” above in a Pthread application, yes?

“volatile” tells the compiler that the variable might change at any
time. As you suggest, it will prevent the compiler from caching the
value in a register or a temporary. However, it does NOT do any kind of
locking, so it sometimes gives a false sense of security.

In fact, volatile is much more useful for hardware registers than for
shared data in multithreaded apps. In a multithreaded app, if you need
to have the absolute current stable value for the variable, you probably
want to grab the lock anyway to prevent it from changing. If you don’t
need the current stable value, or if the value changes only rarely, then
even the volatile may be overkill. It depends on the situation.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

‘Volatile’ is NOT for SMP systems. It is for external hardware or different
programs. A memory mapped hardware device is the first example. The second
is for shared memory between a driver and a service or application. This
tells the compiler to keep the data in the location where it was placed and
always reference it to determine value. The compiler can take a variable
and place it in a register during loop processing and not look at the
variable when it is being checked. If another thread can change it or as in
the days of DOS it was the time variable in the BIOS data area.

“Galchin Vasili” wrote in message news:xxxxx@ntdev…
> Hello,
>
> Suppose I have a shared data:
>
> volatile int shared;
>
> By using “volatile” I am supressing compiler
> optimization to keep “shared” in a register and
> potentially become stale across multiple threads of
> execution. I should use volatile because the compiler
> knows nothing about separate threads of execution. Now
> I am starting to have doubts about multi-threaded
> applications that I have written and that others have
> written using say pthreads because “volatile” was not
> used for cases like variable “shared” above. I must
> use “volatile” for such kind of shared data as
> “shared” above in a Pthread application, yes?
>
> Regards, Vasili
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>

I have to take exception with the statement “‘Volatile’ is NOT for SMP
systems”, actually it is (at least when the C standard was drafted).
Volatile is a way to guarantee access from a memory location for the
variable, so for instance developing your own spinlock like construct needs
volatile.

The reason we do not see it used extensively in SMP programs, is that the
language also requires synchronization of variables with their memory at
some points. One of those points is a function call, and since we typically
call a lock function before accessing shared data, and an unlock at the end
of the access, volatile is not needed in this case.

I may have the terms slightly wrong, my copy of the C standard is buried at
the moment. So you are trusting the memories of a compiler guy.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“David J. Craig” wrote in message
news:xxxxx@ntdev…
> ‘Volatile’ is NOT for SMP systems. It is for external hardware or
> different programs. A memory mapped hardware device is the first example.
> The second is for shared memory between a driver and a service or
> application. This tells the compiler to keep the data in the location
> where it was placed and always reference it to determine value. The
> compiler can take a variable and place it in a register during loop
> processing and not look at the variable when it is being checked. If
> another thread can change it or as in the days of DOS it was the time
> variable in the BIOS data area.
>

I will agree that the data item(s) in a spinlock should be declared
volatile. However just using a volatile data item won’t protect it from
multiple processors. It will at least force both processors to really
access the real memory, which is needed, but all that stuff has been written
for us in the driver world. We have the spinlock routines that work.

“Don Burn” wrote in message news:xxxxx@ntdev…
>I have to take exception with the statement “‘Volatile’ is NOT for SMP
>systems”, actually it is (at least when the C standard was drafted).
>Volatile is a way to guarantee access from a memory location for the
>variable, so for instance developing your own spinlock like construct needs
>volatile.
>
> The reason we do not see it used extensively in SMP programs, is that the
> language also requires synchronization of variables with their memory at
> some points. One of those points is a function call, and since we
> typically call a lock function before accessing shared data, and an unlock
> at the end of the access, volatile is not needed in this case.
>
> I may have the terms slightly wrong, my copy of the C standard is buried
> at the moment. So you are trusting the memories of a compiler guy.
>
>
> –
> Don Burn (MVP, Windows DDK)
> Windows 2k/XP/2k3 Filesystem and Driver Consulting
> Remove StopSpam from the email to reply
>
>
>
> “David J. Craig” wrote in message
> news:xxxxx@ntdev…
>> ‘Volatile’ is NOT for SMP systems. It is for external hardware or
>> different programs. A memory mapped hardware device is the first
>> example. The second is for shared memory between a driver and a service
>> or application. This tells the compiler to keep the data in the location
>> where it was placed and always reference it to determine value. The
>> compiler can take a variable and place it in a register during loop
>> processing and not look at the variable when it is being checked. If
>> another thread can change it or as in the days of DOS it was the time
>> variable in the BIOS data area.
>>
>
>
>

Some notes on ‘volatile’

* My copy of K&R, which was printed in 1978, does not make reference to the
‘volatile’ keyword.

* My Microsft C Reference Manual, which was printed in 1991, says this about
‘volatile’:

“The ‘volatile’ type qualifier declares an item whose value can legitimately
be changed beyond the control of the program in which it appears, such as a
currently executing thread”

* The ‘const’ type qualifier is at the complete opposite end of the
spectrum. The two can be used together in this sort of construct (these are
rather famous examples, so I do not take credit for them):

typedef int *t
const t *volatile p;

and even this:

t const *volatile p;

* Here is a quote from some IBM documentation describing the ‘volatile’ type
qualifier:

“The volatile qualifier declares a data object that can have its value
changed in ways outside the control or detection of the compiler (such as a
variable updated by the system clock).”

* Type qualifiers (‘const’ and ‘volatile’) were introduced in the language
with ANSI C.

Jamey

----- Original Message -----
From: “Don Burn”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Tuesday, August 02, 2005 5:35 AM
Subject: Re:[ntdev] volatile variable

I have to take exception with the statement “‘Volatile’ is NOT for SMP
systems”, actually it is (at least when the C standard was drafted).
Volatile is a way to guarantee access from a memory location for the
variable, so for instance developing your own spinlock like construct needs
volatile.

The reason we do not see it used extensively in SMP programs, is that the
language also requires synchronization of variables with their memory at
some points. One of those points is a function call, and since we typically
call a lock function before accessing shared data, and an unlock at the end
of the access, volatile is not needed in this case.

I may have the terms slightly wrong, my copy of the C standard is buried at
the moment. So you are trusting the memories of a compiler guy.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“David J. Craig” wrote in message
news:xxxxx@ntdev…
> ‘Volatile’ is NOT for SMP systems. It is for external hardware or
> different programs. A memory mapped hardware device is the first example.
> The second is for shared memory between a driver and a service or
> application. This tells the compiler to keep the data in the location
> where it was placed and always reference it to determine value. The
> compiler can take a variable and place it in a register during loop
> processing and not look at the variable when it is being checked. If
> another thread can change it or as in the days of DOS it was the time
> variable in the BIOS data area.
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@rocketdivision.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Jamey,

It was not part of K&R but put in by the ANSI standard, I wasn’t on
the committee but did monitor it, there was a lot of effort to handle the
needs of things like multi-threading and hardware.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Jamey Kirby” wrote in message
news:xxxxx@ntdev…
> Some notes on ‘volatile’
> ------------------------
>
> * My copy of K&R, which was printed in 1978, does not make reference to
> the
> ‘volatile’ keyword.
>
> * My Microsft C Reference Manual, which was printed in 1991, says this
> about
> ‘volatile’:
>
> “The ‘volatile’ type qualifier declares an item whose value can
> legitimately
> be changed beyond the control of the program in which it appears, such as
> a
> currently executing thread”
>
> * The ‘const’ type qualifier is at the complete opposite end of the
> spectrum. The two can be used together in this sort of construct (these
> are
> rather famous examples, so I do not take credit for them):
>
> typedef int *t
> const t *volatile p;
>
> and even this:
>
> t const *volatile p;
>
> * Here is a quote from some IBM documentation describing the ‘volatile’
> type
> qualifier:
>
> “The volatile qualifier declares a data object that can have its value
> changed in ways outside the control or detection of the compiler (such as
> a
> variable updated by the system clock).”
>
> * Type qualifiers (‘const’ and ‘volatile’) were introduced in the language
> with ANSI C.
>
> Jamey
>
>
> ----- Original Message -----
> From: “Don Burn”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Tuesday, August 02, 2005 5:35 AM
> Subject: Re:[ntdev] volatile variable
>
>
> I have to take exception with the statement “‘Volatile’ is NOT for SMP
> systems”, actually it is (at least when the C standard was drafted).
> Volatile is a way to guarantee access from a memory location for the
> variable, so for instance developing your own spinlock like construct
> needs
> volatile.
>
> The reason we do not see it used extensively in SMP programs, is that the
> language also requires synchronization of variables with their memory at
> some points. One of those points is a function call, and since we
> typically
> call a lock function before accessing shared data, and an unlock at the
> end
> of the access, volatile is not needed in this case.
>
> I may have the terms slightly wrong, my copy of the C standard is buried
> at
> the moment. So you are trusting the memories of a compiler guy.
>
>
> –
> Don Burn (MVP, Windows DDK)
> Windows 2k/XP/2k3 Filesystem and Driver Consulting
> Remove StopSpam from the email to reply
>
>
>
> “David J. Craig” wrote in message
> news:xxxxx@ntdev…
>> ‘Volatile’ is NOT for SMP systems. It is for external hardware or
>> different programs. A memory mapped hardware device is the first
>> example.
>> The second is for shared memory between a driver and a service or
>> application. This tells the compiler to keep the data in the location
>> where it was placed and always reference it to determine value. The
>> compiler can take a variable and place it in a register during loop
>> processing and not look at the variable when it is being checked. If
>> another thread can change it or as in the days of DOS it was the time
>> variable in the BIOS data area.
>>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@rocketdivision.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

> * My copy of K&R, which was printed in 1978, does not make
reference to the

‘volatile’ keyword.

No “volatile” in K&R, also no “const” - these are ANSI C stuff.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

Funny, it isn’t listed in my Whitesmith C manual here either. Of
course, it also notes that global static variables are uninitialized…

(This is an attempt at humor. If it wasn’t actually funny, please
disregard. But I had to install one too many Active X components
today…)

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Tuesday, August 02, 2005 5:16 PM
To: ntdev redirect
Subject: Re: Re:[ntdev] volatile variable

* My copy of K&R, which was printed in 1978, does not make
reference to the
‘volatile’ keyword.

No “volatile” in K&R, also no “const” - these are ANSI C stuff.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@osr.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

> disregard. But I had to install one too many Active X components

In other words, one? :slight_smile:

“Tony Mason” wrote in message news:xxxxx@ntdev…
Funny, it isn’t listed in my Whitesmith C manual here either. Of
course, it also notes that global static variables are uninitialized…

(This is an attempt at humor. If it wasn’t actually funny, please
disregard. But I had to install one too many Active X components
today…)

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Tuesday, August 02, 2005 5:16 PM
To: ntdev redirect
Subject: Re: Re:[ntdev] volatile variable

> * My copy of K&R, which was printed in 1978, does not make
reference to the
> ‘volatile’ keyword.

No “volatile” in K&R, also no “const” - these are ANSI C stuff.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@osr.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Jamey - Off topic but … have you ever looked up recursion in the index of
your K&R? :slight_smile:

“Jamey Kirby” wrote in message
news:xxxxx@ntdev…
> Some notes on ‘volatile’
> ------------------------
>
> * My copy of K&R, which was printed in 1978, does not make reference to
> the
> ‘volatile’ keyword.
>
> * My Microsft C Reference Manual, which was printed in 1991, says this
> about
> ‘volatile’:
>
> “The ‘volatile’ type qualifier declares an item whose value can
> legitimately
> be changed beyond the control of the program in which it appears, such as
> a
> currently executing thread”
>
> * The ‘const’ type qualifier is at the complete opposite end of the
> spectrum. The two can be used together in this sort of construct (these
> are
> rather famous examples, so I do not take credit for them):
>
> typedef int *t
> const t *volatile p;
>
> and even this:
>
> t const *volatile p;
>
> * Here is a quote from some IBM documentation describing the ‘volatile’
> type
> qualifier:
>
> “The volatile qualifier declares a data object that can have its value
> changed in ways outside the control or detection of the compiler (such as
> a
> variable updated by the system clock).”
>
> * Type qualifiers (‘const’ and ‘volatile’) were introduced in the language
> with ANSI C.
>
> Jamey
>
>
> ----- Original Message -----
> From: “Don Burn”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Tuesday, August 02, 2005 5:35 AM
> Subject: Re:[ntdev] volatile variable
>
>
> I have to take exception with the statement “‘Volatile’ is NOT for SMP
> systems”, actually it is (at least when the C standard was drafted).
> Volatile is a way to guarantee access from a memory location for the
> variable, so for instance developing your own spinlock like construct
> needs
> volatile.
>
> The reason we do not see it used extensively in SMP programs, is that the
> language also requires synchronization of variables with their memory at
> some points. One of those points is a function call, and since we
> typically
> call a lock function before accessing shared data, and an unlock at the
> end
> of the access, volatile is not needed in this case.
>
> I may have the terms slightly wrong, my copy of the C standard is buried
> at
> the moment. So you are trusting the memories of a compiler guy.
>
>
> –
> Don Burn (MVP, Windows DDK)
> Windows 2k/XP/2k3 Filesystem and Driver Consulting
> Remove StopSpam from the email to reply
>
>
>
> “David J. Craig” wrote in message
> news:xxxxx@ntdev…
>> ‘Volatile’ is NOT for SMP systems. It is for external hardware or
>> different programs. A memory mapped hardware device is the first
>> example.
>> The second is for shared memory between a driver and a service or
>> application. This tells the compiler to keep the data in the location
>> where it was placed and always reference it to determine value. The
>> compiler can take a variable and place it in a register during loop
>> processing and not look at the variable when it is being checked. If
>> another thread can change it or as in the days of DOS it was the time
>> variable in the BIOS data area.
>>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@rocketdivision.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

My 1988 copy of K&R (2nd Edition, ANSI C) includes “volatile” ,
and on page 211 it says “The const and volatile properties are
new with the ANSI standard… The purpose of volatile is to
force an implementation to suppress optimization that could
otherwise occur.”

Alberto.

----- Original Message -----
From: “Tony Mason”
To: “Windows System Software Devs Interest List”

Sent: Tuesday, August 02, 2005 5:38 PM
Subject: RE: Re:[ntdev] volatile variable

Funny, it isn’t listed in my Whitesmith C manual here either.
Of
course, it also notes that global static variables are
uninitialized…

(This is an attempt at humor. If it wasn’t actually funny,
please
disregard. But I had to install one too many Active X
components
today…)

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S.
Shatskih
Sent: Tuesday, August 02, 2005 5:16 PM
To: ntdev redirect
Subject: Re: Re:[ntdev] volatile variable

> * My copy of K&R, which was printed in 1978, does not make
reference to the
> ‘volatile’ keyword.

No “volatile” in K&R, also no “const” - these are ANSI C stuff.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@osr.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag
argument: ‘’
To unsubscribe send a blank email to
xxxxx@lists.osr.com