[For complete, up-to-date TBB information visit: http://www.ThreadingBuildingBlocks.org]

TbbRef (Ver. 20): 8.2 task_scheduler_init Class

8.2 task_scheduler_init Class

Summary

Class that represents thread's interest in task scheduling services.

parent parent child child child childparent parent

Syntax

class task_scheduler_init;

Header

#include "tbb/task_scheduler_init.h"

Description

A task_scheduler_init is either "active" or "inactive". Each thread that uses a task should have one active task_scheduler_init object that stays active over the duration that the thread uses task objects. A thread may have more than one active task_scheduler_init at any given moment.

The default constructor for a task_scheduler_init activates it, and the destructor uninitializes it. To defer initialization, pass the value task_scheduler_init::deferred to the constructor. Such a task_scheduler_init may be initialized later by calling method initialize. Destruction of an initialized task_scheduler_init implicitly deactivates it. To deactivate it earlier, call method terminate.

An optional parameter to the constructor and method initialize allow you to specify the number of threads to be used for task execution. This parameter is useful for scaling studies during development, but should not be set for production use. The Tutorial document says more about this topic.

To minimize time overhead, it is best to have a thread create a single task_scheduler_init object whose activation spans all uses of the library's task scheduler. A task_scheduler_init is not assignable or copy-constructible.

Important

The template algorithms ( 3) implicitly use class task. Hence creating a task_scheduler_init is a prerequisite to using the template algorithms. The debug version of the library reports failure to create the task_scheduler_init.

Example

#include "tbb/task_scheduler_init"

int main() {

task_scheduler_init init;

... use task or template algorithms here...

return 0;

}

Members

namespace tbb {

class task_scheduler_init {

public:

static const int automatic = implementation-defined;

static const int deferred = implementation-defined;

8.2.3 void initialize( int number_of_threads=automatic )

Requirements

The task_scheduler_init shall be inactive.

Effects

Similar to constructor ( 8.2.1).

8.2.4 void terminate()

Requirements

The task_scheduler_init shall be active.

Effects

Deactivates the task_scheduler_init without destroying it. The description of the destructor ( 8.2.2) specifies what deactivation entails.

8.2.5 Mixing with OpenMP

Mixing OpenMP with Intel® Threading Building Blocks is supported. Performance may be less than a pure OpenMP or pure Intel® 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 Intel® Threading Building Blocks. Each of these new OpenMP threads, like native threads, must create a task_scheduler_init object before using Intel® Threading Building Blocks algorithms. The following example demonstrates how to do this.

void OpenMP_Calls_TBB( int n ) {

#pragma omp parallel

{

task_scheduler_init init;

#pragma omp for

for( int i=0; i<n; ++i ) {

...can use class task or

Intel® Threading Building Blocks algorithms here ...

}

}

}

[For complete, up-to-date TBB information visit: http://www.ThreadingBuildingBlocks.org]