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

Commit

Permalink
Start function (#577)
Browse files Browse the repository at this point in the history
* Start function

* wip

* fix
  • Loading branch information
Dronplane committed Nov 22, 2023
1 parent e62c027 commit 0a8d717
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
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

0 comments on commit 0a8d717

Please sign in to comment.