std::tuple WorkQueue::pop()

in e2e-examples/gcs/benchmark/work_queue.cc [25:52]


std::tuple<int, int> WorkQueue::pop(int thread_id) {
  if (thread_id < 1 || thread_id > thread_count_) {
    return std::make_tuple(0, 0);
  }

  absl::MutexLock l(&mu_);

  // Pop the next work if the current thread still has remaining works
  int& cur_thread_work = thread_works_[thread_id - 1];
  if (cur_thread_work < work_count_per_thread_) {
    cur_thread_work += 1;
    return std::make_tuple(thread_id, cur_thread_work);
  }

  // Try to steal a job from other threads if it's enabled
  if (work_stealing_enabled_) {
    for (int t = 0; t < thread_count_; t++) {
      int& t_work = thread_works_[t];
      if (t_work < work_count_per_thread_) {
        t_work += 1;
        return std::make_tuple(t + 1, t_work);
      }
    }
  }

  // Otherwise nothing to do
  return std::make_tuple(0, 0);
}