diff --git a/src/circ_queue.h b/src/circ_queue.h index e510760..d191177 100644 --- a/src/circ_queue.h +++ b/src/circ_queue.h @@ -83,10 +83,11 @@ public: void* acquire(void) { auto st = elem_start() + id(cr_.load(std::memory_order_relaxed)); + // check remain count of consumers do { - // check remain count of consumers - if (!st->head_.load(std::memory_order_acquire)) { - st->head_.store(conn_count()); + std::size_t expected = 0; + if (st->head_.compare_exchange_weak(expected, conn_count(), + std::memory_order_acquire, std::memory_order_relaxed)) { break; } } while(1); @@ -105,7 +106,7 @@ public: return (elem_start() + id(index))->data_; } - void commit(std::uint16_t index) { + void put(std::uint16_t index) { auto st = elem_start() + id(index); st->head_.fetch_sub(1, std::memory_order_release); } diff --git a/test/test_circ_queue.cpp b/test/test_circ_queue.cpp index 0f5ef08..22d2209 100644 --- a/test/test_circ_queue.cpp +++ b/test/test_circ_queue.cpp @@ -53,7 +53,7 @@ void Unit::test_producer(void) { do { while (cur != cq__->cursor()) { int d = *static_cast(cq__->get(cur)); - cq__->commit(cur); + cq__->put(cur); if (d < 0) return; cur = cq__->next(cur); list.push_back(d);