From 9eb924f64a56918536d310dd89698aa7e148c727 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期四, 06 五月 2021 09:41:26 +0800 Subject: [PATCH] refactor atomic queue. --- src/robust.h | 14 ++++++++------ utest/robust_test.cpp | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/robust.h b/src/robust.h index b7459ad..aa54c2f 100644 --- a/src/robust.h +++ b/src/robust.h @@ -312,9 +312,10 @@ size_type i = 0; do { auto pos = tail(); - auto cur = buf[pos].load(); - r = Empty(cur) && buf[pos].compare_exchange_strong(cur, Enc(d)); - tail_.compare_exchange_strong(pos, Next(pos)); + if (tail_.compare_exchange_strong(pos, Next(pos))) { + auto cur = buf[pos].load(); + r = Empty(cur) && buf[pos].compare_exchange_strong(cur, Enc(d)); + } } while (try_more && !r && ++i < capacity); return r; } @@ -325,9 +326,10 @@ size_type i = 0; do { auto pos = head(); - cur = buf[pos].load(); - r = !Empty(cur) && buf[pos].compare_exchange_strong(cur, 0); - head_.compare_exchange_strong(pos, Next(pos)); + if (head_.compare_exchange_strong(pos, Next(pos))) { + cur = buf[pos].load(); + r = !Empty(cur) && buf[pos].compare_exchange_strong(cur, 0); + } } while (try_more && !r && ++i < capacity); if (r) { d = Dec(cur); } return r; diff --git a/utest/robust_test.cpp b/utest/robust_test.cpp index 7799aad..0ffbcd6 100644 --- a/utest/robust_test.cpp +++ b/utest/robust_test.cpp @@ -18,7 +18,7 @@ BOOST_AUTO_TEST_CASE(QueueTest) { const int nthread = 100; - const uint64_t nmsg = 1000 * 1000 * 10; + const uint64_t nmsg = 1000 * 1000 * 100; SharedMemory &shm = TestShm(); shm.Remove(); @@ -34,7 +34,7 @@ } #if 1 - typedef AtomicQueue<3> Rcb; + typedef AtomicQueue<4> Rcb; Rcb tmp; BOOST_CHECK(tmp.like_empty()); -- Gitblit v1.8.0