MPMCQueue 是一个用C ++ 11编写的有界多生产者多用户无锁队列。
MPMCQueue<int> q(10); auto t1 = std::thread([&] { int v; q.pop(v); std::cout << "t1 " << v << "\n"; }); auto t2 = std::thread([&] { int v; q.pop(v); std::cout << "t2 " << v << "\n"; }); q.push(1); q.push(2); t1.join(); t2.join();
MPMCQueue<T>(size_t capacity);
Constructs a new MPMCQueue holding items of type T with capacity capacity.
MPMCQueue
T
capacity
void emplace(Args &&... args);
Enqueue an item using inplace construction. Blocks if queue is full.
bool try_emplace(Args &&... args);
Try to enqueue an item using inplace construction. Returns true on success and false if queue is full.
true
false
bool push(const T &v);
Enqueue an item using copy construction. Blocks if queue is full.
template <typename P> bool push(P &&v);
Enqueue an item using move construction. Participates in overload resolution only if std::is_nothrow_constructible<T, P&&>::value == true. Blocks if queue is full.
std::is_nothrow_constructible<T, P&&>::value == true
bool try_push(const T &v);
Try to enqueue an item using copy construction. Returns true on success and false if queue is full.
template <typename P> bool try_push(P &&v);
Try to enqueue an item using move construction. Participates in overload resolution only if std::is_nothrow_constructible<T, P&&>::value == true. Returns true on success and false if queue is full.
void pop(T &v);
Dequeue an item by copying or moving the item into v. Blocks if queue is empty.
v
bool try_pop(T &v);
Try to dequeue an item by copying or moving the item into v. Return true on sucess and false if the queue is empty.
所有操作都是线程安全的,除了构造和析构函数。
Enqeue:
Dequeue:
参考资料: