Home ⌂Doc Index ◂Up ▴
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 (const no_copy &)=delete
 
 no_copy ()=default
 

Private Attributes

internal::schedulermy_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 66 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 123 of file task_scheduler_init.h.

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

References __TBB_ASSERT, and TBB_USE_CAPTURED_EXCEPTION.

◆ ~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 140 of file task_scheduler_init.h.

140  {
141  if( my_scheduler )
142  terminate();
144  }
void __TBB_EXPORTED_METHOD terminate()
Inverse of method initialize.
Definition: governor.cpp:528
internal::scheduler * my_scheduler
void poison_pointer(T *__TBB_atomic &)
Definition: tbb_stddef.h:305

References tbb::internal::poison_pointer().

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 545 of file governor.cpp.

545  {
547 }
static unsigned default_num_threads()
Definition: governor.h:84

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 477 of file governor.cpp.

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

◆ 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 481 of file governor.cpp.

481  {
482 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
483  uintptr_t new_mode = thread_stack_size & propagation_mode_mask;
484 #endif
485  thread_stack_size &= ~(stack_size_type)propagation_mode_mask;
486  if( number_of_threads!=deferred ) {
487  __TBB_ASSERT_RELEASE( !my_scheduler, "task_scheduler_init already initialized" );
488  __TBB_ASSERT_RELEASE( number_of_threads==automatic || number_of_threads > 0,
489  "number_of_threads for task_scheduler_init must be automatic or positive" );
490  internal::generic_scheduler *s = governor::init_scheduler( number_of_threads, thread_stack_size, /*auto_init=*/false );
491 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
492  if ( s->master_outermost_level() ) {
493  uintptr_t &vt = s->default_context()->my_version_and_traits;
494  uintptr_t prev_mode = vt & task_group_context::exact_exception ? propagation_mode_exact : 0;
496  : new_mode & propagation_mode_captured ? vt & ~task_group_context::exact_exception : vt;
497  // Use least significant bit of the scheduler pointer to store previous mode.
498  // This is necessary when components compiled with different compilers and/or
499  // TBB versions initialize the
500  my_scheduler = static_cast<scheduler*>((generic_scheduler*)((uintptr_t)s | prev_mode));
501  }
502  else
503 #endif /* __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS */
504  my_scheduler = s;
505  } else {
506  __TBB_ASSERT_RELEASE( !thread_stack_size, "deferred initialization ignores stack size setting" );
507  }
508 }
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:172
#define __TBB_ASSERT_RELEASE(predicate, message)
Definition: tbb_stddef.h:134
std::size_t stack_size_type
static const int deferred
Argument to initialize() or constructor that causes initialization to be deferred.
void const char const char int ITT_FORMAT __itt_group_sync s
internal::scheduler * my_scheduler
static const int automatic
Typedef for number of threads that is automatic.

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 510 of file governor.cpp.

510  {
511 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
512  uintptr_t prev_mode = (uintptr_t)my_scheduler & propagation_mode_exact;
513  my_scheduler = (scheduler*)((uintptr_t)my_scheduler & ~(uintptr_t)propagation_mode_exact);
514 #endif /* __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS */
515  generic_scheduler* s = static_cast<generic_scheduler*>(my_scheduler);
516  my_scheduler = NULL;
517  __TBB_ASSERT_RELEASE( s, "task_scheduler_init::terminate without corresponding task_scheduler_init::initialize()");
518 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS
519  if ( s->master_outermost_level() ) {
520  uintptr_t &vt = s->default_context()->my_version_and_traits;
522  : vt & ~task_group_context::exact_exception;
523  }
524 #endif /* __TBB_TASK_GROUP_CONTEXT && TBB_USE_EXCEPTIONS */
525  return governor::terminate_scheduler(s, blocking);
526 }
#define __TBB_ASSERT_RELEASE(predicate, message)
Definition: tbb_stddef.h:134
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:205
internal::scheduler * my_scheduler

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 166 of file task_scheduler_init.h.

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

◆ terminate()

void tbb::task_scheduler_init::terminate ( )

Inverse of method initialize.

Definition at line 528 of file governor.cpp.

528  {
529  internal_terminate(/*blocking_terminate=*/false);
530 }
bool internal_terminate(bool blocking)
Definition: governor.cpp:510

Member Data Documentation

◆ automatic

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

Typedef for number of threads that is automatic.

Definition at line 83 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 86 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 74 of file task_scheduler_init.h.


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

Copyright © 2005-2020 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.