On Saturday, September 26, 2020 at 12:33:40 PM UTC-7 Ian Lance Taylor wrote:

> On Fri, Sep 25, 2020 at 6:29 PM Alex Besogonov <alex.be...@gmail.com> 
> wrote: 
> > If you mutate the value, then only the current goroutine is affected 
> (not even its existing children). Basically, the goroutine-local context is 
> copied on new goroutine launch. 
> > That's how pprof labels work right now, btw. 
> Labels are not variables. Variables have values that can be changed. 
>
Most people don't need fully mutable goroutine-specific variables, but 
something like an automatic context carrier. It's immutable, so this would 
mostly solve the mutation problem. 

So in your example you'll have to write:
for _, p := range s { threadCtx = 
threadCtx.WithValue(p.SlowMethod(threadCtx.get()));}
 
This makes it clear that there's causal relationship between the current 
value and the next one, so the loop can't be naïvely parallelized. Of 
course, a very determined programmer can still shoot themselves in the foot 
(by reading and replacing the automatic context inside p.SlowMethod), but 
the footgun here is pretty explicit.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3b7ab9c2-e1d5-4cc4-b5c0-bcac3e96b37dn%40googlegroups.com.

Reply via email to