diff --git a/src/common.cpp b/src/common.cpp index 22f5e7e4..7afadf8b 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -10,13 +10,16 @@ namespace common = acquire::sink::zarr::common; common::ThreadPool::ThreadPool(size_t n_threads, std::function err) : error_handler_{ err } - , started_{ false } , should_stop_{ false } { - n_threads_ = std::clamp( + n_threads = std::clamp( n_threads, (size_t)1, (size_t)std::max(std::thread::hardware_concurrency(), (unsigned)1)); + + for (auto i = 0; i < n_threads; ++i) { + threads_.emplace_back([this] { thread_worker_(); }); + } } common::ThreadPool::~ThreadPool() noexcept @@ -24,22 +27,9 @@ common::ThreadPool::~ThreadPool() noexcept await_stop(); } -void -common::ThreadPool::start() -{ - EXPECT(!started_, "Thread pool already started."); - - for (auto i = 0; i < n_threads_; ++i) { - threads_.emplace_back([this] { thread_worker_(); }); - } - started_ = true; -} - void common::ThreadPool::push_to_job_queue(JobT&& job) { - EXPECT(started_, "Cannot push to job queue before starting."); - std::unique_lock lock(jobs_mutex_); jobs_.push(std::move(job)); lock.unlock(); @@ -50,10 +40,6 @@ common::ThreadPool::push_to_job_queue(JobT&& job) void common::ThreadPool::await_stop() noexcept { - if (!started_) { - return; - } - should_stop_ = true; cv_.notify_all(); diff --git a/src/common.hh b/src/common.hh index 93996691..e6dc59f0 100644 --- a/src/common.hh +++ b/src/common.hh @@ -60,20 +60,17 @@ struct ThreadPool final ThreadPool(size_t n_threads, std::function err); ~ThreadPool() noexcept; - void start(); void push_to_job_queue(JobT&& job); void await_stop() noexcept; private: std::function error_handler_; - size_t n_threads_; std::vector threads_; mutable std::mutex jobs_mutex_; std::condition_variable cv_; std::queue jobs_; - bool started_; std::atomic should_stop_; /// Multithreading diff --git a/src/zarr.cpp b/src/zarr.cpp index 64a81759..f629e194 100644 --- a/src/zarr.cpp +++ b/src/zarr.cpp @@ -404,7 +404,6 @@ void zarr::Zarr::start() { error_ = true; - thread_pool_->start(); if (fs::exists(dataset_root_)) { std::error_code ec;