cartographer线程池ThreadPool::DoWork()

Posted COCO_PEAK_NOODLE

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cartographer线程池ThreadPool::DoWork()相关的知识,希望对你有一定的参考价值。

absl::MutexLock locker(&mutex_);保证各个线程按序排队领任务,领完任务后,就并行计算各自的任务了(Execute(task.get());)
线程中执行的函数好像是进行了拷贝,所以线程之间调用同一个函数,看似是应该排队,但实际是相互不影响的。

void ThreadPool::DoWork() 
#ifdef __linux__
  // This changes the per-thread nice level of the current thread on Linux. We
  // do this so that the background work done by the thread pool is not taking
  // away CPU resources from more important foreground threads.
  CHECK_NE(nice(10), -1);
#endif
  const auto predicate = [this]() EXCLUSIVE_LOCKS_REQUIRED(mutex_) 
    return !task_queue_.empty() || !running_;
  ;
  for (;;) 
    std::shared_ptr<Task> task;
    
      absl::MutexLock locker(&mutex_);
      mutex_.Await(absl::Condition(&predicate));
      if (!task_queue_.empty()) 
        task = std::move(task_queue_.front());
        //LOG(INFO) << "this thread " << this << " start task " << task.get();
        //absl::SleepFor(absl::Seconds(3));
        task_queue_.pop_front();
       else if (!running_) 
        return;
      

      //LOG(INFO) << "this thread " << this << " end   task " << task.get();

    

        
    CHECK(task);
    CHECK_EQ(task->GetState(), common::Task::DEPENDENCIES_COMPLETED);
    //LOG(INFO) << "this thread " << this << " start task " << task.get();
    //absl::SleepFor(absl::Seconds(3));
    Execute(task.get());
    //LOG(INFO) << "this thread " << this << " end   task " << task.get();
  

以上是关于cartographer线程池ThreadPool::DoWork()的主要内容,如果未能解决你的问题,请参考以下文章

Cartographer中的线程池操作

多线程之旅(ThreadPool 线程池)

C#多线程--线程池(ThreadPool)

ThreadPool类(线程池)

线程池(ThreadPool)

多线程二:线程池(ThreadPool)