cameron314 / cameron314/concurrentqueue

Inconsistent order of enqueue and try_dequeue

Open
#316 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
12.5k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Two threads:
one thread continuously generates data per 10ms about and enqueue();
the other thread continuously reads data and processes it. If the queue is empty, it will sleep for 20ms,

but the data obtained by try_dequeue is messy

the code like this:

// thread 1
void SendMessage(message) {
  // message 1:  id = 1
  // message 2:  id = 2
  // message 2:  id = 3
  message_queue_.enqueue(message);
}

// thread 2
void ReceiveMessage() {
   while (is_running_ || message_queue_.size_approx() > 0) {
    if (message_queue_.size_approx() <= 0) {
      std::this_thread::sleep_for(std::chrono::milliseconds(20));
      continue;
    }
    CHECK(message_queue_.try_dequeue(message));
    // message 1: id = 3
    // message 2: id = 2
    //  message 3: id = 1
  }
}

So what is the reason for this phenomenon?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the enqueue, try_dequeue, and size_approx calls in the provided two-thread example, then read the queue's ordering and concurrency semantics. Reproduce the scenario with the stated timing and determine whether the observed order is expected or indicates a defect; document the reason and any required correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.