Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
frankist opened this issue Oct 16, 2023 · 0 comments
Open

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

frankist opened this issue Oct 16, 2023 · 0 comments

Comments

@frankist
Copy link

frankist commented Oct 16, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant