c++ - Waiting for multiple futures? -


i'd run tasks (worker threads) of same type, not more number of tasks @ time. when task finishes, result input new task which, then, can started.

is there way implement async/future paradigm in c++11?

at first glance, looks straight forward, spawn multiple tasks with:

std::future<t> result = std::async(...); 

and, then, run result.get() async result of task.

however, problem here future objects has stored in sort of queue , waited 1 one. is, though, possible iterate on future objects on , on again checking if of them ready, it's not desired due unnecessary cpu load.

is possible somehow wait any future given set ready , result?

the option can think of far old-school approach without async/future. specifically, spawning multiple worker threads , @ end of each thread push result mutex-protected queue notifying waiting thread via condition variable queue has been updated more results.

is there other better solution async/future possible?

thread support in c++11 first pass, , while std::future rocks, not support multiple waiting yet.

you can fake relatively inefficiently, however. end creating helper thread each std::future (ouch, expensive), gathering "this future ready" synchronized many-producer single-consumer message queue, setting consumer task dispatches fact given std::future ready.

the std::future in system doesn't add functionality, , having tasks directly state ready , sticks result above queue more efficient. if go route, write wrapper match pattern of std::async or std::thread, , return std::future object represents queue message. involves reimplementing chunk of the concurrency library.

if want stay std::future, create shared_futures, , have each dependent task depend on set of shared_futures: ie, without central scheduler. doesn't permit things abort/shutdown messages, consider essential robust multi threaded task system.

finally, can wait c++2x, or whenever concurrency ts folded standard, solve problem you.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -