Subj : Re: an interesting threading-question To : comp.programming.threads From : Sean Kelly Date : Wed Sep 14 2005 08:28 pm Oliver S. wrote: > I've got a question regarding threading with C/C++: > When I call a function which is out of scope for the compiler turn > (f.e. in another module without global optimization or a OS-function), > the compiler would have to flush all preceding writes and execute all > preceding reads to non-local variables (and local variables which have > been passed as references to "unknown" functions as well) to ensure > that these variables can be seen by the called function and that pre- > ceding reads wouldn't read data which could have been changed by the > called function - and this is usually done by a combined read/write > -barrier (and this is the same for following reads/writes and thus, > explicit barriers are often not necessary). But this is only necessary > for the executing thread to become self-consistent and not to become > consistent with other threads. So theroretically this barriers could > affect only the self-consitency and there could be further mechanisms > which would help to become globally consistent. So soes anyone know > an architecture which has two consistency-levels on account of this? An optimizer will not perform optimizations that alter the visible effects of a program (assuming a single-threaded program model). You can think of the CPU as a hardware-level optimizer and make the same assertion: a CPU will not perform optimizations that alter the visible effects of a program (assuming the same single-threaded model). Thus, the only time either becomes an issue is in multithreaded programs, and then only if your program involves asynchronous accesses to shared data. In this case, you typically have to ensure that both the optimizer and the CPU--as observed from another thread--order accesses in a way that does not violate program integrity. At the hardware level, this ordering is accomplished via memory barriers. But it is important to note that none of this matters for self-consistency, as I am not aware of any architecture that requires the programmer to make such an effort, nor do I think that anything could be gained by doing so. Sean .