A task_group represents concurrent execution of a group of tasks. Tasks may be dynamically added to the group as it is executing.
#include "tbb/task_group.h" using namespace tbb; int Fib(int n) { if( n<2 ) { return n; } else { int x, y; task_group g; g.run([&]{x=Fib(n-1);}); // spawn a task g.run([&]{y=Fib(n-2);}); // spawn another task g.wait(); // wait for both tasks to complete return x+y; } }
Creating a large number of tasks for a single task_group is not scalable, because task creation becomes a serial bottleneck. If creating more than a small number of concurrent tasks, consider using parallel_for or parallel_invoke instead, or structure the spawning as a recursive tree.
namespace tbb { class task_group { public: task_group(); ~task_group(); template<typename Func> void run( const Func& f ); template<typename Func> void run( task_handle<Func>& handle ); template<typename Func> void run_and_wait( const Func& f ); template<typename Func> void run_and_wait( task_handle<Func>& handle ); task_group_status wait(); bool is_canceling(); void cancel(); } }