Click here to Skip to main content
15,912,400 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Yes. Pin
CPallini28-Dec-07 22:50
mveCPallini28-Dec-07 22:50 
GeneralRe: Yes. Pin
George_George28-Dec-07 23:08
George_George28-Dec-07 23:08 
GeneralRe: Yes. Pin
CPallini28-Dec-07 23:45
mveCPallini28-Dec-07 23:45 
GeneralRe: Yes. Pin
George_George29-Dec-07 0:00
George_George29-Dec-07 0:00 
GeneralRe: Yes. Pin
Maxwell Chen28-Dec-07 4:25
Maxwell Chen28-Dec-07 4:25 
GeneralRe: Yes. Pin
JudyL_MD28-Dec-07 12:21
JudyL_MD28-Dec-07 12:21 
GeneralRe: Yes. Pin
George_George28-Dec-07 18:17
George_George28-Dec-07 18:17 
GeneralRe: Yes. [modified] Pin
JudyL_MD28-Dec-07 20:01
JudyL_MD28-Dec-07 20:01 
George_George wrote:
In your cross-platform development experience, I think you mean if we are not using volatile, the function of the program will change in your experience?


yes, it happened to me in a VS2008 release mode version of a multi-threaded program

George_George wrote:
Could you show a more specific example please? I am interested in this since in my experience I have not found any case which behaves differently with/without volatile


Sorry, I can't show you the specific chunk of code since it is proprietary, but the use of the volatile variable was very simple. A class created a worker thread during its processing. The worker thread was encapsulated in a class that contained a public boolean variable that was set to false in the constructor **. At some further point in its processing, independent of the thread, the class would change the value of the boolean (using its pointer to the thread class it had created) from false to true, indicating that the thread should stop what it was doing. The thread contained an infinite loop that checked the value of the boolean during its loop. When the boolean went true, the thread would terminate.

Without the use of volatile in the definition of the boolean within the thread class, the thread never terminated in release mode. When the code ran in debug mode, it worked correctly. When I examined the assembly language generated in release mode, I found that the compiler had optimized away the check of the boolean in the thread's loop.

If you are going to modify a variable used between threads, you should use volatile for safety's sake. Note that when I used Windows exclusively, I always used events for a simple "value change" flag between threads because it was guaranteed to be thread-safe. My worker threads don't usually directly affect data used by the initiating thread - they tend to communicate via windows messages (or the equivalent on other platforms) and pass data that way.

Your statement in other messages in this thread that you can omit the volatile keyword is not correct. Depending on what the optimizing compiler does, it can make a difference and this is the worst kind of bug to try and find. It all depends on what the optimizer does which changes every time you change the code.

Most developers don't encounter this kind of bug since this is not the best or safest way to share data between threads. I only encountered it when I started to "cheat" in my code because I didn't want to have to write platform-specific code every time I wanted to simply signal a thread.

Judy

** < edit >Thinking about it some more (since I don't have the code here with me at home), the variable was initializaed at the begining of the Run function which contains the infinite loop, not the constructor. Big difference since this way the optimizer sees the variable set to false and never changed, so therefore takes away the check of the variable inside the loop. < /edit >

modified on Saturday, December 29, 2007 2:42:20 AM

GeneralRe: Yes. Pin
George_George28-Dec-07 23:01
George_George28-Dec-07 23:01 
GeneralRe: Yes. Pin
JudyL_MD29-Dec-07 4:40
JudyL_MD29-Dec-07 4:40 
GeneralRe: Yes. Pin
George_George29-Dec-07 19:39
George_George29-Dec-07 19:39 
GeneralRe: Yes. Pin
JudyL_MD30-Dec-07 4:37
JudyL_MD30-Dec-07 4:37 
GeneralRe: Yes. Pin
George_George31-Dec-07 4:01
George_George31-Dec-07 4:01 
GeneralRe: Yes. Pin
JudyL_MD31-Dec-07 4:30
JudyL_MD31-Dec-07 4:30 
GeneralRe: Yes. Pin
George_George31-Dec-07 5:17
George_George31-Dec-07 5:17 
GeneralRe: Yes. Pin
George_George28-Dec-07 18:19
George_George28-Dec-07 18:19 
GeneralRe: Yes. Pin
David Crow28-Dec-07 4:58
David Crow28-Dec-07 4:58 
GeneralRe: Yes. Pin
George_George28-Dec-07 18:17
George_George28-Dec-07 18:17 
GeneralRe: MSDN volatile sample Pin
peterchen28-Dec-07 2:47
peterchen28-Dec-07 2:47 
GeneralRe: MSDN volatile sample Pin
George_George28-Dec-07 3:20
George_George28-Dec-07 3:20 
GeneralRe: MSDN volatile sample [modified] Pin
peterchen28-Dec-07 4:21
peterchen28-Dec-07 4:21 
GeneralRe: MSDN volatile sample Pin
Maxwell Chen28-Dec-07 4:31
Maxwell Chen28-Dec-07 4:31 
GeneralRe: MSDN volatile sample Pin
peterchen28-Dec-07 4:32
peterchen28-Dec-07 4:32 
GeneralRe: MSDN volatile sample Pin
Maxwell Chen28-Dec-07 4:34
Maxwell Chen28-Dec-07 4:34 
GeneralRe: MSDN volatile sample Pin
George_George28-Dec-07 18:37
George_George28-Dec-07 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.