Newsgroups: microsoft.public.vc.language
From: "Doug Harrison [MVP]" <d...@mvps.org>
Date: Fri, 06 Feb 2004 12:35:59 -0600
Local: Fri, Feb 6 2004 1:35 pm
Subject: Re: SEH and standard C++ exceptions
ak wrote: So many misconceptions exist concerning the use of volatile with threads >when marking a variable volatile one indicates for >the compiler that maybe another thread would >update the variable in question so therefor do not >optimize away the variable. >the optimizer could otherwise sometimes drop the that it's probably best not to mention them in the same breath. :) In multithreaded programming, synchronization is far more important than volatile, which is neither sufficient nor necessary when all access to a variable is protected by a mutex. When you faithfully observe your locking protocol to access a variable, declaring that variable volatile merely slows down your code, and for class types, greatly interferes with use of the object, because no one (A.A. excepted :) declares member functions volatile. In addition, if your class type "X" contains a char* member "p" which points to a buffer you would like to be volatile, declaring an X object volatile doesn't apply to the buffer data at all, but just to p. So it's hard to imagine a valid use of volatile on objects of class type. What about fundamental types like int? I can think of four valid uses for 1. On amenable hardware like x86, this idiom can work, and it requires // The variable x is accessible to multiple threads, one or more of while (x) // Assuming everyone observes the locking protocol, this approach bool x = true; // As above, just non-volatile for (;;) } 2. You need volatile to avoid undefined behavior in certain signal handler and setjmp/longjmp scenarios. 3. You can use volatile, say, to declare a pointer to a volatile int, which 4. You can use volatile to prevent the compiler from optimizing away -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||