buffer_node クラス

概要

T 型のメッセージの無制限バッファー。メッセージは任意の順序で転送されます。

構文

template< typename T, typename A = cache_aligned_allocator<T> >
class buffer_node;

ヘッダー

#include "tbb/flow_graph.h"

説明

buffer_node は、サクセサーセットの 1 つのサクセサーに任意の順序でメッセージを転送する graph_nodereceiver<T>sender<T> です。ノードに登録された順番で、サクセサーにメッセージが転送されます。サクセサーがメッセージを拒否すると、そのサクセサーはメッセージ・パッシング・プロトコルのポリシーに従ってサクセサーリストから削除され、セットの次のサクセサーが処理されます。これはサクセサーがメッセージを受け付けるまで、またはすべてのサクセサーが処理されるまで続行されます。サクセサーに転送されたアイテムはバッファーから削除されます。

buffer_node は予約可能で、一度に 1 つの予約をサポートします。1 つのアイテムが予約されている間、ほかのアイテムはサクセサーに転送され、try_get の呼び出しはほかに予約されていないアイテムがある場合はそのアイテムを返します。1 つのアイテムが予約されている間、try_puttrue を返し、バッファーにアイテムを追加します。

A 型のアロケーターが、buffer_node に内部メモリーを割り当てるために使用されます。

T は、コピー構築可能および代入可能でなければなりません。

サクセサーがメッセージを拒否した場合、「メッセージ・パッシング・プロトコル」で説明されているプロトコルを使用して処理されます。

サンプル

buffer_node の使用シナリオの一例を以下に示します。buffer_node は、拒否されたメッセージを格納して損失を防ぎ、ほかのグラフノードが処理できるようにするロードバランサーとして使用されています。

#include "tbb/flow_graph.h"
#include <chrono>
#include <thread>

int main() {
    tbb::flow::graph g;

    tbb::flow::function_node< int, int > prepare(g, tbb::flow::unlimited, [] (const int& data) {
        printf("Prepare data: %d\n", data);
        return data;
    });

    tbb::flow::buffer_node< int > load_balancer(g); 

    tbb::flow::function_node< int, tbb::flow::continue_msg, tbb::flow::rejecting > 
            first_worker(g, tbb::flow::serial, [] (const int& data) {
                 printf("Process data with first worker: %d\n", data);
                 // Long computation
                 std::this_thread::sleep_for(std::chrono::milliseconds(5));
    });

    tbb::flow::function_node< int, tbb::flow::continue_msg, tbb::flow::rejecting > 
            second_worker(g, tbb::flow::serial, [] (const int& data) {
                 printf("Process data with second worker: %d\n", data);
                 // Long computation
                 std::this_thread::sleep_for(std::chrono::milliseconds(5));
    });

    tbb::flow::make_edge(prepare, load_balancer);
    tbb::flow::make_edge(load_balancer, first_worker);
    tbb::flow::make_edge(load_balancer, second_worker);

    for (int i = 0; i < 10; ++i) {
        prepare.try_put(i);
    }

    g.wait_for_all();

    return 0;
}

メンバー

namespace tbb {
namespace flow {
 
 
template< typename T, typename A = cache_aligned_allocator<T> >
class buffer_node :
  public graph_node, public receiver<T>, public sender<T> {
public:
    explicit buffer_node( graph &g );
    buffer_node( const buffer_node &src );
 
    // receiver<T>
    typedef T input_type;
    typedef sender<input_type> predecessor_type;
    bool try_put( const input_type &v );
    bool register_predecessor( predecessor_type &p );
    bool remove_predecessor( predecessor_type &p );
 
    // sender<T>
    typedef T output_type;
    typedef receiver<output_type> successor_type;
    bool register_successor( successor_type &r );
    bool remove_successor( successor_type &r );
    bool try_get( output_type &v );
    bool try_reserve( output_type &v );
    bool try_release( );
    bool try_consume( );
};
 
}
}
次の表は、このクラスのメンバーの詳細な情報を提供します。
メンバー 説明
explicit buffer_node( graph &g )

graph g に属する空の buffer_node を構築します。

buffer_node( const buffer_node &src )

空の buffer_node を構築します。バッファーに格納される値とサクセサーのリストは src からコピーされません。

bool try_put( const input_type &v )

バッファーに v を追加します。v がバッファーの唯一のアイテムの場合、アイテムをサクセサーに転送するタスクがスポーンされます。

戻り値: true

bool register_predecessor( predecessor_type &p )

値を拒否しないため、プレデセッサーのリストを維持する必要はありません。

戻り値: false

bool remove_predecessor( predecessor_type &p )

値を拒否しないため、プレデセッサーのリストを維持する必要はありません。

戻り値: false

bool register_successor( successor_type &r )

サクセサーのセットに r を追加します。

戻り値: true

bool remove_successor( successor_type &r )

サクセサーのセットから r を削除します。

戻り値: true

bool try_get( output_type &v )

戻り値: バッファーから削除して v に代入できるアイテムがある場合は true。バッファーに現在予約されていないアイテムがない場合は false

bool try_reserve( output_type &v )

現在保持されている予約がなく、バッファーに利用可能なアイテムが少なくとも 1 つある場合、新しく予約するアイテムを v に代入します。新しい予約が行われると、バッファーは予約済みとしてマークされます。

戻り値: 新しく予約するアイテムが v に代入された場合は ture。その他の場合は false

bool try_release( )

バッファーの予約を解除します。最後に成功した try_reserve の呼び出しで返されたアイテムがバッファーに残されます。

戻り値: バッファーが現在予約されている場合は true。その他の場合は false

bool try_consume( )

バッファーの予約を解除します。最後に成功した try_reserve の呼び出しで返されたアイテムがバッファーに残されます。

戻り値: バッファーが現在予約されている場合は true。その他の場合は false

関連情報