Click here to Skip to main content
15,906,625 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
George_George wrote:
and I think the optimization provided by compiler is wrong -- causing wrong function in a multi-threaded environment. Do you have any comments?


The optimization is not wrong. The C++ Standard says the compiler may do that*), so you have to live with this.

C++ as a language does not understand anything about threads. So for the compiler, changing Sentinel in a separate thread is like changing it secretly behind its back. volatile is the warning to the compiler "be careful! this value may change behind your back!"

A bug in your source code that appears only under (agressive) optimizations is actually quite frequent. There are a lot of subtle pitfalls in the C++ standard that code that looks good and works well in an unoptimized build breaks under the otimizer. (my recent post in the "Subtle Bugs" forum is a good example).

You have to differentiate three things: What you think is correct, what your compiler should or may do according to the standard, and what your compiler actually does. In most cases, you are wrong - not the compiler Smile | :)
I've seen maybe hundreds of posts of people claiming to "have found a compiler bug", virtually always it was a bug in their source code, due to ignorance or misunderstanding of the C++ Language Standard.

George_George wrote:
I think it will bring high risks to careless developers, who does not know about volatile and forget to put it ahead of the variable, which will result in the wrong optimization of compiler

Absolutely. It would be good if C++ had better sematincs for multithreaded applications - but it doesn't have. C++ is not a simple language - not because of its syntax, but because of things like these. Multithreading is naturally not simple. C++ is not a language for careless programmers Big Grin | :-D (and I think there is no language dedicated to them)


*) rather, it doesn't say the compiler may not do that...



[edit] I want to add that right now, no compiler implements the C++ standard perfectly - so there is always a discrepancy between what the standard says and the compiler does. Still, with most "errors", it is not the compilers fault. But too, just because your compiler accepts it doesn't mean it's legal.

Comeau[^] is known as most compliant, they also have a web applet where you can test short code snippets.

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


modified on Friday, December 28, 2007 5:28:27 PM

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 
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 

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.