diff options
author | Geir Storli <geirst@yahooinc.com> | 2023-02-24 17:15:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-24 17:15:11 +0100 |
commit | 5b1995344966b854ddbdf3810c3f5614c30bea2f (patch) | |
tree | cd5444d6c993e940ab0a274cfce31dfc80793714 /vespalib | |
parent | f82bc56396e7251aca9c46a067d189afddb077cc (diff) | |
parent | a907c35031c5a09fb2fc76080f273ff95e663e65 (diff) |
Merge pull request #26175 from vespa-engine/havardpe/avoid-fastos-thread-in-storage
avoid using fastos thread in storage
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/thread/thread_test.cpp | 8 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/thread.h | 6 |
2 files changed, 12 insertions, 2 deletions
diff --git a/vespalib/src/tests/thread/thread_test.cpp b/vespalib/src/tests/thread/thread_test.cpp index cde8a3596bf..077b85ea1ac 100644 --- a/vespalib/src/tests/thread/thread_test.cpp +++ b/vespalib/src/tests/thread/thread_test.cpp @@ -50,9 +50,17 @@ TEST("use thread pool to run multiple things") { bool init_called = false; bool was_run = false; ThreadPool pool; + EXPECT_TRUE(pool.empty()); + EXPECT_EQUAL(pool.size(), 0u); pool.start(my_fun, &was_run); + EXPECT_TRUE(!pool.empty()); + EXPECT_EQUAL(pool.size(), 1u); pool.start(agent, wrap(test_agent_thread, &init_called)); + EXPECT_TRUE(!pool.empty()); + EXPECT_EQUAL(pool.size(), 2u); pool.join(); + EXPECT_TRUE(pool.empty()); + EXPECT_EQUAL(pool.size(), 0u); EXPECT_TRUE(init_called); EXPECT_TRUE(agent.was_run); EXPECT_TRUE(was_run); diff --git a/vespalib/src/vespa/vespalib/util/thread.h b/vespalib/src/vespa/vespalib/util/thread.h index 2a5693d2d26..9f3ebd89165 100644 --- a/vespalib/src/vespa/vespalib/util/thread.h +++ b/vespalib/src/vespa/vespalib/util/thread.h @@ -24,16 +24,18 @@ private: public: ThreadPool() noexcept : _threads() {} void start(Runnable &runnable, Runnable::init_fun_t init_fun) { - _threads.reserve(_threads.size() + 1); + reserve(size() + 1); _threads.push_back(thread::start(runnable, std::move(init_fun))); } template<typename F, typename... Args> requires std::invocable<F,Args...> void start(F &&f, Args && ... args) { - _threads.reserve(_threads.size() + 1); + reserve(size() + 1); _threads.emplace_back(std::forward<F>(f), std::forward<Args>(args)...); }; + void reserve(size_t capacity) { _threads.reserve(capacity); } size_t size() const { return _threads.size(); } + bool empty() const { return _threads.empty(); } void join() { for (auto &thread: _threads) { thread.join(); |