News for hackers. Stuff that matters.
Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by yalue on Monday February 20, 2017 @08:58AM (#13687215)
Variable writes that look like they have no side effects may actually be intended for another thread to read (but hopefully most reasonable programs don't do this without some good explanation). However, many concurrency models say that this method of inter-thread communication is Okay to optimize out, because most threading libraries (including Go's) make no guarantees about the order in which threads are scheduled. Therefore, removing the "writing" thread entirely is equivalent to it never being scheduled.

However, this is not the case for atomic operations (which are generally specifically intended for communication via shared variables). This optimization of removing the writing thread entirely isn't allowed if atomic operations are used, because those actually do come with a guarantee that other threads will get to see the update in finite time.

Go was incorrectly "optimizing" the atomic operations out entirely. It's a fairly subtle bug, actually, but still likely a big deal for the few programs communicating via shared variables.

Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by masklinn on Monday February 20, 2017 @09:10AM (#13687323)
> However, this is not the case for atomic operations (which are generally specifically intended for communication via shared variables). This optimization of removing the writing thread entirely isn't allowed if atomic operations are used, because those actually do come with a guarantee that other threads will get to see the update in finite time.

That sounds more like volatile than atomic. At its core, atomic just means that observer may see either pre-update or post-update state but won't see intermediate states.

Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by yalue on Monday February 20, 2017 @09:17AM (#13687373)
I agree that this sounds more like "volatile", but it still makes sense that programmers should be able to rely on atomic operations for inter-thread communication.

According to the comments on the bug report, this behavior is inspired by C++'s <atomic> library, which guarantees cross-thread visibility.

Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by chimeracoder on Monday February 20, 2017 @09:30AM (#13687492)
> I agree that this sounds more like "volatile", but it still makes sense that programmers should be able to rely on atomic operations for inter-thread communication.

Only in the sense that they won't see inconsistent (intermediate) states.

Atomicity shouldn't guarantee order per se - if you want both, you need a mutex.

Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by gpderetta on Monday February 20, 2017 @12:04PM (#13688842)
Ordered atomic operations like C++ std::atomic or java/C# volatile do guarantee some ordering. For example if one thread modifies an object and then stores a pointer to an atomic variable, under default visibility rules, another thread reading that pointer from the atomic variable is guaranteed to see the modifications to the object itself, i.e. the update, the store and the remote load are ordered.

It is possible to have a model where all atomic operations are relaxed (i.e. unordered) and use explicit barriers for ordering (which is closer to how some, but not all, CPUs work), but it is significantly harder to reason about and prove algorithms correct under it.

edit: wording

Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by wbl on Monday February 20, 2017 @12:37PM (#13689159)
No. Read the spec. There is no deadline for atomics to be visible. What is true is that some operations have happens-before ordering, and if a store happens-before a load, the load will see the result of the store.
Re:Cmd/compile: Go 1.8 regression: sync/atomic loop elided by gpderetta on Monday February 20, 2017 @01:00PM (#13689379)
Did you reply to the wrong post? My post was only about ordering.

Still the C++ spec has wording about making stores visible in a reasonable time. The wording is non-normative as they run out of time (ah!) while trying to come up with a more formal (and implementable) specification.

edit: oh, I see I wrote 'default visibility' instead of 'default ordering'.