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

C / C++ / MFC

 
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 
GeneralRe: MSDN volatile sample Pin
peterchen29-Dec-07 10:51
peterchen29-Dec-07 10:51 
2. Almost, yes Wink | ;)

My key point is: The optumization is correct, and knowing this is a vital part of being a good programmer.

(and that's a very important thing to learn)




1. What follows is a technical explanation, that is possibly valid for your compiler and your platform. The same behavior could be caused by other effects on your platform. Other compilers may react differently and may or may not expose similar results.

The compiler sees the following loop:
while(Sentinel==0)
 Sleep(0);


The optimizer loads the contents of the memory location to a CPU register. Since it sees no location where Sentinel changes (and doesn't need the CPU register for something else). In each loop iteration, it keeps on checking the CPU register, thinking "no, Sentinel can't have changed, and I have the value still in a register, so I check that register". But the register value never changes.

A similar effect can occur with dual core CPU's where each core has it's own CPU cache - the compiler has to emit special code to tell the CPU "..and make sure you synchronize all caches"



For an interesting read how complex these things can get, try the The "Double Checked Locking Pattern is Broken" declaration[^]

We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!| FoldWithUs! | sighist


GeneralRe: MSDN volatile sample Pin
George_George29-Dec-07 19:51
George_George29-Dec-07 19:51 
GeneralRe: MSDN volatile sample Pin
peterchen30-Dec-07 0:22
peterchen30-Dec-07 0:22 
GeneralRe: MSDN volatile sample Pin
George_George30-Dec-07 0:39
George_George30-Dec-07 0:39 
GeneralRe: MSDN volatile sample Pin
peterchen30-Dec-07 2:19
peterchen30-Dec-07 2:19 
GeneralRe: MSDN volatile sample Pin
George_George30-Dec-07 2:48
George_George30-Dec-07 2:48 
GeneralRe: MSDN volatile sample Pin
peterchen30-Dec-07 3:47
peterchen30-Dec-07 3:47 
GeneralRe: MSDN volatile sample Pin
George_George31-Dec-07 3:48
George_George31-Dec-07 3:48 
QuestionI define a template function in header, code in cpp. Include header. Undefined reference?? Pin
ArmchairAthlete27-Dec-07 21:47
ArmchairAthlete27-Dec-07 21:47 
AnswerRe: I define a template function in header, code in cpp. Include header. Undefined reference?? Pin
ArmchairAthlete27-Dec-07 21:48
ArmchairAthlete27-Dec-07 21:48 
GeneralRe: I define a template function in header, code in cpp. Include header. Undefined reference?? Pin
CPallini28-Dec-07 1:03
mveCPallini28-Dec-07 1:03 
QuestionRe: I define a template function in header, code in cpp. Include header. Undefined reference?? Pin
David Crow28-Dec-07 2:40
David Crow28-Dec-07 2:40 
QuestionI want to write C program about CPU Scheduling simmulator Pin
Smith1927-Dec-07 21:40
Smith1927-Dec-07 21:40 
GeneralRe: I want to write C program about CPU Scheduling simmulator Pin
Garth J Lancaster27-Dec-07 23:32
professionalGarth J Lancaster27-Dec-07 23:32 
GeneralTrayIcon RightClick - Enable and Disable Pin
Paulraj G27-Dec-07 18:24
Paulraj G27-Dec-07 18:24 
GeneralRe: TrayIcon RightClick - Enable and Disable Pin
Nishad S27-Dec-07 19:52
Nishad S27-Dec-07 19:52 
GeneralProblem in writing template function inside class Pin
Naveen27-Dec-07 18:21
Naveen27-Dec-07 18:21 
GeneralRe: Problem in writing template function inside class [modified] Pin
Maxwell Chen27-Dec-07 18:47
Maxwell Chen27-Dec-07 18:47 

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.