cameron314 / cameron314/concurrentqueue
Inconsistent order of enqueue and try_dequeue
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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