@nwp You're right, but there's a little more to it than that. The specification of UB comes later, in subclause 21.2: "The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below. Any such data race results in undefined behavior." No write->no conflict. No conflict->no UB.
Is it legal that there is a :: in the variable name?
Sorry for disturbing you, guys. I understand it now. The key is static const Cat Tiger, Lion, Cheetah; . The Lion is a static member variable for class named Cat. So Cat::Lion is legal.
How could I make a dependency in a makefile refer to the name of the target? For example for helloworld : helloworld.cpp, I want something like helloworld : [email protected], but doing that evaluates the $@ to nothing
@JerryCoffin I could pull the "But they were talking C and not C++ card" but I won't, I get what y'all are saying. In my personal experience that's been dubious. If the compiler detects it can be written to at all it seems to trigger the UB or just maybe the implementation doesn't care to sync the caches. Either way the effect is the same.
I don't think there is a contradiction. If the compiler can see a write at potentially the same time as a read then it's UB. There is no need to sync caches, unless primitives like mutexes require it to do so, but the compiler is more or less explicitly told when to do so.
I mean in theory the scenario presented the write should happen prior to thread start. Assuming (this being c) they used thrd_create then IIRC the standard does require its execution state to be synced.
I thought so too, but now that I think about it I don't see the point. The new thread is new and thus cannot have different values in cache. Or maybe it's run on a physical core which has a cache of whatever ran before which now needs to be synced. That's probably it.
I vaguely remember thread creation to be a synchronization primitive like mutex locking.
On x86 it probably doesn't make a difference anyway and I have never messed around on ARMs.
@nwp Then again, thread switching is not something the compiler needs to care about.
@nwp I've actually run into the issue on x86 ironically. Windows only so it could be an MS thing. In all fairness it was a CreateThread thread and not a std::thread or thrd_create (which AFAIK MS doesn't support yet) thread.
@Mgetz Oh--I missed that it was C rather than C++, or I'd have stayed out of it. In my opinion, about the only reason to write new C code is for things like device drivers or embedded code that deals directly with hardware (and even the latter is questionable). For application code, I consider C at least as obsolete as COBOL...
@Mgetz I rather doubt that it does. But I haven't tried to keep up on the C standard for years, so I'd barely know where to start looking for quotes about what's defined (or not) about threads in C (other than that I'd have to look in a newer standard than I have right now).