Re: Volatile for shared data protected by critical section
- From: "Dean Ramsier" <ramsiernospam@xxxxxxxxxx>
- Date: Fri, 25 Sep 2009 19:21:36 -0400
Interesting, I hadn't really thought much about this. Other than variables
used for hard spin loops and register accesses, I don't recall seeing
'volatile' used much with globals in the CE code base.
Does the current compiler implementation not attempt to optimize variables
across function calls, or are we just getting lucky because the call to
EnterCriticalSection happens to consume all the registers that could be used
for optimization?
--
Dean Ramsier - eMVP
BSQUARE Corporation
"Paul G. Tobey [eMVP]" <paultobey _at_ earthlink _dot_ net> wrote in message
news:9395D70D-BEDE-466C-BC91-AFF560B83C2E@xxxxxxxxxxxxxxxx
It should be declared volatile, regardless of whether you've properly
protected it or not. Volatile is a hint to the compiler that it should
not
assume that some previously cached (usually in a processor register),
value
will still be valid just because the compiler doesn't see any code in the
current block that will change the value. While it's doubtful that this
would ever cause you a problem if you are enclosing all code that accesses
the variable in a critical section, future enhancements to global
optimization in the compiler might note that you are not changing that
variable value in the code block and decide that it should be cached and
only
the cached value used.
Paul T.
"shiju" wrote:
Thanks Bruce for your response.
You are right that the global data will be shared by multiple threads
but these data are protected within a critical section . But not sure
whether the global data that is protected within a critical section
needs to be declared as volatile. In some OS like linux, it says that
don't use volatile if the global data is already protected with
spin_locks etc.
(http://www.mjmwired.net/kernel/Documentation/volatile-considered-harmful.txt).
I don't see any such info for wince or windows.
Thanks,
Shiju
Bruce Eitman [eMVP] wrote:
I am not sure that I followed you completely. But, if the global data
will
be shared by multiple threads then you should use volatile. This
ensures
that the compiler knows that the data could be modified by another
thread.
It would also tell the compiler that writes must be done before the
function
call since the compiler has no idea what is being done in the critical
section functions.
.
- Follow-Ups:
- Re: Volatile for shared data protected by critical section
- From: Paul G. Tobey [eMVP]
- Re: Volatile for shared data protected by critical section
- References:
- Volatile for shared data protected by critical section
- From: shiju
- Re: Volatile for shared data protected by critical section
- From: Bruce Eitman [eMVP]
- Re: Volatile for shared data protected by critical section
- From: shiju
- Re: Volatile for shared data protected by critical section
- From: Paul G. Tobey [eMVP]
- Volatile for shared data protected by critical section
- Prev by Date: Re: What is the procedure to find out the type of File System used in my product
- Next by Date: Re: Context switch in Windows CE 6
- Previous by thread: Re: Volatile for shared data protected by critical section
- Next by thread: Re: Volatile for shared data protected by critical section
- Index(es):
Relevant Pages
|