I'd like to run tasks (worker threads) of the same type, but not more than a certain number of tasks at a time. When a task finishes, its result is an input for a new task which, then, can be started.
Is there any good way to implement this with async/future paradigm in C++11?
At first glance, it looks straight forward, you just spawn multiple tasks with:
std::future<T> result = std::async(...);
Thread support in C++11 was just a first pass, and while
std::future rocks, it does not support multiple waiting as yet.
You can fake it relatively inefficiently, however. You end up creating a helper thread for each
std::future (ouch, very expensive), then gathering their "this
future is ready" into a synchronized many-producer single-consumer message queue, then setting up a consumer task that dispatches the fact that a given
std::future is ready.
std::future in this system doesn't add much functionality, and having tasks that directly state that they are ready and sticks their result into the above queue would be more efficient. If you go this route, you could write wrapper that match the pattern of
std::thread, and return a
std::future like object that represents a queue message. This basically involves reimplementing a chunk of the the concurrency library.
If you want to stay with
std::future, you could create
shared_futures, and have each dependent task depend on the set of
shared_futures: ie, do it without a central scheduler. This doesn't permit things like abort/shutdown messages, which I consider essential for a robust multi threaded task system.
Finally, you can wait for C++2x, or whenever the concurrency TS is folded into the standard, to solve the problem for you.