Subj : Re: Memory visibility and synchronization using pipes To : comp.programming.threads From : David Hopwood Date : Wed Jan 19 2005 09:36 pm Joseph Seigh wrote: > On Sat, 15 Jan 2005 16:34:49 +0100, Alexander Terekhov > wrote: >> Marcin 'Qrczak' Kowalczyk wrote: >> [...] >> >>> Is it safe, or the change to the variable may not be seen by the other >> >> It is "unsafe". You have a data race. Undefined behavior. >> >>> thread? I guess write() and poll() are not memory barriers and this is >>> unsafe. >> >> Even if they do have memory barriers internally, compiler is >> not constrained and can reorder you accesses across write() >> and poll(). They both are standard functions and they are NOT >> listed in the XBD 4.10. > > Technically, all threaded programming in C is undefined. That includes > Posix pthreads since it is incomplete with respect to thread behavior > and it has no formal definition of its semantics. Yes, but the OP asked what was "safe". Alexander's answer is a reasonable one, not just from a technical standards compliance point of view but also for writing real pthreads programs. The OP's program is not a borderline case that might be affected by the fine detail of a formal pthreads memory model; it clearly has undefined behaviour. It may sometimes be necessary to rely on implementation-specific behaviour, but you should know that you're doing that, not do it by accident. -- David Hopwood .