The task_scheduler_observer Class is extended to support local semantics and to observe when worker threads join and leave a task scheduler arena. Before these extensions, a task_scheduler_observer would only observe when a worker thread would join and exit the global task scheduler.
class task_scheduler_observer;
#define TBB_PREVIEW_LOCAL_OBSERVER 1 #include "tbb/task_scheduler_observer.h"
namespace tbb { class task_scheduler_observer { public: task_scheduler_observer( bool local = false ); // See task_scheduler_observer for the methods // not related to local observer ... #if TBB_PREVIEW_TASK_ARENA task_scheduler_observer( task_arena & a) #endif virtual bool on_scheduler_leaving() { return true; } }; }
The following example sketches the code of an observer used to pin worker threads to hardware threads and to prevent workers from sleeping.
class pinning_observer: public tbb::task_scheduler_observer { public: affinity_mask_t m_mask; // HW affinity mask to be used with an arena pinning_observer( affinity_mask_t mask ) : m_mask(mask) { } pinning_observer( tbb::task_arena &a, affinity_mask_t mask ) : tbb::task_scheduler_observer(a), m_mask(mask) { observe(true); // activate the observer } /*override*/ void on_scheduler_entry( bool worker ) { set_thread_affinity(tbb::task_arena::current_slot(), m_mask); } /*override*/ bool on_scheduler_leaving( ) { // Return false to trap the thread in the arena return /*bool*/ !is_more_work_available_soon(); } /*override*/ void on_scheduler_exit( bool worker ) { } };
Member | Description |
---|---|
task_scheduler_observer( bool local = false ) |
Constructs a task_scheduler_observer object in an inactive state (observation is disabled). If local is false, it does not differ from the global semantics described for task_scheduler_observer. If local is true, and the observer has been activated, the entry notifications is invoked only for threads in current arena. Correspondently, a thread receives exit notifications when it leaves this arena. |
task_scheduler_observer( task_arena & ) |
Constructs a task_scheduler_observer object in the inactive state (observation is disabled) tied to the specified task_arena. It receives notifications related only to this specified arena. NoteInvocation of observe(true) for such an object can force initialization of the internal arena representation of the specified task_arena object |
virtual bool on_scheduler_leaving() |
The callback is invoked in a worker thread before it leaves an arena. If it returns false, the thread remains in the arena trying to find more work. It will not be called for master threads or if a worker thread leaves the arena due to rebalancing, priority changes, etc. NoteThe application must be linked against the Community Preview library for this method to take effect. |