並列操作を含むキュー用のテンプレート・クラス。
template<typename T, typename Alloc=cache_aligned_allocator<T> > class concurrent_queue;
#include "tbb/concurrent_queue.h"
concurrent_queue は、複数のスレッドがアイテムのプッシュとポップを同時に行うことができる FIFO (先入れ先出し) データ構造です。大きさは無制限で、対象のマシン上のメモリー制限に従います。
インターフェイスは、同時変更を安全に行えるようにしている点を除いて、STL std::queue に似ています。
|
機能 |
STL std::queue |
concurrent_queue |
|---|---|---|
|
front および back へのアクセス |
front メソッドと back メソッド |
提供されません。並列操作が行われている間は安全ではありません。 |
|
T 型をデフォルト宣言できるようにする |
× |
○ |
|
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 A = cache_aligned_allocator<T> >
class concurrent_queue {
public:
// 型
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef A allocator_type;
explicit concurrent_queue( const allocator_type& a = allocator_type() );
concurrent_queue( const concurrent_queue& src, const allocator_type& a = allocator_type() );
template<typename InputIterator>
concurrent_queue( InputIterator first, InputIterator last, const allocator_type& a = allocator_type() );
// C++11 仕様
concurrent_queue( concurrent_queue&& src );
concurrent_queue( concurrent_queue&& src, const allocator_type& a );
~concurrent_queue();
void push( const T& source );
bool try_pop( T& destination );
// C++11 仕様
void push( T&& source );
template<typename... Args>
void emplace(Args&&... args);
void clear();
size_type unsafe_size() const;
bool empty() const;
allocator_type 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;
};
}
| メンバー | 説明 |
|---|---|
| concurrent_queue( const allocator_type& a = allocator_type() ) |
空のキューを構築します。 |
| concurrent_queue( const concurrent_queue& src, const allocator_type& a = allocator_type() ) |
src のコピーを構築します。 |
| template<typename InputIterator> concurrent_queue( InputIterator first, InputIterator last, const allocator_type& a = allocator_type() ) |
半開区間 [first,last) のイテレーターの要素のコピーを含むキューを構築します。 |
| concurrent_queue( concurrent_queue&& src ) |
C++11 仕様。src のコンテンツを移動することにより新しいキューを構築します。src は無指定の状態のままですが、安全に破棄することができます。 |
| concurrent_queue( concurrent_queue&& src, const allocator_type& a ) |
C++11 仕様。指定されたアロケーターを使って src のコンテンツを移動することにより新しいキューを構築します。src は無指定の状態のままですが、安全に破棄することができます。 |
| ~concurrent_queue() |
キューのすべてのアイテムを破棄します。 |
| void push( const T& source ) |
source のコピーをキューにプッシュします。 |
| void push(T&& elem) |
C++11 仕様。要素の move コンストラクターを使用することにより指定された要素をキューにプッシュします (利用可能な場合)。 |
| template<typename... Arguments> void emplace(Arguments&&... args); |
C++11 仕様。新しい要素をキューにプッシュします。要素は指定された引数で構築されます。 |
| bool try_pop ( T& destination ) |
値が利用可能な場合、値をキューからポップして、destination に代入し、オリジナルの値を破棄します。その他の場合は、何もしません。 戻り値: 値がポップされた場合は true、その他の場合は false。 |
| void clear() |
キューを消去します。その後、size()==0 にします。 |
| size_type unsafe_size() const |
戻り値: キューのアイテムの数。同時に処理中の変更がある場合、戻り値は実際のキューのアイテム数とは異なることがあります。 |
| bool empty() const |
戻り値: キューにアイテムがない場合は true。その他の場合は false。 |
| allocator_type get_allocator() const |
戻り値: キューの構築に使用されるアロケーターのコピー。 |
concurrent_queue クラスは、次の例外の安全性を保証します。
コンストラクターの push および無指定の try_pop メソッドで、構築中またはデータアイテムのコピー中に例外がスローされた場合:
キューは有効なままです。同じキューの同時処理操作および将来の操作は影響を受けません。
コンストラクターや push によって追加されるデータアイテム、および try_pop によって削除されるデータアイテムは失われます。
concurrent_queue のほかのすべてのメソッドは例外をスローしません。
これらは、T 型のデストラクターが例外をスローしない場合のみ保証されます。その他の場合、動作は不定です。
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。 |