class aggregator;
#define TBB_PREVIEW_AGGREGATOR 1 #include "tbb/aggregator.h"
aggregator は、操作の排他制御を許可するという点で mutex に似ていますが、インターフェイスは大きく異なります。 実行するには、操作 (関数ボディーやラムダ関数) は aggregator オブジェクトの execute メソッドを使用して aggregator に渡されます。 同じ aggregator オブジェクトに渡された操作は互いに排他的に実行されます。 execute メソッドは、渡された関数の実行が完了した後にリターンします。
namespace tbb {
class aggregator {
public:
aggregator();
template<typename Body>
void execute(const Body& b);
};
}
| メンバー | 説明 |
|---|---|
| aggregator() |
aggregator オブジェクトを構築します。 |
| template<typename Body> void execute(const Body& b) |
互いに排他的に実行されるように b を aggregator に渡します。 b の実行が完了するとリターンします。 |
次の例は、aggregator を使用して、コンカレントでない std::priority_queue コンテナーを安全に処理します。
typedef priority_queue<value_type, vector<value_type>, compare_type> pq_t;
pq_t my_pq;
aggregator my_aggregator;
value_type elem = 42;
// 要素をプライオリティー・キューに格納
my_aggregator.execute( [&my_pq, &elem](){ my_pq.push(elem); } );
// 要素をプライオリティー・キューから取得
bool result = false;
my_aggregator.execute( [&my_pq, &elem, &result](){
if (!my_pq.empty()) {
result = true;
elem = my_pq.top();
my_pq.pop();
}
} );