On Sun, Oct 28, 2007 at 09:47:36 -0400, Robert Dewar wrote: > Bart Van Assche wrote: > > >Requiring that all thread-shared variables should be declared > >volatile is completely unacceptable. > > Why is this unacceptable .. seems much better to me than writing > undefined stuff.
There's a parallel thread in the Linux Kernel Mailing List. Everyone is advised to read it, if not already. There are several good points there: - the problem is not limited to multithreaded domain: the page with the object could be made read-only during execution, thus if (! page_is_read_only) v = 1; would SIGSEGV for no apparent reason. - making things volatile is unacceptable from performance POV. - optimization in question might well turn out to be misoptimization for anything but microbenchmarks (read LKML for cache flush/dirty page issues). - "people knowledgeable in POSIX say that this optimization is bogus". I would add that though we may say that Standard C is not aware of threads, POSIX _is_ aware of Standard C. While POSIX failed to solve the issue by formal word, its intent is clear: to make POSIX Threads usable. The compiler that claims to be POSIX compatible should take this into account. - there's also a good talk on lawyer-ish vs attached-to-reality approach. I personally doubt those who continue to advise to use volatile are actually writing such multithreaded programs. Most argue just for the fun of it. > Well Hans is talking about C/C++, you are talking about some other > language in which programs which do not have well defined semantics > in C or C++ do have well defined semantics in your language. Good thing we have this _bug_ in languages that define memory semantics (Ada, Java), and no one yet argues that GCC should be fixed wrt to only those languages. -- Tomash Brechko