[Insight-developers] volatile keyword, C-C++ Users Journal Web Site

Miller, James V (Research) millerjv@crd.ge.com
Mon, 10 Feb 2003 10:50:23 -0500


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C2D117.C8F19C62
Content-Type: multipart/alternative;
	boundary="----_=_NextPart_001_01C2D117.C8F19C62"


------_=_NextPart_001_01C2D117.C8F19C62
Content-Type: text/plain;
	charset="iso-8859-1"

At the meeting last week, a question was raised as to  whether we should be using the volatile
keyword.  The volatile keyword should be used to keep the optimizer from caching a variable in  a
register.  This is important in threading applications where one thread is looking for the state of
the variable and another thread is setting the state of the variable (busy spin loops, etc.).

The volatile keywords acts like a modifier similar to const.  It can be used on a primitive type, on
a user defined type (and hence on a member function of a user defined type).  So like const, a
limited set of member functions can be accessed from a volatile object.

Should ITK use volatile?  This comes down to a question as to whether a given variable can be
accessed from multiple threads (where one thread may change the variable). For threading within a
filter, ITK uses a model where each thread is responsible for a different portion of the output data.
So only one thread will write to a particular block of memory. So the bulk data should be fine. When
values need to be calculated in a filter then accessed in all threads, the pipeline design provides
the BeforeThreadedGenerateData() method for calculating these values.  Each thread can then access
the values without modifying it. So the volatile keyword should not be needed.
 
For threading outside a filter (at the application level), there is an assumption in ITK that the
same object is not being used across mulltiple threads. So again, the volatile keyword should not be
needed.
 
For more "global" values like the modified time and the reference count on an object, these counts
are protected by mutexes.  However, the article below indicates that it is good practise to combine
the volatile and mutex concepts. So maybe we should incorporate the volatile keyword for these items.
However, since we don't busy spin loop on these values and access to them is via series of function
calls (GetMTime() calls a method on an ivar of type TimeStamp), the optimizer would have to optimize
out code that is across several function calls.  This is pretty unlikely but it might be good to be
strictly proper.



This is good article that explains the keyword volatile.
  http://www.cuj.com/experts/1902/alexandr.htm <http://www.cuj.com/experts/1902/alexandr.htm>  


------_=_NextPart_001_01C2D117.C8F19C62
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE></TITLE>

<META content="MSHTML 6.00.2715.400" name=GENERATOR></HEAD>
<BODY>
<P><FONT size=2>At the meeting last week, a question was raised as 
to&nbsp;&nbsp;whether we should be using the&nbsp;volatile keyword.&nbsp; The 
volatile&nbsp;keyword should be used to keep the optimizer from caching a 
variable in&nbsp; a&nbsp;register.&nbsp; This is important in threading 
applications where one thread is looking for the state of the variable&nbsp;and 
another thread is setting the state of the variable (busy spin loops, 
etc.).</FONT></P>
<P><FONT size=2>The volatile keywords acts like a modifier similar to 
const.&nbsp;&nbsp;It can be&nbsp;used on a primitive type, on a user defined 
type (and hence on a member function of a user defined type).&nbsp; So like 
const, a limited set of member functions can be accessed from a volatile 
object.</FONT></P>
<DIV><FONT size=2>Should ITK use volatile?&nbsp; This comes down to a question 
as to whether a given variable can be accessed from multiple threads (where one 
thread may change the variable). For threading within a filter, ITK uses a model 
where each thread is responsible for a different portion of the output 
data.&nbsp; So only one thread will write to a particular block of 
memory.&nbsp;So the bulk data should be fine.&nbsp;When values need to be 
calculated in a filter then accessed in all threads, the pipeline design 
provides the BeforeThreadedGenerateData() method for calculating these 
values.&nbsp; Each thread can then access the values without modifying it. So 
the volatile keyword should not be needed.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>For threading outside a filter (at the application level), 
there is an assumption in ITK that the same object is not being used across 
mulltiple threads. So again, the volatile keyword should not be 
needed.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>For more "global" values like the modified time and the 
reference count on an object, these counts are protected by mutexes.&nbsp; 
However, the article below indicates that it is good practise to combine the 
volatile and mutex concepts. So maybe we should incorporate the volatile keyword 
for these items. However, since we don't busy spin loop on these values and 
access to them is via series of function calls (GetMTime() calls a method on an 
ivar of type TimeStamp), the optimizer would have to optimize out code that is 
across several function calls.&nbsp; This is pretty unlikely but it might be 
good to be strictly proper.</FONT></DIV>
<DIV><FONT size=2><BR></FONT></DIV>
<P><FONT size=2>This is good article that explains the keyword 
volatile.</FONT><FONT size=2><BR>&nbsp;<A 
href="http://www.cuj.com/experts/1902/alexandr.htm" 
target=_blank>http://www.cuj.com/experts/1902/alexandr.htm</A></FONT> 
</P></BODY></HTML>

------_=_NextPart_001_01C2D117.C8F19C62--

------_=_NextPart_000_01C2D117.C8F19C62
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

[Filename: C-C++ Users Journal Web Site.url, Content-Type: application/octet-stream]
This e-Mail had an attachment was removed in order to meet GE Security Guidelines.

------_=_NextPart_000_01C2D117.C8F19C62--