Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
queue_factory.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
7
8#include <thread>
9
10namespace kcenon::thread {
11
13 -> std::unique_ptr<scheduler_interface>
14{
15 // If accuracy is required, use mutex-based queue
16 if (reqs.need_exact_size || reqs.need_atomic_empty ||
17 reqs.need_batch_operations || reqs.need_blocking_wait) {
18 return std::make_unique<job_queue>();
19 }
20
21 // If lock-free is preferred and no accuracy needs
22 // Use adaptive_job_queue with performance_first policy instead of direct lockfree_job_queue
23 // (lockfree_job_queue is now an internal implementation detail)
24 if (reqs.prefer_lock_free) {
25 return std::make_unique<adaptive_job_queue>(adaptive_job_queue::policy::performance_first);
26 }
27
28 // Default: adaptive queue for flexibility
29 return std::make_unique<adaptive_job_queue>();
30}
31
32auto queue_factory::create_optimal() -> std::unique_ptr<scheduler_interface>
33{
34 // Check architecture for memory model strength
35#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
36 constexpr bool strong_memory_model = true;
37#else
38 // ARM and other architectures have weaker memory models
39 constexpr bool strong_memory_model = false;
40#endif
41
42 // ARM and other weak memory model architectures: prefer mutex for safety
43 if (!strong_memory_model) {
44 return std::make_unique<job_queue>();
45 }
46
47 // Low core count: mutex is efficient enough
48 const auto core_count = std::thread::hardware_concurrency();
49 if (core_count <= 2) {
50 return std::make_unique<job_queue>();
51 }
52
53 // Default: adaptive for best of both worlds
54 return std::make_unique<adaptive_job_queue>();
55}
56
57auto queue_factory::create_policy_queue() -> std::unique_ptr<standard_queue>
58{
59 return std::make_unique<standard_queue>();
60}
61
62auto queue_factory::create_lockfree_policy_queue() -> std::unique_ptr<policy_lockfree_queue>
63{
64 return std::make_unique<policy_lockfree_queue>();
65}
66
78
79} // namespace kcenon::thread
@ performance_first
Always use lock-free mode.
Policy that limits queue size to a maximum.
Synchronization policy using mutex and condition variable.
Policy that rejects new items when queue is full.
Policy-based queue template.
static auto create_optimal() -> std::unique_ptr< scheduler_interface >
Create optimal queue for current environment.
static auto create_policy_queue() -> std::unique_ptr< standard_queue >
Create a standard policy_queue (mutex-based, unbounded)
static auto create_bounded_policy_queue(std::size_t max_size) -> std::unique_ptr< policy_queue< policies::mutex_sync_policy, policies::bounded_policy, policies::overflow_reject_policy > >
Create a bounded policy_queue with specified size.
static auto create_lockfree_policy_queue() -> std::unique_ptr< policy_lockfree_queue >
Create a lock-free policy_queue.
static auto create_for_requirements(const requirements &reqs) -> std::unique_ptr< scheduler_interface >
Create queue based on requirements.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
Policy-based job queue template with customizable sync, bound, and overflow.
Factory for creating queue instances based on configuration.