Newbie question on channels:
I send a message to a buffered channel which has a receiver waiting on a select.
If I send the message and immediately check len(channel), its empty.
This appears to be because the receiver has immediately read the channel before the len() is executed. This is reasonable behaviour.
I’m trying to understand if I can see if the receiver process is busy or not. Especially for a unit test, and maybe later to see if an entire set of goroutines are quiescent (i.e. all block on a select). Currently I’m setting a boolean immediately after the select fires, but there is a race condition where len() is zero but the boolean is not yet set, and so the code thinks the receiver is not busy but in fact it has only just begun.
Is there a way to solve this, e.g. by being able to place a lock as soon as the channel is read?
current code:
select {
case msg := <- channel:
mu.Lock()
Busy = true
mu.Unlock()
In the above there is a window where the channel has been read (len() is 0) but we have not yet assigned Busy.
Is there a way to solve this?
But perhaps a better question is is there a way to know if a specific goroutine is blocked on a select in a thread safe way?