Answer the question
In order to leave comments, you need to log in
How to know when goroutines have finished recording to close the channel?
[Updated code link, forgot to do wg.Add for the last goroutine]
Hello everyone, I need to write a function for parallel execution of jobs in n parallel goroutines:
- the number of created goroutines should not depend on the number of jobs, i.e. the function should run n goroutines for parallel processing of tasks and, possibly, a few more auxiliary goroutines;
- the function must stop its work if m errors occur;
- after the completion of the function (successful or due to exceeding m), there should not be any running goroutines;
- if the tasks run without errors, then len(tasks) of tasks will be executed (i.e. all tasks);
- if errors occur in the first m tasks, then no more than n+m tasks will be executed.
my codehttps://goplay.tools/snippet/ZEngzDhQB_W
test code https://goplay.tools/snippet/2iauUBIAzA0
I'm waiting on the errorsCh channel, in theory, I need to close it, but if I close it on line 65 as
case <-done:
close(errors)
return
then I always get a panic: close of closed channel and this is logical, because some goroutine will not have time to complete yet and will try to write to the error channel again, but I'm confused how and where close it properly?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question