I have a pre-defined number of tasks (objects of Task) and an also pre-defined number of threads.
Let's assume 8 threads and 32 tasks.
My current solution allows me to run 8 tasks in parallel, and once they're finished, run another 8 in parallel.
With this solution it might happen, that 7 tasks will have to wait for the slowest one.
I'd like to have a solution, where once a thread finishes its task, it'll directly re-run with a new task, until there are none left.
My current implementation:
vector <Task> tasks; //the 32 tasks
unsigned int maxThreadCount(8);
for ( unsigned int i = 0; i < tasks.size() - maxThreadCount; i+=maxThreadCount )
{
vector <thread> threads;
for (unsigned int j = 0; j < maxThreadCount; ++j)
threads.push_back (thread(std::bind( &Task::some_function, tasks[i+j])));
for ( auto &i : threads)
i.join();
}