Skip to content

Commit 0a8d717

Browse files
author
Andrei Lobov
authored
Start function (iresearch-toolkit#577)
* Start function * wip * fix
1 parent e62c027 commit 0a8d717

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

core/utils/async_utils.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,19 @@ void busywait_mutex::unlock() noexcept {
5555
}
5656

5757
template<bool UseDelay>
58-
ThreadPool<UseDelay>::ThreadPool(size_t threads, basic_string_view<Char> name)
59-
: name_{name} {
58+
ThreadPool<UseDelay>::ThreadPool(size_t threads, basic_string_view<Char> name) {
59+
start(threads, name);
60+
}
61+
62+
template<bool UseDelay>
63+
void ThreadPool<UseDelay>::start(size_t threads, basic_string_view<Char> name) {
64+
IRS_ASSERT(threads_.empty());
6065
threads_.reserve(threads);
6166
for (size_t i = 0; i != threads; ++i) {
62-
threads_.emplace_back([&] {
63-
if (!name_.empty()) {
64-
set_thread_name(name_.c_str());
67+
threads_.emplace_back([this, name] {
68+
if (!name.empty()) {
69+
IRS_ASSERT(std::char_traits<Char>::length(name.data()) == name.size());
70+
set_thread_name(name.data());
6571
}
6672
Work();
6773
});

core/utils/async_utils.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ class ThreadPool {
5757
using Clock = std::chrono::steady_clock;
5858
using Func = fu2::unique_function<void()>;
5959

60+
ThreadPool() = default;
6061
explicit ThreadPool(size_t threads, basic_string_view<Char> name = {});
6162
~ThreadPool() { stop(true); }
6263

64+
void start(size_t threads, basic_string_view<Char> name = {});
6365
bool run(Func&& fn, Clock::duration delay = {});
6466
void stop(bool skip_pending = false) noexcept; // always a blocking call
6567
size_t tasks_active() const {
@@ -95,7 +97,6 @@ class ThreadPool {
9597

9698
bool WasStop() const { return state_ % 2 != 0; }
9799

98-
basic_string<Char> name_;
99100
std::vector<std::thread> threads_;
100101
mutable std::mutex m_;
101102
std::condition_variable cv_;

0 commit comments

Comments
 (0)