74 std::thread m_running_thread;
95 Worker() { m_is_running.red(); }
106 m_push_worker = [
tp,id]()->
void {
tp->push_worker(
id ); };
107 m_running_thread = std::thread( &Worker::worker_loop,
this );
119 m_is_running.green();
120 if ( m_running_thread.joinable() ) m_running_thread.join();
126 void wait() { m_is_running.wait_red(); }
135 m_is_running.wait_red();
136 m_job = std::move(fun);
137 m_is_running.green();
147 std::vector<Worker> m_workers;
148 std::list<unsigned> m_queue;
149 std::mutex m_queue_mutex;
150 std::condition_variable m_queue_cond;
158 push_worker(
unsigned id ) {
159 std::unique_lock<std::mutex> lock(m_queue_mutex);
160 m_queue.emplace_back(
id);
161 m_queue_cond.notify_one();
171 std::unique_lock<std::mutex> lock(m_queue_mutex);
172 while ( m_queue.empty() ) m_queue_cond.wait( lock );
173 unsigned id{ m_queue.back() }; m_queue.pop_back();
185 unsigned nthread = std::max(
187 unsigned(std::thread::hardware_concurrency()-1)
191 , m_workers( size_t(nthread) )
195 for ( Worker & w : m_workers ) {
new (&w) Worker(
this,
id ); ++id; }
196 while (
id-- > 0 ) push_worker(
id );
218 m_workers[pop_worker()].exec( std::move(fun) );
226 {
for (
auto & w : m_workers ) w.wait(); }
235 {
return unsigned(m_workers.size()); }
242 static char const *
Name() {
return "ThreadPool5"; }
244 char const *
name()
const override {
return Name(); }
void exec(FUN &&fun) override
Executes a task and assigns it to an available worker.
Definition ThreadPool5.hxx:216
void wait() override
Waits for all tasks to be completed.
Definition ThreadPool5.hxx:225
virtual ~ThreadPool5()
Destructor for the ThreadPool5 class.
Definition ThreadPool5.hxx:205
unsigned thread_count() const override
Gets the current number of threads in the pool.
Definition ThreadPool5.hxx:234
char const * name() const override
Definition ThreadPool5.hxx:244
static char const * Name()
Gets the name of the thread pool implementation.
Definition ThreadPool5.hxx:242
ThreadPool5(unsigned nthread=std::max(unsigned(1), unsigned(std::thread::hardware_concurrency() -1)))
Constructs a new ThreadPool5 instance with a specified number of threads.
Definition ThreadPool5.hxx:184
ThreadPoolBase(ThreadPoolBase const &)=delete
std::function< void(void)> FUN
Definition ThreadPoolBase.hxx:55
#define UTILS_SEMAPHORE
Definition ThreadUtils.hxx:42
Definition ThreadPoolBase.hxx:87
Definition SystemUtils.cc:39