Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb::task_scheduler_init Class Reference

Class delimiting the scope of task scheduler activity. More...

#include <task_scheduler_init.h>

Inheritance diagram for tbb::task_scheduler_init:
Collaboration diagram for tbb::task_scheduler_init:

Public Member Functions

void __TBB_EXPORTED_METHOD initialize (int number_of_threads=automatic)
 Ensure that scheduler exists for this thread. More...
 
void __TBB_EXPORTED_METHOD initialize (int number_of_threads, stack_size_type thread_stack_size)
 The overloaded method with stack size parameter. More...
 
void __TBB_EXPORTED_METHOD terminate ()
 Inverse of method initialize. More...
 
 task_scheduler_init (int number_of_threads=automatic, stack_size_type thread_stack_size=0)
 Shorthand for default constructor followed by call to initialize(number_of_threads). More...
 
 ~task_scheduler_init ()
 Destroy scheduler for this thread if thread has no other live task_scheduler_inits. More...
 
bool is_active () const
 Returns true if scheduler is active (initialized); false otherwise. More...
 

Static Public Member Functions

static int __TBB_EXPORTED_FUNC default_num_threads ()
 Returns the number of threads TBB scheduler would create if initialized by default. More...
 

Static Public Attributes

static const int automatic = -1
 Typedef for number of threads that is automatic. More...
 
static const int deferred = -2
 Argument to initialize() or constructor that causes initialization to be deferred. More...
 

Private Types

enum  ExceptionPropagationMode { propagation_mode_exact = 1u, propagation_mode_captured = 2u, propagation_mode_mask = propagation_mode_exact | propagation_mode_captured }
 

Private Member Functions

bool internal_terminate (bool blocking)
 
- Private Member Functions inherited from tbb::internal::no_copy
 no_copy ()
 Allow default construction. More...
 

Private Attributes

internal::scheduler * my_scheduler
 

Detailed Description

Class delimiting the scope of task scheduler activity.

A thread can construct a task_scheduler_init object and keep it alive while it uses TBB's tasking subsystem (including parallel algorithms).

This class allows to customize properties of the TBB task pool to some extent. For example it can limit concurrency level of parallel work initiated by the given thread. It also can be used to specify stack size of the TBB worker threads, though this setting is not effective if the thread pool has already been created.

If a parallel construct is used without task_scheduler_init object previously created, the scheduler will be initialized automatically with default settings, and will persist until this thread exits. Default concurrency level is defined as described in task_scheduler_init::initialize().

Definition at line 56 of file task_scheduler_init.h.

Member Enumeration Documentation

◆ ExceptionPropagationMode

Constructor & Destructor Documentation

◆ task_scheduler_init()

tbb::task_scheduler_init::task_scheduler_init ( int  number_of_threads = automatic,
stack_size_type  thread_stack_size = 0 
)
inline

Shorthand for default constructor followed by call to initialize(number_of_threads).

Definition at line 113 of file task_scheduler_init.h.

113  : my_scheduler(NULL)
114  {
115  // Two lowest order bits of the stack size argument may be taken to communicate
116  // default exception propagation mode of the client to be used when the
117  // client manually creates tasks in the master thread and does not use
118  // explicit task group context object. This is necessary because newer
119  // TBB binaries with exact propagation enabled by default may be used
120  // by older clients that expect tbb::captured_exception wrapper.
121  // All zeros mean old client - no preference.
122  __TBB_ASSERT( !(thread_stack_size & propagation_mode_mask), "Requested stack size is not aligned" );
123 #if TBB_USE_EXCEPTIONS
125 #endif /* TBB_USE_EXCEPTIONS */
126  initialize( number_of_threads, thread_stack_size );
127  }
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void __TBB_EXPORTED_METHOD initialize(int number_of_threads=automatic)
Ensure that scheduler exists for this thread.
Definition: governor.cpp:307
internal::scheduler * my_scheduler
#define TBB_USE_CAPTURED_EXCEPTION
Definition: tbb_config.h:506

References __TBB_ASSERT, initialize(), propagation_mode_captured, propagation_mode_exact, propagation_mode_mask, and TBB_USE_CAPTURED_EXCEPTION.

