Boost.Lockfreeのキューには、境界チェックのないpush()と、境界チェックのあるbounded_push()という、2種類の値追加関数があります。
まず、境界チェックなしのpush()メンバ関数。こちらは、容量を超えてpushした場合に、容量が自動伸長します。
#include <iostream> #include <boost/lockfree/queue.hpp> int main() { boost::lockfree::queue<int> que(3); std::cout << std::boolalpha; std::cout << que.push(1) << std::endl; std::cout << que.push(2) << std::endl; std::cout << que.push(3) << std::endl; std::cout << que.push(4) << std::endl; while (!que.empty()) { int x = 0; que.pop(x); std::cout << x << std::endl; } }
true true true true 1 2 3 4
容量3のキューに4要素を追加して、全ての追加に成功し、実際に値が入っていることがわかります。
境界チェックありのbounded_push()メンバ関数は、容量を超えてpushした場合に、追加に失敗し、falseを返します。
#include <iostream> #include <boost/lockfree/queue.hpp> int main() { boost::lockfree::queue<int> que(3); std::cout << std::boolalpha; std::cout << que.bounded_push(1) << std::endl; std::cout << que.bounded_push(2) << std::endl; std::cout << que.bounded_push(3) << std::endl; std::cout << que.bounded_push(4) << std::endl; while (!que.empty()) { int x = 0; que.pop(x); std::cout << x << std::endl; } }
true true true false 1 2 3
4つめの要素追加に失敗していることがわかります。