←back to thread

Go channels are bad

(www.jtolds.com)
298 points jtolds | 1 comments | | HN request time: 0.202s | source
Show context
Jabbles ◴[] No.11210740[source]
Effective Go has always said:

Do not communicate by sharing memory; instead, share memory by communicating.

This approach can be taken too far. Reference counts may be best done by putting a mutex around an integer variable, for instance.

https://golang.org/doc/effective_go.html#sharing

replies(3): >>11210862 #>>11210978 #>>11210990 #
poizan42 ◴[] No.11210862[source]
Reference counts are best done using interlocked increment/decrement primitives.
replies(2): >>11210871 #>>11211088 #
chrisseaton ◴[] No.11211088[source]
I wonder if there are any compilers which can replace

    mutex.lock { x++ }
With a 'lock xaddl x 1' instruction.
replies(4): >>11211325 #>>11211581 #>>11211703 #>>11212909 #
pjmlp ◴[] No.11211581[source]
Java and .NET will do it if you make use of InterlockedIncrement APIs.
replies(1): >>11211672 #
chrisseaton ◴[] No.11211672[source]
But I think InterlockedIncrement is just 'lock xaddl x 1', so using InterlockedIncrement would be to do it manually.

I'm asking if any compiler can take a statement which uses a high level, general purpose lock and increments a variable inside it using conventional language expressions, and convert it to use 'lock xaddl x 1' (perhaps via InterlockedIncrement or whatever other intrinsics you have) instead.

I only know Java well, not .NET, but I'm pretty sure no Java compiler does it.

replies(1): >>11213205 #
1. pjmlp ◴[] No.11213205[source]
Ah, I missed the point.