task クラス

概要

タスクの基本クラス。

構文

class task;

ヘッダー

#include "tbb/task.h"

説明

このクラスは、タスクの基本クラスです。プログラマーは、タスクからクラスを派生させて、仮想メソッド task* task::execute() をオーバーライドすると予測されます。

task の各インスタンスには、関連する属性があります。これらの属性を直接見ることはできませんが、タスク・オブジェクトがどのように使用されるかを把握するために理解する必要があります。属性の説明を下記の表に示します。

タスクの属性

属性

説明

successor

Null または現在のタスクが完了した後に refcount フィールドがデクリメントされる別のタスクのポインター。通常、successor は、現在のタスクを割り当てたタスク、またはタスクの継続として割り当てられたタスクです。

this が共通に使用されたケースであるため、task クラスのメソッドはサクセサータスクを "親"、プレデセッサー・タスクを "子" と呼びます。しかし、ライブラリーは、子-親の関係がプレデセッサー-サクセサーの間で必要ないように進化しました。

refcount

this を親として持つタスクの数。refcount のインクリメントおよびデクリメントは常にアトミックです。

ヒント

常に特別にオーバーロードされる new 演算子の 1 つを使用してタスク・オブジェクトにメモリーを割り当てます。その他の場合、結果は不定です。task の破棄は通常は暗黙的です。task のコピー・コンストラクターと代入演算子はアクセスできません。これは、タスクが偶然コピーされて内部データ構造が不正確になったり壊れたりすることを防ぐための制限です。

表記規則

いくつかのメンバーの説明では、下記の図のようなダイアグラムで結果が示されます。

結果ダイアグラムのサンプル

これらのダイアグラムの表記規則は次のとおりです。

メンバー

次の説明で、proxy1...proxy5 型は内部の型です。これらの型を返すメソッドは、「タスクの割り当て」セクションで説明されているように、特別にオーバーロードされた new 演算子とともにのみ使用してください。

namespace tbb {
        class task {
        protected:
            task();
     
        public:
            virtual ~task() {}
     
            virtual task* execute() = 0;
     
            // 割り当て
            static proxy1 allocate_root();
            static proxy2 allocate_root( task_group_context& );
            proxy3 allocate_continuation();
            proxy4 allocate_child();
            static proxy5 allocate_additional_child_of( task& );
     
            // 明示的な破棄
            static void destroy( task& victim );
     
            // 再利用
            void recycle_as_continuation();
            void recycle_as_safe_continuation();
            void recycle_as_child_of( task& new_parent );
     
            // 同期
            void set_ref_count( int count );
            void increment_ref_count();
            int add_ref_count( int count );
            int decrement_ref_count();
            void wait_for_all();
            static void spawn( task& t );
            static void spawn( task_list& list );
            void spawn_and_wait_for_all( task& t );  
            void spawn_and_wait_for_all( task_list& list );  
            static void spawn_root_and_wait( task& root );
            static void spawn_root_and_wait( task_list& root );
            static void enqueue( task& );
     
            // タスクのコンテキスト
            static task& self();
            task* parent() const;
            void set_parent( task *p );
            bool is_stolen_task() const;
            task_group_context* group();
            void change_group( task_group_context& ctx );
     
            // キャンセル
            bool cancel_group_execution();
            bool is_cancelled() const;
     
            // 優先度
            static void enqueue( task&, priority_t );
            void set_group_priority ( priority_t );
            priority_t group_priority () const;
     
            // アフィニティー
            typedef implementation-defined-unsigned-type affinity_id;
            virtual void note_affinity( affinity_id id );
            void set_affinity( affinity_id id );
            affinity_id affinity() const;
     
            // デバッグ
            enum state_type {
                executing,
                reexecute,
                ready,
                allocated,
                freed
            };
            int ref_count() const;
            state_type state() const;
        };
    } // tbb 名前空間
     
    void *operator new( size_t bytes, const proxy1& p );
    void operator delete( void* task, const proxy1& p );
    void *operator new( size_t bytes, const proxy2& p );
    void operator delete( void* task, const proxy2& p );
    void *operator new( size_t bytes, const proxy3& p );
    void operator delete( void* task, const proxy3& p );
    void *operator new( size_t bytes, proxy4& p );
    void operator delete( void* task, proxy4& p );
    void *operator new( size_t bytes, proxy5& p );
    void operator delete( void* task, proxy5& p );

インテル® TBB 3.0 よりも前は、allocate_additional_child_ofdestroyspawn メソッドは非スタティックでした。ライブラリーの進化により、これらの呼び出しに this 引数は必要でなくなりました。メソッドのアドレスが使用される場合を除いて、ソースの互換性は保たれます。インテル® TBB 3.0 ランタイム・ライブラリーとリンクした場合でも、非スタティック形式の古いヘッダーでコンパイルされた実行ファイルはそのまま動作します。

関連情報