From ad93efafe98029ff72dec710949ea168d648dbd7 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sun, 4 Sep 2022 19:48:08 -0400 Subject: format thread_pool.hpp --- mingui/main.cpp | 1 - thread_pool.hpp | 222 ++++++++++++++++++++++++++++++-------------------------- 2 files changed, 121 insertions(+), 102 deletions(-) diff --git a/mingui/main.cpp b/mingui/main.cpp index 57c2fb1..8199cb9 100644 --- a/mingui/main.cpp +++ b/mingui/main.cpp @@ -136,7 +136,6 @@ int main(int argc, char **argv) w->show_images(build_list(lists[curlist])); w->update_distances(build_dists(lists[curlist])); w->update_viewstatus(curlist, lists.size()); - }); QObject::connect(w, &MinGuiWidget::prev, []{ diff --git a/thread_pool.hpp b/thread_pool.hpp index ee661ce..c28b5e8 100644 --- a/thread_pool.hpp +++ b/thread_pool.hpp @@ -14,114 +14,134 @@ template class _atomic_queue { public: - void push(T&v) - { - std::unique_lock lck(mtx); - q.push(v); - } - bool pop(T&v) - { - std::unique_lock lck(mtx); - if(!q.empty()) - { - v=std::move(q.front()); - q.pop(); - return true; - } - return false; - } - size_t size() - { - std::unique_lock lck(mtx); - return q.size(); - } + void push(T&v) + { + std::unique_lock lck(mtx); + q.push(v); + } + bool pop(T&v) + { + std::unique_lock lck(mtx); + if(!q.empty()) + { + v = std::move(q.front()); + q.pop(); + return true; + } + return false; + } + size_t size() + { + std::unique_lock lck(mtx); + return q.size(); + } private: - std::queue q; - std::mutex mtx; + std::queue q; + std::mutex mtx; }; class thread_pool { public: - thread_pool(size_t njobs):waiting_threads(0),stop(false),wait_interrupt(false) - { - thr.resize(njobs); - thstop.resize(njobs); - for(size_t i=0;i>(false); - auto looper=[this,i,cstop]{ - std::atomic&stop=*cstop; - std::function *f; - bool popped=wq.pop(f); - while(1) - { - for(;popped;popped=wq.pop(f)) - { - std::unique_ptr> pf(f); - (*f)(i); - if(stop)return; - } - std::unique_lock lck(mtx); - ++waiting_threads; - cv.wait(lck,[this,&f,&popped,&stop]{ - popped=wq.pop(f); - return popped||wait_interrupt||stop; - }); - --waiting_threads; - if(!popped)return; - } - }; - thr[i].reset(new std::thread(looper)); - } - } - template - auto create_task(F&&f,A&&...args)->std::future - { - auto task=std::make_shared>( - std::bind(std::forward(f),std::placeholders::_1,std::forward(args)...) - ); - auto worktask=new std::function([task](int id){(*task)(id);}); - wq.push(worktask); - std::unique_lock lck(mtx); - cv.notify_one(); - return task->get_future(); - } - void wait() - { - if(!stop)wait_interrupt=true; - { - std::unique_lock lck(mtx); - cv.notify_all(); - } - for(size_t i=0;ijoinable())thr[i]->join(); - std::function *f; - while(wq.size()){wq.pop(f);delete f;} - thr.clear();thstop.clear(); - } - void terminate() - { - stop=true; - std::function *f; - while(wq.size()){wq.pop(f);delete f;} - for(size_t i=0;i lck(mtx); - cv.notify_all(); - } - for(size_t i=0;ijoinable())thr[i]->join(); - while(wq.size()){wq.pop(f);delete f;} - thr.clear();thstop.clear(); - } + thread_pool(size_t njobs): waiting_threads(0), stop(false), wait_interrupt(false) + { + thr.resize(njobs); + thstop.resize(njobs); + for(size_t i = 0; i < njobs; ++i) + { + auto cstop = thstop[i] = std::make_shared>(false); + auto looper = [this, i, cstop] + { + std::atomic&stop = *cstop; + std::function *f; + bool popped = wq.pop(f); + while(1) + { + for(; popped; popped = wq.pop(f)) + { + std::unique_ptr> pf(f); + (*f)(i); + if(stop)return; + } + std::unique_lock lck(mtx); + ++waiting_threads; + cv.wait(lck, [this, &f, &popped, &stop] + { + popped = wq.pop(f); + return popped || wait_interrupt || stop; + }); + --waiting_threads; + if(!popped)return; + } + }; + thr[i].reset(new std::thread(looper)); + } + } + template + auto create_task(F&&f, A&&...args)->std::future + { + auto task = std::make_shared>( + std::bind(std::forward(f), std::placeholders::_1, std::forward(args)...) + ); + auto worktask = new std::function([task](int id) + { + (*task)(id); + }); + wq.push(worktask); + std::unique_lock lck(mtx); + cv.notify_one(); + return task->get_future(); + } + void wait() + { + if(!stop) + wait_interrupt = true; + { + std::unique_lock lck(mtx); + cv.notify_all(); + } + for(size_t i = 0; i < thr.size(); ++i)if(thr[i]->joinable())thr[i]->join(); + std::function *f; + while(wq.size()) + { + wq.pop(f); + delete f; + } + thr.clear(); + thstop.clear(); + } + void terminate() + { + stop = true; + std::function *f; + while(wq.size()) + { + wq.pop(f); + delete f; + } + for(size_t i = 0; i < thstop.size(); ++i)*thstop[i] = true; + { + std::unique_lock lck(mtx); + cv.notify_all(); + } + for(size_t i = 0; i < thr.size(); ++i)if(thr[i]->joinable())thr[i]->join(); + while(wq.size()) + { + wq.pop(f); + delete f; + } + thr.clear(); + thstop.clear(); + } private: - std::vector> thr; - std::vector>> thstop; - _atomic_queue*> wq; - std::atomic wait_interrupt; - std::atomic stop; - std::atomic waiting_threads; - std::mutex mtx; - std::condition_variable cv; + std::vector> thr; + std::vector>> thstop; + _atomic_queue*> wq; + std::atomic wait_interrupt; + std::atomic stop; + std::atomic waiting_threads; + std::mutex mtx; + std::condition_variable cv; }; #endif -- cgit v1.2.3