Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions readerwriterqueue.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ©2013-2020 Cameron Desrochers.
// ©2013-2020 Cameron Desrochers.
// Distributed under the simplified BSD license (see the license file that
// should have come with this header).

Expand Down Expand Up @@ -619,14 +619,14 @@ class MOODYCAMEL_MAYBE_ALIGN_TO_CACHELINE ReaderWriterQueue
newBlock->tail = newBlock->localTail = 1;

newBlock->next = tailBlock_->next.load();
tailBlock_->next = newBlock;

// Might be possible for the dequeue thread to see the new tailBlock->next
// *without* seeing the new tailBlock value, but this is OK since it can't
// advance to the next block until tailBlock is set anyway (because the only
// case where it could try to read the next is if it's already at the tailBlock,
// and it won't advance past tailBlock in any circumstance).
// Publish all writes to *newBlock before it becomes reachable via
// tailBlock_->next.
fence(memory_order_release);
tailBlock_->next = newBlock;

// Ensure that readers observing the new tailBlock also observe the
// preceding publication of tailBlock_->next.
fence(memory_order_release);
tailBlock = newBlock;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need a release fence before setting tailBlock itself? Otherwise anything reading tailBlock might not see the latest value of tailBlock->next, which would be the opposite bug.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need a release fence before setting tailBlock itself? Otherwise anything reading tailBlock might not see the latest value of tailBlock->next, which would be the opposite bug.

Yeap, that is right. We need to guarantee that tailBlock's data was prepared before changing it.
I'll restore the second fence and clarify the comment accordingly.

}
Expand Down