diff options
author | Alexander Blair <snipa@jagtech.io> | 2020-01-16 17:27:15 -0800 |
---|---|---|
committer | Alexander Blair <snipa@jagtech.io> | 2020-01-16 17:27:15 -0800 |
commit | 3acb22b889168e8a8f326bd9a6778623861a1bc2 (patch) | |
tree | ce8c7765e5b797bfa0a1a65391d0c406d67e82ad /src | |
parent | Merge pull request #6112 (diff) | |
parent | core_tests: reset thread pool between tests (diff) | |
download | monero-3acb22b889168e8a8f326bd9a6778623861a1bc2.tar.xz |
Merge pull request #6115
27522aaa core_tests: reset thread pool between tests (moneromooo-monero)
Diffstat (limited to 'src')
-rw-r--r-- | src/common/threadpool.cpp | 29 | ||||
-rw-r--r-- | src/common/threadpool.h | 5 |
2 files changed, 27 insertions, 7 deletions
diff --git a/src/common/threadpool.cpp b/src/common/threadpool.cpp index 2748c798c..6eb527cdc 100644 --- a/src/common/threadpool.cpp +++ b/src/common/threadpool.cpp @@ -37,16 +37,14 @@ static __thread bool is_leaf = false; namespace tools { threadpool::threadpool(unsigned int max_threads) : running(true), active(0) { - boost::thread::attributes attrs; - attrs.set_stack_size(THREAD_STACK_SIZE); - max = max_threads ? max_threads : tools::get_max_concurrency(); - size_t i = max ? max - 1 : 0; - while(i--) { - threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this, false))); - } + create(max_threads); } threadpool::~threadpool() { + destroy(); +} + +void threadpool::destroy() { try { const boost::unique_lock<boost::mutex> lock(mutex); @@ -64,6 +62,23 @@ threadpool::~threadpool() { try { threads[i].join(); } catch (...) { /* ignore */ } } + threads.clear(); +} + +void threadpool::recycle() { + destroy(); + create(max); +} + +void threadpool::create(unsigned int max_threads) { + boost::thread::attributes attrs; + attrs.set_stack_size(THREAD_STACK_SIZE); + max = max_threads ? max_threads : tools::get_max_concurrency(); + size_t i = max ? max - 1 : 0; + running = true; + while(i--) { + threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this, false))); + } } void threadpool::submit(waiter *obj, std::function<void()> f, bool leaf) { diff --git a/src/common/threadpool.h b/src/common/threadpool.h index 5e490ee7d..a49d0e14f 100644 --- a/src/common/threadpool.h +++ b/src/common/threadpool.h @@ -69,12 +69,17 @@ public: // task to finish. void submit(waiter *waiter, std::function<void()> f, bool leaf = false); + // destroy and recreate threads + void recycle(); + unsigned int get_max_concurrency() const; ~threadpool(); private: threadpool(unsigned int max_threads = 0); + void destroy(); + void create(unsigned int max_threads); typedef struct entry { waiter *wo; std::function<void()> f; |