Skip to content

Update README with more precise description of the queue behavior #41

@frankist

Description

@frankist

I am opening this issue because I found that the README didn't explain with precision the behavior that is expected from the MPMC queue.

In the README, one can read the following:

bool try_push(const T &v);

Try to enqueue an item using copy construction. Returns true on success and false if queue is full.

However, if I run the following test, it fails:

TEST(mpmc_queue_test, push_after_pop_should_not_fail)
{
  rigtorp::MPMCQueue<int> q(256);
  for (int i = 0; i != 256; ++i) {
    ASSERT_TRUE(q.try_push(i));
  }
  std::atomic<bool> running{true};

  auto task = [&q, &running]() {
    while (running) {
      int v;
      if (q.try_pop(v)) {
        ASSERT_TRUE(q.try_push(v));
      }
    }
  };

  std::thread t1{task};
  std::thread t2{task};
  std::thread t3{task};
  std::thread t4{task};

  std::this_thread::sleep_for(std::chrono::seconds{5});
  running = false;
  t1.join();
  t2.join();
  t3.join();
  t4.join();
}

Notice that the try_push inside the lambda is always going to push to a queue that is never full. This is guaranteed via the try_pop just one line above. If there can be spurious failures when multiple writers call try_push concurrently, then this should be clear in the function description in the README.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions