From ba97aeb0a6ff52b79f43f883c639a55c8e3cf78d Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 20 Apr 2022 00:48:44 +0800 Subject: Reserve vector thread_pool::tasks to speed up task initializing. --- threading.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'threading.h') diff --git a/threading.h b/threading.h index 6d79494..249a115 100644 --- a/threading.h +++ b/threading.h @@ -41,9 +41,12 @@ class thread_pool { void worker_main(); public: - explicit thread_pool(unsigned thread_count, const U &shared_ctx, V &mut_shared_ctx) : + thread_pool(unsigned thread_count, const U &shared_ctx, V &mut_shared_ctx, size_t reserve_tasks = -1) : thread_count{thread_count}, shared_ctx{shared_ctx}, mut_shared_ctx{mut_shared_ctx} { std::cerr << "Using " << (counter.is_lock_free() ? "lock-free" : "locking") << " dispatcher." << std::endl; + if (reserve_tasks > 0) { + tasks.reserve(reserve_tasks); + } } // Thread unsafe! -- cgit v1.2.3