The following code fragment launches nTasks
times the function myTask
as an asynchronous thread, then collects results, maintaining the order of tasks. It works well unless the number of tasks exceeds an OS dependent limit. For a big number of tasks a more elaborate management is needed that keeps the number of active threads under a reasonable limit, launching new threads as old threads terminate.
I guess this is quite a standard problem for which I should not develop a solution of my own. Is there a solution in the standard library? Or in Boost or other middleware? Or an example from stable code somewhere?
vector<std::future<string>> workers;
for (int i=0; i<nTasks; ++i) {
try {
workers.push_back(std::async(std::launch::async, myTask, i));
} catch (std::exception& ex) {
throw "launching job " + S(i) + "/" + S(nTasks) + " failed: " + ex.what();
}
}
for (auto& w: workers)
report(w.get());
Aucun commentaire:
Enregistrer un commentaire