knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::ThreadPool::Worker Class Reference

#include <ThreadPool.h>

Collaboration diagram for knowrob::ThreadPool::Worker:

Public Member Functions

 Worker (ThreadPool *thread_pool)
 
 ~Worker ()
 
 Worker (const Worker &)=delete
 
 Worker (ThreadPool *thread_pool)
 
 ~Worker ()
 
 Worker (const Worker &)=delete
 

Protected Member Functions

void run ()
 
void run ()
 

Protected Attributes

ThreadPoolthreadPool_
 
std::atomic< bool > isTerminated_
 
std::atomic< bool > hasTerminateRequest_
 
std::thread thread_
 

Friends

class ThreadPool
 

Detailed Description

A worker thread that pulls work goals from the work queue of a thread pool.

Definition at line 65 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ Worker() [1/4]

ThreadPool::Worker::Worker ( ThreadPool thread_pool)
explicit

Definition at line 101 of file ThreadPool.cpp.

102  : threadPool_(threadPool),
103  isTerminated_(false),
104  hasTerminateRequest_(false),
105  thread_(&Worker::run, this) {
106 }
std::atomic< bool > hasTerminateRequest_
Definition: ThreadPool.h:80
std::atomic< bool > isTerminated_
Definition: ThreadPool.h:79

◆ ~Worker() [1/2]

ThreadPool::Worker::~Worker ( )

Definition at line 108 of file ThreadPool.cpp.

108  {
109  hasTerminateRequest_ = true;
110  if (thread_.joinable()) thread_.join();
111 }

◆ Worker() [2/4]

knowrob::ThreadPool::Worker::Worker ( const Worker )
delete

Cannot be copy-assigned.

◆ Worker() [3/4]

knowrob::ThreadPool::Worker::Worker ( ThreadPool thread_pool)
explicit

◆ ~Worker() [2/2]

knowrob::ThreadPool::Worker::~Worker ( )

◆ Worker() [4/4]

knowrob::ThreadPool::Worker::Worker ( const Worker )
delete

Cannot be copy-assigned.

Member Function Documentation

◆ run() [1/2]

void ThreadPool::Worker::run ( )
protected

Definition at line 113 of file ThreadPool.cpp.

113  {
114  KB_DEBUG("Worker started.");
115  // let the pool do some thread specific initialization
116  if (!threadPool_->initializeWorker()) {
117  KB_ERROR("Worker initialization failed.");
118  isTerminated_ = true;
119  threadPool_->numFinishedThreads_ += 1;
120  return;
121  }
122  threadPool_->numActiveWorker_ += 1;
123 
124  // loop until the application exits
125  while (!hasTerminateRequest_) {
126  // wait for a claim
127  {
128  std::unique_lock<std::mutex> lk(threadPool_->workMutex_);
129  threadPool_->numActiveWorker_ -= 1;
130  if (!hasTerminateRequest_) {
131  threadPool_->workCV_.wait(lk, [this] {
132  return hasTerminateRequest_ || !threadPool_->workQueue_.empty();
133  });
134  }
135  threadPool_->numActiveWorker_ += 1;
136  }
137  if (hasTerminateRequest_) {
138  break;
139  }
140 
141  // pop work from queue
142  auto goal = threadPool_->popWork();
143  // do the work
144  if (goal) {
145  goal->runInternal();
146  }
147  }
148 
149  isTerminated_ = true;
150  // tell the thread pool that a worker thread exited
153  }
154  // note: counter indicates that there are finished threads in workerThreads_ list.
155  threadPool_->numFinishedThreads_ += 1;
156  threadPool_->numActiveWorker_ -= 1;
157  KB_DEBUG("Worker terminated.");
158 }
#define KB_DEBUG
Definition: Logger.h:25
#define KB_ERROR
Definition: Logger.h:28
std::function< void()> finalizeWorker_
Definition: ThreadPool.h:168

◆ run() [2/2]

void knowrob::ThreadPool::Worker::run ( )
protected

Friends And Related Function Documentation

◆ ThreadPool

ThreadPool
friend

Definition at line 86 of file ThreadPool.h.

Member Data Documentation

◆ hasTerminateRequest_

std::atomic< bool > knowrob::ThreadPool::Worker::hasTerminateRequest_
protected

Definition at line 80 of file ThreadPool.h.

◆ isTerminated_

std::atomic< bool > knowrob::ThreadPool::Worker::isTerminated_
protected

Definition at line 79 of file ThreadPool.h.

◆ thread_

std::thread knowrob::ThreadPool::Worker::thread_
protected

Definition at line 82 of file ThreadPool.h.

◆ threadPool_

ThreadPool * knowrob::ThreadPool::Worker::threadPool_
protected

Definition at line 77 of file ThreadPool.h.


The documentation for this class was generated from the following files: