←back to thread

159 points mpweiher | 2 comments | | HN request time: 0.017s | source
Show context
ricardobeat ◴[] No.43671958[source]
Strange to go all this length without mentioning the approaches that solve the problem in that first example:

1. send a close message on the channel that stops the goroutine

2. use a Context instance - `ctx.Done()` returns a channel you can select on

Both are quite easy to grasp and implement.

replies(2): >>43671984 #>>43672081 #
jtolds ◴[] No.43671984[source]
Hi! No, I think you've misunderstood the assignment. The example posits that you have a "game" running, which should end when the last player leaves. While only using channels as a synchronization primitive (a la CSP), at what point do you decide the last player has left, and where and when do you call close on the channel?
replies(5): >>43672077 #>>43672431 #>>43672954 #>>43673132 #>>43675471 #
1. aflag ◴[] No.43672431[source]
Naive question, can't you just have a player count alongside the best score and leave when that reaches 0?
replies(1): >>43674754 #
2. jtolds ◴[] No.43674754[source]
Adding an atomic counter is absolutely a great solution in the real world, definitely, and compare and swap or a mutex or similar totally is what you want to do. In fact, that's my point in that part of the post - you want an atomic variable or a mutex or something there. Other synchronization primitives are more useful than sticking with the CSP idea of only using channels for synchronization.