This video tutorial took a lot of effort because of my inflated expectations. I thought that std::async
was a gateway to task-based parallelism. I blogged about task-based concurrency in The Future of Concurrent Programming and, in the context of Haskell, in Parallel Programming with Hints. And of course there is the problem of lack of composability of futures. So for the next 10 or so years we’ll have to stick to libraries, such as Microsoft PPL or Intel TBB or even OpenMP. Or experiment with other languages.
Follow @BartoszMilewski
(You can also follow me on Google+, if you search for Bartosz Milewski.)
October 3, 2011
C++11 Concurrency Tutorial: 5. Tasks
Posted by Bartosz Milewski under C++, Concurrency, Multicore, Multithreading, Parallelism, Programming, Tutorial[4] Comments
October 3, 2011 at 3:49 pm
[…] Read the whole article […]
October 7, 2011 at 8:22 am
I’m not sure if I got this. With launch::deferred you basically create a task that can’t be run in parallel unless it is explicitly passed to another thread?
October 15, 2011 at 3:31 pm
A deferred task is functionally equivalent to a lambda. At some point in your program you have the arguments for a function call (and possibly some state to capture), but you don’t want to execute it yet. So you pack code and state into a lambda or a deferred task, whichever is more convenient. Then at some other point in your program you execute it. The difference between a lambda and a deferred task is that a lambda object has a weird type that cannot be expressed in a program, whereas access to a deferred task is through a well-typed future. Since you know the type of the future, you can store it in an array, or return it from a function. You can’t do it with lambdas. However, you can always convert a lambda into a std::function, and then these limitations are gone. So the only real difference between the two that I can see is that you can store, in the same container, a mixture of futures representing async and deferred tasks, as long as they return the same type.
October 17, 2011 at 3:00 am
Thanks for the clarification and also for the very interesting follow-up post on the topic.