65 std::mutex m_task_mutex;
66 std::condition_variable m_task_cv;
69 std::thread m_running_thread;
70 bool m_task_done{
true};
76 std::unique_lock<std::mutex> lock(m_task_mutex);
78 while ( m_task_done ) m_task_cv.wait( lock );
81 m_task_cv.notify_one();
88 Worker( Worker
const & ) =
delete;
90 Worker& operator = ( Worker
const & ) =
delete;
91 Worker& operator = ( Worker && ) =
delete;
96 Worker() : m_running_thread( std::thread( &Worker::worker_loop,
this ) ) {}
102 auto dummy_task = []()->
void{};
105 if ( m_running_thread.joinable() ) m_running_thread.join();
111 Worker( Worker && rhs ) noexcept
112 : m_active(rhs.m_active)
113 , m_running_thread(std::move(rhs.m_running_thread))
114 , m_task(std::move(rhs.m_task))
122 std::unique_lock<std::mutex> lock(m_task_mutex);
123 while ( !m_task_done ) m_task_cv.wait( lock );
134 std::unique_lock<std::mutex> lock(m_task_mutex);
135 while ( !m_task_done ) m_task_cv.wait( lock );
137 m_task = std::move(fun);
138 m_task_cv.notify_one();
143 std::size_t m_thread_to_send{0};
146 std::vector<Worker> m_workers;
157 unsigned nthread = std::max(
159 unsigned(std::thread::hardware_concurrency()-1)
176 m_workers[m_thread_to_send].exec( std::move(fun) );
177 if ( ++m_thread_to_send >= m_workers.size() ) m_thread_to_send = 0;
181 void wait()
override {
for (
auto && w : m_workers ) w.wait(); }
188 unsigned thread_count()
const override {
return unsigned(m_workers.size()); }
190 static char const *
Name() {
return "ThreadPool1"; }
192 char const *
name()
const override {
return Name(); }
ThreadPool1(unsigned nthread=std::max(unsigned(1), unsigned(std::thread::hardware_concurrency() -1)))
Constructs a new ThreadPool1 with a specified number of threads.
Definition ThreadPool1.hxx:156