Skip to Main Content
Intel Threading Building Blocks
book

Intel Threading Building Blocks

by James Reinders
July 2007
Intermediate to advanced content levelIntermediate to advanced
332 pages
10h 4m
English
O'Reilly Media, Inc.
Content preview from Intel Threading Building Blocks

Empty tasks

You might need a task that does not do anything but wait for its children to complete. The header file task.h defines class empty_task for this purpose. Example 9-10 shows its definition.

Example 9-10. empty_task

// Task that does nothing. Useful for synchronization.
class empty_task: public task {
    /*override*/ task* execute() {
        return NULL;
    }
};

A good example of empty_task in action shown in Example 9-11. It invokes parallel_for in the method start_for::execute(). The code there uses continuation-passing style. It creates two child tasks and uses an empty_task as the continuation when the child tasks complete. The top-level routine parallel_for waits on the root.

Example 9-11. empty_task usage from parallel_for

template<typename Range, typename Body>
task* start_for<Range,Body>::execute() {
  if( !my_range.is_divisible() ) {
   my_body( my_range );
   return NULL;
  } else {
   empty_task& c = *new(allocate_continuation()) empty_task;
   recycle_as_child_of(c);
   c.set_ref_count(2);
   start_for& b =
     *new(c.allocate_child()) start_for(Range(my_range,split()),my_body);
   c.spawn(b);
   return this; 
  }
}
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Intel® Xeon Phi™ Coprocessor Architecture and Tools: The Guide for Application Developers

Intel® Xeon Phi™ Coprocessor Architecture and Tools: The Guide for Application Developers

Rezaur Rahman

Publisher Resources

ISBN: 9780596514808Errata Page