Mixing with Other Threading Packages

Intel Threading Building Blocks can be mixed with other threading packages. No special effort is required to use the containers, synchronization primitives, or atomic operations with other threading packages. However, using the parallel algorithms or task scheduler requires extra effort because each thread that uses one of those features must construct its own task_scheduler_init object that is live while the feature is in use.

Mixing OpenMP (which was introduced in Chapter 1) with Threading Building Blocks is supported. Performance may be inferior to a pure OpenMP or pure Threading Building Blocks solution if the two forms of parallelism are nested.

An OpenMP parallel region that plans to use the task scheduler should create a task_ scheduler_init inside the parallel region, because the parallel region may create new threads unknown to Threading Building Blocks. Each of these new OpenMP threads, like native threads, must create a task_scheduler_init object before using Threading Building Blocks algorithms.

Example 10-1 parallelizes an outer loop with OpenMP and an inner loop with Intel Threading Building Blocks.

Example 10-1. OpenMP and parallel_for used together

int M, N;

struct InnerBody {
    ...
};

void TBB_NestedInOpenMP( ) {
#pragma omp parallel
    {
        task_scheduler_init init;
#pragma omp for
        for( int i=0; i<M; ++j ) {
            parallel_for( blocked_range<int>(0,N,10), InnerBody(i) );
        }
    }
}

The details of InnerBody are omitted for brevity. What is important ...

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.