...
> Declaring the lock pointer "volatile" seems to prevent this misbehavior.
Great. That is what it is anyway, right?
> Personally I'd call this a compiler bug; isn't it supposed to consider
> semicolons as sequence points? I never heard that rearranging the order
> of stores into memory was considered a kosher optimization of C code.
Sure it is. Presumably "-O0" or equivalent would have kept this from
happening, but seemingly unrelated stores into non-overlapping memory
are always fair game at even modest levels of optimization. The "x = 0"
is cheaper than the other operations, though it may be reordered for
internal RISC-y reasons rather than "cheapest first" considerations.
- Thomas