I'm confused by something about C++11 std::future
. I want to balance work load dynamically, so if there are some processors idle, I create a std::future
with std::async
to divide the remaining data. It works fine.
std::future<int> f[MAX_CHILD];
for ( each data item ){
if ( found_idle_processor )
f[i] = std::async( ... );
process();
}
// At last, query the result of f.
for ( each future )
hold = f[i].get();
But sometimes, once some special data items were found, all other data will be discarded and the program should give the final result immediately, then another task will be launched.
std::future<int> f[MAX_CHILD];
for ( each data item ){
if ( found_idle_processor )
f[i] = std::async( ... );
process();
if ( found_special_item )
return final_result;
}
// At last, query the result of each f.
for ( each future )
hold = f[i].get();
But the created f
are out of my control, are they still running after return? how can I terminate them to release CPU time they used?