is it correct to use memory_order_relaxed in ProducerConsumerQueue::read

236 views
Skip to first unread message

Derek

unread,
Jun 26, 2012, 6:00:22 PM6/26/12
to faceboo...@googlegroups.com
ProducerConsumerQueue::read has below code in the beginning to check if queue is empty, memory_order_relaxed looks bit suspicious. Presume the reason isEmpty is not called here is to avoid load readIndex twice, but isEmpty uses memory_order_consume on both readIndex and writeIndex.

    auto const currentRead = readIndex_.load(std::memory_order_relaxed);
    if (currentRead == writeIndex_.load(std::memory_order_acquire)) {
      // queue is empty
      return false;
    }

Derek

unread,
Jun 26, 2012, 6:04:43 PM6/26/12
to faceboo...@googlegroups.com

realized I was wrong when looking at write function. Since read will only be called by the reader thread, and readIndex is increased by reader thread only, memory_order_relaxed should be fine for reader thread.
Reply all
Reply to author
Forward
0 new messages