Here is the call graph for this function:

◆ ~task_scheduler_init()

tbb::task_scheduler_init::~task_scheduler_init ( )
inline

Destroy scheduler for this thread if thread has no other live task_scheduler_inits.

Definition at line 130 of file task_scheduler_init.h.

130  {
131  if( my_scheduler )
132  terminate();
134  }
internal::scheduler * my_scheduler
void poison_pointer(T *__TBB_atomic &)
Definition: tbb_stddef.h:309
void __TBB_EXPORTED_METHOD terminate()
Inverse of method initialize.
Definition: governor.cpp:358

References my_scheduler, tbb::internal::poison_pointer(), and terminate().

Here is the call graph for this function:

Member Function Documentation

◆ default_num_threads()

int tbb::task_scheduler_init::default_num_threads ( )
static

Returns the number of threads TBB scheduler would create if initialized by default.

Result returned by this method does not depend on whether the scheduler has already been initialized.

Because tbb 2.0 does not support blocking tasks yet, you may use this method to boost the number of threads in the tbb's internal pool, if your tasks are doing I/O operations. The optimal number of additional threads depends on how much time your tasks spend in the blocked state.

Before TBB 3.0 U4 this method returned the number of logical CPU in the system. Currently on Windows, Linux and FreeBSD it returns the number of logical CPUs available to the current process in accordance with its affinity mask.

NOTE: The return value of this method never changes after its first invocation. This means that changes in the process affinity mask that took place after this method was first invoked will not affect the number of worker threads in the TBB worker threads pool.

Definition at line 375 of file governor.cpp.

375  {
377 }
static unsigned default_num_threads()
Definition: governor.h:85

References tbb::internal::governor::default_num_threads().

Here is the call graph for this function:

◆ initialize() [1/2]

void tbb::task_scheduler_init::initialize ( int  number_of_threads = automatic)

Ensure that scheduler exists for this thread.

