A
A
Alexey Verkhovtsev2021-08-02 14:13:37
go
Alexey Verkhovtsev, 2021-08-02 14:13:37

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

2 answer(s)
F
falconandy, 2021-08-02
@falconandy

Something like this: https://goplay.tools/snippet/TciyGAE8dyn

W
WinPooh32, 2021-08-03
@WinPooh32

I suggest not to abuse the channels:
https://goplay.tools/snippet/a91nAx2wevG
Your test from the question passes. I did not check the correctness of the test :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question