cameron314 / concurrentqueue

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11
Other
10k stars 1.69k forks source link

First try_queue return 1 always #394

Open Junxia0Wang opened 3 months ago

Junxia0Wang commented 3 months ago

ConcurrentQueue define as : moodycamel::ConcurrentQueue<uint64_t> clocks_;

Enqueue thread:

  while (curr_clock_time < end_clock_time && !stop_) {
    SINFO("ENQUEUE CLOCK: {}", curr_clock_time);
    while (!clocks_.try_enqueue(curr_clock_time)) {
      std::this_thread::sleep_for(std::chrono::nanoseconds(options_.precision));
    }
    curr_clock_time += step;
  }

Dequeue thread:

while (timestamp > clock_time && clocks_.size_approx() > 0) {
      auto msg = rosgraph_msgs::msg::Clock();
      msg.clock.sec = static_cast<int32_t>(clock_time / 1e9);
      msg.clock.nanosec = static_cast<uint32_t>(clock_time % static_cast<uint64_t>(1e9));
      SINFO("DEQUEUE CLOCK: {}", clock_time);
      clock_writer_->publish(msg);
      while (!clocks_.try_dequeue(clock_time) && clocks_.size_approx() > 0) {
        std::this_thread::sleep_for(std::chrono::nanoseconds(options_.precision));
      }
    }

First clock_time get by try_dequeue always is 1.

cameron314 commented 3 months ago

Well, what value is first enqueued? How are you inspecting the first value dequeued?