Aiden <
aide...@gmail.com> writes:
> Thanks for the feedback. As for the busy loop, I was using a pattern I
> saw elsewhere on the net where the select{} on the channel blocks
> waiting for a signal to wake the goroutine, stopping the busyness of
> the for{}, and the for{} exists only to never stop reading items from
> PushChan ... or am I mistaken there?
That was a misread on my part. select with just one channel does the
same thing as not using select. When I see that code with only one
case, I assume there's a default (because I wouldn't expect you to do it
for any other reason).
Should do roughly the same thing.
> Your range example does look clearer, I guess I didn't pick up that
> range polls until PushChan is closed and performs the for body on each
> item. They look equivalent to me?
Not quite equivalent. Yours didn't terminate on close (which *would*
make it a busy loop).
> The Lock was used in-place of a mutex (as per go blogs etc) to stop
> the concurrency issue of Pop() decrementing and PushRoutine()
> incrementing (and both stuffing in to the slice) without control. TBH
> both the other examples given by David and Steve are much cleaner
> altogether.
I wouldn't use a channel as a mutex other than as an exercise. I
didn't look at the other examples, but assuming you're running a select
over a few different channels to decide what to do and you're pushing
instructions into those channels, it gets really easy to think about,
and there's no locking that needs to occur as all the state is
manipulated on a single goroutine's context. When you do this, you
don't need locks.
--
dustin