concurrent_queue テンプレート・クラス

概要

並列操作を含むキュー用のテンプレート・クラス。

構文

template<typename T, typename Alloc=cache_aligned_allocator<T> > class concurrent_queue;

ヘッダー

#include "tbb/concurrent_queue.h"

説明

concurrent_queue は、複数のスレッドが項目のプッシュとポップを同時に行うことができる FIFO (先入れ先出し) データ構造です。 大きさは無制限で、対象のマシン上のメモリー制限に従います。

インターフェイスは、同時変更を安全に行えるようにしている点を除いて、STL std::queue に似ています。

STL のキューとインテル® スレッディング・ビルディング・ブロック (インテル® TBB) の concurrent_queue の相違点

機能

STL std::queue

concurrent_queue

front および back へのアクセス

front メソッドと back メソッド

提供されません。並列操作が行われている間は安全ではありません。

size_type

符号なし整数型

符号付き 整数型

unsafe_size()

キューの項目の数を返します

キューの項目の数を返します。同時に処理中の push 操作または try_pop 操作がある場合は誤った値を返す可能性があります。

キュー q が空でなければ、項目をコピーしてポップする

bool b=!q.empty();

if(b) {

    x=q.front();

    q.pop();

}

bool b = q.try_pop (x)

メンバー

namespace tbb {
                    template<typename T, 
                 typename Alloc=cache_aligned_allocator<T> >
        class concurrent_queue {
        public:
            // 型
            typedef T value_type;
            typedef T& reference;
            typedef const T& const_reference;
            typedef std::ptrdiff_t size_type;
            typedef std::ptrdiff_t difference_type;
            typedef Alloc allocator_type;
     
            explicit concurrent_queue(const Alloc& a = Alloc ());
            concurrent_queue(const concurrent_queue& src,
                             const Alloc& a = Alloc());
            template<typename InputIterator>
            concurrent_queue(InputIterator first, InputIterator last,
                             const Alloc& a = Alloc());
            ~concurrent_queue();
     
            void push( const T& source );
            bool try_pop T& destination );
            void clear() ;
     
            size_type unsafe_size() const;
            bool empty() const;
            Alloc get_allocator() const;
     
            typedef implementation-defined iterator;
            typedef implementation-defined const_iterator;
     
            // イテレーター (低速、デバッグ用)
            iterator unsafe_begin();
            iterator unsafe_end();
            const_iterator unsafe_begin() const;
            const_iterator unsafe_end() const;
        };
    }

インテル® TBB 2.1 では、concurrent_queue は制限することができます。 インテル® TBB 2.2 では、この機能がconcurrent_bounded_queue に移動しました。 TBB_DEPRECATED=1 を指定してコンパイルすると以前の機能を使用できますが、concurrent_bounded_queue を使用することを推奨します。

次の表は、このテンプレート・クラスのメンバーの詳細な情報を提供します。
メンバー 説明
concurrent_queue( const Alloc& a = Alloc () )

空のキューを構築します。

concurrent_queue( const concurrent_queue& src, const Alloc& a = Alloc() )

src のコピーを構築します。

template<typename InputIterator> concurrent_queue( InputIterator first, InputIterator last, const Alloc& a = Alloc() )

半開区間 [first,last) のイテレーターの要素のコピーを含むテーブルを構築します。

~concurrent_queue()

キューのすべての項目を破棄します。

void push( const T& source )

source のコピーをキューにプッシュします。

bool try_pop ( T& destination )

値が利用可能な場合、値をキューからポップして、destination に代入し、オリジナルの値を破棄します。 その他の場合は、何もしません。

戻り値: 値がポップされた場合は true、その他の場合は false。

インテル® TBB 2.1 で try_poppop_if_present と呼ばれていました。 以前の名前を使用するには、TBB_DEPRECATED=1 を指定してコンパイルします。

void clear()

キューを消去します。その後、size()==0 にします。

size_type unsafe_size() const

戻り値: キューの項目の数。同時に処理中の変更がある場合、戻り値は実際のキューの項目数とは異なることがあります。

bool empty() const

戻り値: キューに項目がない場合は true。その他の場合は false。

Alloc get_allocator() const

戻り値: キューの構築に使用されるアロケーターのコピー。

イテレーター

concurrent_queue は、単独でプログラマーがデバッグ中にキューを検査できるように制限付きのイテレーター・サポートを提供します。 iterator 型および const_iterator 型を提供します。どちらも、前方イテレーター用の通常の STL 規則に従います。 反復順は、以前プッシュされたものから最近プッシュされたものの順になります。 変更すると、concurrent_queue を参照するすべてのイテレーターが無効になります。

警告

イテレーターは比較的遅いため、デバッグにのみ使用してください。

次のプログラムは、整数 0..9 でキューを構築した後、キューを標準出力にダンプします。 その結果、0 1 2 3 4 5 6 7 8 9 が出力されます。

#include "tbb/concurrent_queue.h"
#include <iostream>
 
using namespace std;
using namespace tbb;
 
int main() {
    concurrent_queue<int> queue;
    for( int i=0; i<10; ++i )
        queue.push(i);
    typedef concurrent_queue<int>::iterator iter;
    for(iter i(queue.unsafe_begin()); i!=queue.unsafe_end(); ++i)
        cout << *i << " ";
    cout << endl;
    return 0;
 
}

次の表は、このテンプレート・クラスのメンバーの詳細な情報を提供します。
メンバー 説明
iterator unsafe_begin()

戻り値: キューの最初を指す iterator

iterator unsafe_end()

戻り値: キューの最後を指す iterator

const_iterator unsafe_begin() const

戻り値: キューの最初を指す const_iterator

const_iterator unsafe_end() const

戻り値: キューの最後を指す const_iterator