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

C / C++ / MFC

 
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 
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 
George_George wrote:
Actually, I do not see many people add volatile keyword to the stopped bool variable.


You've come very far in what seems a short time, George Smile | :)

Maybe I should distinguish two things here: From what you (and I) would like to have, the compiler is doing something wrong, agreed. But from the view of the language definition, the compiler is absolutely correct.

My point of this post: The compiler cannot help you as much as you now think it could.

There are several reasons:
Holding frequently used values in a register is very important for optimizations - otherwise the compiler could generate neither fast nor compact code.

If the compiler would blindly assume that "some thread may modify values", the following variables could nopt be held in registers:

* all variables not declared in the local function block
* all variables of which a reference / addres is passed to some function

That would pretty much kill many optimizations that are essential to efficient object-oriented code.
Furthermore, all access to these variables would require to force synchronization between core caches, pretty much killing the advantage of multiple cores.

So you can either opt for the compiler to make things easy for you, or the compiler giving you full control. C++ almost always opts for "full control, but you need to know what you are doing". (That's why C++ is usually compared to a ferrari)

Keep in mind that, even above loop can be exited legally. Sleep(0) might throw an exception when it is finished. It might terminate the program using exit(). Generally, the compiler doesn't know the source code of Sleep.




It isn't as bad as you think. You can split data access from separate threads into the following two cases:

access to complex data
that's most structs and classes. In this case, you need to use a lock (e.g. CRITICAL_SECTION, or Mutex) anyway, since the access to these is not atomic. Acquiring/Releasing the lock takes care of the ugly stuff that would be caused by multiple threads.

Note that the compiler - even if it were aware of threads - couldn't help you with ANYTHING in that case.

atomic reads & writes
that's usually byte-, word- or integer- sized access, when they are well-aligned. The Sentinel example falls into that category.

In this case, the lock is not strictly necessary. BUT we now have to handle all the processor and memory issues involved with multithreading.

For this case, C++ offers volatile, which is a bit of a crutch. Additionally, the Win32 API offers Interlocked routines that allow safe access to a well aligned 32 bit integer. When linked correctly, this emits minimal assembly instructions. Using Interlocked, the sample could be fixed like this:

replace Sentinel = n with InterlockedExchange(&Sentinel, n)
replace Sentinel++ with InterlockedIncrement(&Sentinel);
replace reading Sentinel with InterlockedExchangeAdd(&Sentinel, 0)


On 64 bit systems, you also have 64 bit Interlocked functions, as well as finer-controlled routines

Phew.

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_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 
GeneralRe: Problem in writing template function inside class Pin
Naveen27-Dec-07 19:16
Naveen27-Dec-07 19:16 
GeneralRe: Problem in writing template function inside class Pin
Sarath C27-Dec-07 20:11
Sarath C27-Dec-07 20:11 

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.