A value of -1 lets TBB decide on the number of threads, which is usually maximal hardware concurrency for this process, that is the number of logical CPUs on the machine (possibly limited by the processor affinity mask of this process (Windows) or of this thread (Linux, FreeBSD). It is preferable option for production code because it helps to avoid nasty surprises when several TBB based components run side-by-side or in a nested fashion inside the same process.

The number_of_threads is ignored if any other task_scheduler_inits currently exist. A thread may construct multiple task_scheduler_inits. Doing so does no harm because the underlying scheduler is reference counted.

Left out-of-line for the sake of the backward binary compatibility

Definition at line 307 of file governor.cpp.

307  {
308  initialize( number_of_threads, 0 );
309 }
void __TBB_EXPORTED_METHOD initialize(int number_of_threads=automatic)
Ensure that scheduler exists for this thread.
Definition: governor.cpp:307

Referenced by task_scheduler_init().

Here is the caller graph for this function:

◆ initialize() [2/2]

void tbb::task_scheduler_init::initialize ( int  number_of_threads,
stack_size_type  thread_stack_size 
)

The overloaded method with stack size parameter.

Overloading is necessary to preserve ABI compatibility

Definition at line 311 of file governor.cpp.

311  {
312 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
313  uintptr_t new_mode = thread_stack_size & propagation_mode_mask;
314 #endif
315  thread_stack_size &= ~(stack_size_type)propagation_mode_mask;
316  if( number_of_threads!=deferred ) {
317  __TBB_ASSERT_RELEASE( !my_scheduler, "task_scheduler_init already initialized" );
318  __TBB_ASSERT_RELEASE( number_of_threads==automatic || number_of_threads > 0,
319  "number_of_threads for task_scheduler_init must be automatic or positive" );
320  internal::generic_scheduler *s = governor::init_scheduler( number_of_threads, thread_stack_size, /*auto_init=*/false );
321 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
322  if ( s->master_outermost_level() ) {
323  uintptr_t &vt = s->default_context()->my_version_and_traits;
324  uintptr_t prev_mode = vt & task_group_context::exact_exception ? propagation_mode_exact : 0;
326  : new_mode & propagation_mode_captured ? vt & ~task_group_context::exact_exception : vt;
327  // Use least significant bit of the scheduler pointer to store previous mode.
328  // This is necessary when components compiled with different compilers and/or
329  // TBB versions initialize the
330  my_scheduler = static_cast<scheduler*>((generic_scheduler*)((uintptr_t)s | prev_mode));
331  }
332  else
333 #endif /* __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS */
334  my_scheduler = s;
335  } else {
336  __TBB_ASSERT_RELEASE( !thread_stack_size, "deferred initialization ignores stack size setting" );
337  }
338 }
static const int automatic
Typedef for number of threads that is automatic.
internal::scheduler * my_scheduler
static const int deferred
Argument to initialize() or constructor that causes initialization to be deferred.
#define __TBB_ASSERT_RELEASE(predicate, message)
Definition: tbb_stddef.h:138
std::size_t stack_size_type
void const char const char int ITT_FORMAT __itt_group_sync s
static generic_scheduler * init_scheduler(int num_threads, stack_size_type stack_size, bool auto_init)
Processes scheduler initialization request (possibly nested) in a master thread.
Definition: governor.cpp:176

References __TBB_ASSERT_RELEASE, tbb::task_group_context::exact_exception, tbb::internal::governor::init_scheduler(), and s.

Here is the call graph for this function:

◆ internal_terminate()

bool tbb::task_scheduler_init::internal_terminate ( bool  blocking)
private

Definition at line 340 of file governor.cpp.

340  {
341 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
342  uintptr_t prev_mode = (uintptr_t)my_scheduler & propagation_mode_exact;
343  my_scheduler = (scheduler*)((uintptr_t)my_scheduler & ~(uintptr_t)propagation_mode_exact);
344 #endif /* __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS */
345  generic_scheduler* s = static_cast<generic_scheduler*>(my_scheduler);
346  my_scheduler = NULL;
347  __TBB_ASSERT_RELEASE( s, "task_scheduler_init::terminate without corresponding task_scheduler_init::initialize()");
348 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
349  if ( s->master_outermost_level() ) {
350  uintptr_t &vt = s->default_context()->my_version_and_traits;
352  : vt & ~task_group_context::exact_exception;
353  }
354 #endif /* __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS */
355  return governor::terminate_scheduler(s, blocking);
356 }
internal::scheduler * my_scheduler
#define __TBB_ASSERT_RELEASE(predicate, message)
Definition: tbb_stddef.h:138
void const char const char int ITT_FORMAT __itt_group_sync s
static bool terminate_scheduler(generic_scheduler *s, bool blocking)
Processes scheduler termination request (possibly nested) in a master thread.
Definition: governor.cpp:207

References __TBB_ASSERT_RELEASE, tbb::task_group_context::exact_exception, s, and tbb::internal::governor::terminate_scheduler().

Here is the call graph for this function:

◆ is_active()

bool tbb::task_scheduler_init::is_active ( ) const
inline

Returns true if scheduler is active (initialized); false otherwise.

Definition at line 156 of file task_scheduler_init.h.

156 { return my_scheduler != NULL; }
internal::scheduler * my_scheduler

References my_scheduler.

◆ terminate()

void tbb::task_scheduler_init::terminate ( )

Inverse of method initialize.

Definition at line 358 of file governor.cpp.

358  {
359  internal_terminate(/*blocking_terminate=*/false);
360 }
bool internal_terminate(bool blocking)
Definition: governor.cpp:340

Referenced by ~task_scheduler_init().

Here is the caller graph for this function:

Member Data Documentation

◆ automatic

const int tbb::task_scheduler_init::automatic = -1
static

Typedef for number of threads that is automatic.

Definition at line 73 of file task_scheduler_init.h.

Referenced by tbb::internal::governor::init_scheduler(), and tbb::internal::governor::local_scheduler().

◆ deferred

const int tbb::task_scheduler_init::deferred = -2
static

Argument to initialize() or constructor that causes initialization to be deferred.

Definition at line 76 of file task_scheduler_init.h.

◆ my_scheduler

internal::scheduler* tbb::task_scheduler_init::my_scheduler
private

NULL if not currently initialized.

Definition at line 64 of file task_scheduler_init.h.

Referenced by is_active(), and ~task_scheduler_init().


The documentation for this class was generated from the following files:

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.