Refactored thread_pool_executor methods
This commit is contained in:
@@ -1,23 +1,93 @@
|
|||||||
/**
|
/**
|
||||||
* src/util/thread_pool_executor.cc
|
* src/util/thread_pool_executor.cc
|
||||||
*
|
*
|
||||||
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
|
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "thread_pool_executor.h"
|
#include "thread_pool_executor.h"
|
||||||
|
|
||||||
namespace clanguml::util {
|
namespace clanguml::util {
|
||||||
|
thread_pool_executor::thread_pool_executor()
|
||||||
|
: thread_pool_executor{0}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
thread_pool_executor::thread_pool_executor(unsigned int pool_size)
|
||||||
|
: done_{false}
|
||||||
|
{
|
||||||
|
if (pool_size == 0U)
|
||||||
|
pool_size = std::thread::hardware_concurrency();
|
||||||
|
|
||||||
|
for (auto i = 0U; i < pool_size; i++) {
|
||||||
|
threads_.push_back(std::thread{&thread_pool_executor::worker, this});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~thread_pool_executor::thread_pool_executor() { stop(); }
|
||||||
|
|
||||||
|
std::future<void> thread_pool_executor::add(std::function<void()> &&task)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> l(tasks_mutex_);
|
||||||
|
|
||||||
|
std::packaged_task<void()> ptask{std::move(task)};
|
||||||
|
auto res = ptask.get_future();
|
||||||
|
|
||||||
|
tasks_.emplace_back(std::move(ptask));
|
||||||
|
|
||||||
|
l.unlock();
|
||||||
|
tasks_cond_.notify_one();
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void thread_pool_executor::stop()
|
||||||
|
{
|
||||||
|
done_ = true;
|
||||||
|
for (auto &thread : threads_) {
|
||||||
|
if (thread.joinable())
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void thread_pool_executor::worker()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
while (!done_) {
|
||||||
|
auto task = get();
|
||||||
|
|
||||||
|
task();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (std::runtime_error &e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::packaged_task<void()> thread_pool_executor::get()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> l(tasks_mutex_);
|
||||||
|
|
||||||
|
while (tasks_.empty()) {
|
||||||
|
if (done_)
|
||||||
|
throw std::runtime_error("Thread pool closing...");
|
||||||
|
|
||||||
|
tasks_cond_.wait_for(l, std::chrono::seconds(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto res = std::move(tasks_.front());
|
||||||
|
tasks_.pop_front();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,78 +25,20 @@
|
|||||||
namespace clanguml::util {
|
namespace clanguml::util {
|
||||||
class thread_pool_executor {
|
class thread_pool_executor {
|
||||||
public:
|
public:
|
||||||
thread_pool_executor()
|
thread_pool_executor();
|
||||||
: thread_pool_executor{0}
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
thread_pool_executor(unsigned int pool_size)
|
thread_pool_executor(unsigned int pool_size);
|
||||||
: done_{false}
|
|
||||||
{
|
|
||||||
if (pool_size == 0U)
|
|
||||||
pool_size = std::thread::hardware_concurrency();
|
|
||||||
|
|
||||||
for (auto i = 0U; i < pool_size; i++) {
|
~thread_pool_executor();
|
||||||
threads_.push_back(
|
|
||||||
std::thread{&thread_pool_executor::worker, this});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~thread_pool_executor() { stop(); }
|
std::future<void> add(std::function<void()> &&task);
|
||||||
|
|
||||||
std::future<void> add(std::function<void()> &&task)
|
void stop();
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> l(tasks_mutex_);
|
|
||||||
|
|
||||||
std::packaged_task<void()> ptask{std::move(task)};
|
|
||||||
auto res = ptask.get_future();
|
|
||||||
|
|
||||||
tasks_.emplace_back(std::move(ptask));
|
|
||||||
|
|
||||||
l.unlock();
|
|
||||||
tasks_cond_.notify_one();
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop()
|
|
||||||
{
|
|
||||||
done_ = true;
|
|
||||||
for (auto &thread : threads_) {
|
|
||||||
if (thread.joinable())
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void worker()
|
void worker();
|
||||||
{
|
|
||||||
try {
|
|
||||||
while (!done_) {
|
|
||||||
auto task = get();
|
|
||||||
|
|
||||||
task();
|
std::packaged_task<void()> get();
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (std::runtime_error &e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::packaged_task<void()> get()
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> l(tasks_mutex_);
|
|
||||||
|
|
||||||
while (tasks_.empty()) {
|
|
||||||
if (done_)
|
|
||||||
throw std::runtime_error("Thread pool closing...");
|
|
||||||
|
|
||||||
tasks_cond_.wait_for(l, std::chrono::seconds(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto res = std::move(tasks_.front());
|
|
||||||
tasks_.pop_front();
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::atomic_bool done_;
|
std::atomic_bool done_;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user