Already searched through previous posts, but could not find optimal
solution.
Suppose I have the following:
- 1 goroutine generating work
- N worker goroutines
- 1 go routine receiving results
Amount of work is arbitrary, does not fit single buffered channel, nor can
I know in advance how many units of work will there be.
Current implementation works when there is fixed number of units, and is
implemented as such:
- 1 work channel
- 1 data channel
- 1 done channel
- 1 error channel
All work is (N units) generated up-front and put into buffer channel
(work). Each worker takes 1 unit from work channel, does work and sends
data or error through respective channels. In any case, worker sends 1 done
to done channel for each unit of work.
There is another goroutine collecting data/errors and checking how many
units have completed (it expects N reads from done channel).
This obviously works when number of units (N) is known up-front, as its
simply a matter of receiving form done channel N times.
I was thinking how to apply this to where N is not known. There would be
another goroutine generating work (instead of generating it up-front).
Simplest solution would be to mutex protect a counter, increase it for each
work added and decrease it for each processed (on receive from done).
Obviously some checks need to be done to make sure no more units are being
generated before ending the whole thing when counter reaches 0 (if workers
are faster than producer).
Is there a more idiomatic way to solve this? Seems like mutex is simplest
to use. Would be also simple to solve if channels had .isEmpty() function ;)
Thx,
Igor
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.