The idea of having a channel to which the main-routine sends tasks and a fixed number of sub-routines (pool) which are reading from that channel, is a very interesting one. It would optimize the code in so far that we initialize these routines only once. Of course, you can always have as many routines run in parallel as you want, but for numeric computations it would make less sense to have more than one heavy computations running on the same core at the same time. For IO-heavy tasks things look quite different of course.

The code could look like so:

var taskList = … // some list of tasks to run
tasksCh := make(channel Task)
waitOnAll := make(channel bool, len(taskList))

// building a pool:
for i:=0; i<len(tasksCh); i++{
go func(){
for task := range tasksCh{
… // some cpu-heavy computation based on task’s parameter
waitOnAll <- true // inform task have been processed
}
}
}

// scheduling a list of tasks to run
for task := range taskList{
tasksCh <- task // blocks whenever all above routines are in working phase
}

<- waitOnAll // block until all have finished
tasksCh.Close()
waitOnAll.Close()

Thanks for this idea!

I am a mathematician and software developer who likes writing advanced code and do research in applied and pure mathematics.

I am a mathematician and software developer who likes writing advanced code and do research in applied and pure mathematics.