Re: MSDN volatile sample
George schrieb:
In the MSDN volatile sample,
http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx
I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)
THe result may be the same with your compiler version and optimizer setting, but
is is not guaranteed to work without volatile. volatile tells the compiler not
to optimize the use of the variable because it could be modified at any time by
something the compiler has no control over.
Code with a "forgotten volatile" at the right place can work for years but fail
unexpected after minor unrelated code changes.
Norbert
.
Relevant Pages
- Re: Is the following code MT-Safe?
... the assert (which is a fundamental design error: ... and almost always leads to either major synchronization failures ... >Does _bRunning need to be tagged as volatile? ... compiler to cache values, but only during the execution of a function; ... (microsoft.public.vc.mfc) - Re: Volatile + multithreading
... The volatile keyword has little use in multithreaded programming. ... > field or global seems to have no impact because it seems the compiler will ... operations as "special" and suppress optimizations around them. ... > majority of the multithreading issues that could have resulted from ... (microsoft.public.vc.language) - Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures
... If I could trust atomic_read/atomic_setto cause the compiler ... as you've explained it below) -- neither w.r.t. CPU re-ordering (which ... You are correct about CPU re-ordering (and about the fact that this ... The compiler is prohibited from moving a volatile access across a sequence ... (Linux-Kernel) - Re: Is the following code MT-Safe?
... >>Explanation follows code example, MFC synchronization objects used ... > the first thread to resume, which then takes the assert. ... > volatile is unrelated to synchronization. ... > compiler to cache values, but only during the execution of a function; ... (microsoft.public.vc.mfc) - Re: Share .cpp and .h along projects
... pvector = InterlockedExchangePointerAcquire(&g_sharedVector, ... I really would like to hear what you think volatile accomplishes ... You can't require people to use volatile on top of synchronization. ... compiler useful for multithreaded programming. ... (microsoft.public.vc.language) |
|