Skip to content

Commit 59d9597

Browse files
authored
Fixing windows singleton executors (#580)
The static singleton executors were being created in a member function templated on the task type. This was causing unnecessary thread pools to get constructed. Moved instances to the .cpp file to make it simpler to share across dlls (open issue is to normalize the various implementations).
1 parent 85a52e7 commit 59d9597

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

include/stlab/concurrency/default_executor.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,17 +451,16 @@ priority_task_system& pts();
451451

452452
#if STLAB_TASK_SYSTEM(WINDOWS)
453453

454+
template <executor_priority P>
455+
extern task_system<P>& single_task_system();
456+
454457
template <executor_priority P = executor_priority::medium>
455458
struct executor_type {
456459
using result_type = void;
457460

458461
template <class F>
459462
auto operator()(F&& f) const -> std::enable_if_t<std::is_nothrow_invocable_v<std::decay_t<F>>> {
460-
static task_system<P> only_task_system{[] {
461-
at_pre_exit([]() noexcept { only_task_system.join(); });
462-
return task_system<P>{};
463-
}()};
464-
only_task_system(std::forward<F>(f));
463+
single_task_system<P>()(std::forward<F>(f));
465464
}
466465
};
467466

src/concurrency/default_executor.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ priority_task_system& pts() {
5151
}()};
5252
return only_task_system;
5353
}
54+
55+
#elif STLAB_TASK_SYSTEM(WINDOWS)
56+
57+
/// Returns the singleton instance of the task system for the given priority.
58+
template <executor_priority P>
59+
task_system<P>& single_task_system() {
60+
static task_system<P> _task_system{[] {
61+
at_pre_exit([]() noexcept { single_task_system<P>().join(); });
62+
return task_system<P>{};
63+
}()};
64+
return _task_system;
65+
}
66+
67+
/// Instantiations of single_task_system for each priority level.
68+
template task_system<executor_priority::high>& single_task_system<executor_priority::high>();
69+
template task_system<executor_priority::medium>& single_task_system<executor_priority::medium>();
70+
template task_system<executor_priority::low>& single_task_system<executor_priority::low>();
71+
5472
#endif
5573

5674
} // namespace detail

0 commit comments

Comments
 (0)