Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Start function #577

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions core/utils/async_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ void busywait_mutex::unlock() noexcept {
}

template<bool UseDelay>
ThreadPool<UseDelay>::ThreadPool(size_t threads, basic_string_view<Char> name)
: name_{name} {
ThreadPool<UseDelay>::ThreadPool(size_t threads, basic_string_view<Char> name) {
start(threads, name);
}

template<bool UseDelay>
void ThreadPool<UseDelay>::start(size_t threads, basic_string_view<Char> name) {
IRS_ASSERT(threads_.empty());
threads_.reserve(threads);
for (size_t i = 0; i != threads; ++i) {
threads_.emplace_back([&] {
if (!name_.empty()) {
set_thread_name(name_.c_str());
threads_.emplace_back([this, name] {
if (!name.empty()) {
IRS_ASSERT(std::char_traits<Char>::length(name.data()) == name.size());
set_thread_name(name.data());
}
Work();
});
Expand Down
3 changes: 2 additions & 1 deletion core/utils/async_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ class ThreadPool {
using Clock = std::chrono::steady_clock;
using Func = fu2::unique_function<void()>;

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

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

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

basic_string<Char> name_;
std::vector<std::thread> threads_;
mutable std::mutex m_;
std::condition_variable cv_;
Expand Down