3 ms·
?? You say that it is only thing outside of the execution model that require volatile, but you explain that a signal handler requires volatile and to be the si
by mlyle 7y ago
??
You say that it is only thing outside of the execution model that require volatile, but you explain that a signal handler requires volatile and to be the signal-atomic type. ;) In practice, volatile with lock-free types (that now stdatomic has a way to query whether types are lock-free) are correctly used all the time to pass data between threads.
A volatile declaration may be used to describe an object corresponding to a... an object accessed by an asynchronously interrupting function.
Like, execution in other thread contexts.
Or, from the C99 Rationale:
The translator may assume, for an unqualified lvalue, that it may read or write the referenced
object, that the value of this object cannot be changed except by explicitly programmed actions
in the current thread of control, but that other lvalue expressions could reference the same
object.
vs volatile:
No cacheing through this lvalue: each operation in the abstract semantics must
be performed (that is, no cacheing assumptions may be made, since the location
is not guaranteed to contain any previous value). In the absence of this qualifier,
the contents of the designated location may be assumed to be unchanged except
for possible aliasing.
> Double-checked locking is problematic in large part because of hardware reordering of loads and stores.
This was unsafe even on in-order uniprocessors, because the compiler was free to reorder loads and non-aliased stores.
- jcranmer 7y ago> You say that it is only thing outside of the execution model that require volatile No, I'm saying that the statement that volatile is only necessary and sufficient for outside the execution model. For signal handlers, it is necessary but not sufficient; for threads, it is neither necessary nor sufficient. > Or, from the C99 Rationale: C99 does not consider threading at all. C11 and C++11 do, and any compiler written in the past decade is going to be obeying the rules for the C++11 memory model (which C11 adopted as its memory model). The committees explicitly considered whether it would make sense to imbue volatile with any special threading semantics, and they explicitly rejected doing so. A volatile read or write is not guaranteed to be converted into a single hardware load or store, even for lock-free types. There are situations where the compiler will narrow or widen the load/store, or even insert extraneous loads and stores to the value. > This was unsafe even on in-order uniprocessors, because the compiler was free to reorder loads and non-aliased stores. The compiler is free to reorder non-volatile loads and stores with volatile loads and stores. Only reordering volatile loads and stores with respect to other volatile loads and stores is prohibited. Volatile is not sufficient to make double-checked locking safe.
- mlyle 7y agoI've been quoting C99 and my answers should be considered in that context. However, to say C99 does not "consider threading at all" is to neglect that the standards body document above explicitly mentions the thread of execution... in the quote that I stated. You seem to not be reading what I'm saying and also to be arguing with things I never asserted. e.g. > Volatile is not sufficient to make double-checked locking safe. OK, but I didn't say volatile makes double-checked locking safe... I said that the compiler's ability to reorder non-qualified variable accesses is "a big part of why" double-checked locking is an antipattern. My statement: > Actually, you promise not to change things from another thread..., let alone a const! The compiler loads things into registers and has no way to know if memory in a passed reference changes underneath the hood... It can freely make optimizations that lead to incorrect computation, infinite loops, segmentation faults if this is not obeyed. If you've ever head about how "double check locking" is an antipattern, this is a big part of why.