Factory for creating queue instances.
More...
#include <queue_factory.h>
Factory for creating queue instances.
Provides convenient methods for queue creation based on requirements. Following Kent Beck's Simple Design principle, we now offer only 2 public queue types:
Queue Selection Guide (Simplified)
| Queue Type | Use Case | Key Feature |
| adaptive_job_queue (Recommended) | Most use cases | Auto-optimizing, best of both worlds |
| job_queue | Blocking wait required | Mutex-based, exact size tracking |
Recommended Usage
auto bounded = std::make_shared<job_queue>(1000);
static auto create_optimal() -> std::unique_ptr< scheduler_interface >
Create optimal queue for current environment.
static auto create_standard_queue() -> std::shared_ptr< job_queue >
Create standard job_queue.
static auto create_adaptive_queue(adaptive_job_queue::policy policy=adaptive_job_queue::policy::balanced) -> std::unique_ptr< adaptive_job_queue >
Create adaptive queue (RECOMMENDED for most use cases)
static auto create_for_requirements(const requirements &reqs) -> std::unique_ptr< scheduler_interface >
Create queue based on requirements.
Queue selection requirements.
bool need_exact_size
Require exact size()
- Note
- Internal implementations (lockfree_job_queue, concurrent_queue) are now in the detail:: namespace and should not be used directly.
Definition at line 64 of file queue_factory.h.
◆ create_adaptive_queue()
Create adaptive queue (RECOMMENDED for most use cases)
- Parameters
-
| policy | Adaptation policy (default: balanced) |
- Returns
- Unique pointer to new adaptive_job_queue
This is the recommended default choice for most applications.
Use this when you:
- Want automatic optimization between mutex and lock-free modes
- Need high throughput with variable workloads
- Are unsure which implementation to choose
- Want the best of both worlds (safety + performance)
- Note
- Internally uses lock-free queue for high-contention scenarios and mutex-based queue for low-contention scenarios.
Definition at line 120 of file queue_factory.h.
123 {
124 return std::make_unique<adaptive_job_queue>(policy);
125 }
Referenced by practical_use_cases(), and simple_factory_usage().
◆ create_bounded_policy_queue()
Create a bounded policy_queue with specified size.
- Parameters
-
| max_size | Maximum number of jobs the queue can hold |
- Returns
- Unique pointer to bounded queue
This creates a policy_queue with:
- mutex_sync_policy: Thread-safe with blocking support
- bounded_policy: Limited to max_size items
- overflow_reject_policy: Returns error when full
auto result = queue->schedule(std::make_unique<my_job>());
}
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.
A template class representing either a value or an error.
Definition at line 67 of file queue_factory.cpp.
72{
73 return std::make_unique<policy_queue<
74 policies::mutex_sync_policy,
75 policies::bounded_policy,
76 policies::overflow_reject_policy>>(policies::bounded_policy(max_size));
77}
◆ create_custom_policy_queue()
template<typename SyncPolicy , typename BoundPolicy , typename OverflowPolicy >
| static auto kcenon::thread::queue_factory::create_custom_policy_queue |
( |
SyncPolicy | sync_policy, |
|
|
BoundPolicy | bound_policy, |
|
|
OverflowPolicy | overflow_policy ) -> std::unique_ptr<policy_queue<SyncPolicy, BoundPolicy, OverflowPolicy>>
|
|
inlinestaticnodiscard |
Create a policy_queue with custom policies.
- Template Parameters
-
| SyncPolicy | Synchronization policy type |
| BoundPolicy | Bounding policy type |
| OverflowPolicy | Overflow handling policy type |
- Parameters
-
| sync_policy | Sync policy instance |
| bound_policy | Bound policy instance |
| overflow_policy | Overflow policy instance |
- Returns
- Unique pointer to the configured policy_queue
This factory method allows full customization of queue behavior.
policies::overflow_drop_oldest_policy{}
);
Policy that limits queue size to a maximum.
Synchronization policy using mutex and condition variable.
static auto create_custom_policy_queue(SyncPolicy sync_policy, BoundPolicy bound_policy, OverflowPolicy overflow_policy) -> std::unique_ptr< policy_queue< SyncPolicy, BoundPolicy, OverflowPolicy > >
Create a policy_queue with custom policies.
Definition at line 249 of file queue_factory.h.
254 {
256 "SyncPolicy must be a valid sync policy type");
258 "BoundPolicy must be a valid bound policy type");
260 "OverflowPolicy must be a valid overflow policy type");
261
262 return std::make_unique<policy_queue<SyncPolicy, BoundPolicy, OverflowPolicy>>(
263 std::move(sync_policy),
264 std::move(bound_policy),
265 std::move(overflow_policy)
266 );
267 }
constexpr bool is_overflow_policy_v
Check if type is an overflow policy.
constexpr bool is_bound_policy_v
Check if type is a bound policy.
constexpr bool is_sync_policy_v
Check if type is a sync policy.
References kcenon::thread::is_bound_policy_v, kcenon::thread::is_overflow_policy_v, and kcenon::thread::is_sync_policy_v.
◆ create_for_requirements()
Create queue based on requirements.
- Parameters
-
| reqs | Requirements specification |
- Returns
- Scheduler interface pointer to appropriate queue type
Selection logic:
- If need_exact_size or need_atomic_empty or need_batch_operations or need_blocking_wait: returns job_queue
- If prefer_lock_free and no accuracy needs: returns lockfree_job_queue
- Otherwise: returns adaptive_job_queue
Definition at line 12 of file queue_factory.cpp.
14{
15
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
22
23
24 if (reqs.prefer_lock_free) {
26 }
27
28
29 return std::make_unique<adaptive_job_queue>();
30}
@ performance_first
Always use lock-free mode.
References kcenon::thread::adaptive_job_queue::performance_first.
Referenced by requirements_based_selection().
◆ create_lockfree_policy_queue()
| auto kcenon::thread::queue_factory::create_lockfree_policy_queue |
( |
| ) |
-> std::unique_ptr<policy_lockfree_queue> |
|
staticnodiscard |
Create a lock-free policy_queue.
- Returns
- Unique pointer to policy_lockfree_queue
This creates a policy_queue with:
- lockfree_sync_policy: High-throughput, non-blocking
- unbounded_policy: No size limits
- overflow_reject_policy: Rejects on overflow (not applicable for unbounded)
Use this for high-contention scenarios.
- Note
- size() returns approximate values, empty() is non-atomic
Definition at line 62 of file queue_factory.cpp.
63{
64 return std::make_unique<policy_lockfree_queue>();
65}
◆ create_optimal()
Create optimal queue for current environment.
- Returns
- Scheduler interface pointer to queue
Considers:
- CPU architecture (x86 vs ARM memory model)
- Number of CPU cores
- Default: adaptive_job_queue for flexibility
Selection:
Definition at line 32 of file queue_factory.cpp.
33{
34
35#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
36 constexpr bool strong_memory_model = true;
37#else
38
39 constexpr bool strong_memory_model = false;
40#endif
41
42
43 if (!strong_memory_model) {
44 return std::make_unique<job_queue>();
45 }
46
47
48 const auto core_count = std::thread::hardware_concurrency();
49 if (core_count <= 2) {
50 return std::make_unique<job_queue>();
51 }
52
53
54 return std::make_unique<adaptive_job_queue>();
55}
Referenced by optimal_selection().
◆ create_policy_queue()
| auto kcenon::thread::queue_factory::create_policy_queue |
( |
| ) |
-> std::unique_ptr<standard_queue> |
|
staticnodiscard |
Create a standard policy_queue (mutex-based, unbounded)
- Returns
- Unique pointer to standard_queue
This creates a policy_queue with:
- mutex_sync_policy: Thread-safe with blocking support
- unbounded_policy: No size limits
- overflow_reject_policy: Rejects on overflow (not applicable for unbounded)
Use this as the modern replacement for legacy job_queue.
queue->schedule(std::make_unique<my_job>());
static auto create_policy_queue() -> std::unique_ptr< standard_queue >
Create a standard policy_queue (mutex-based, unbounded)
Definition at line 57 of file queue_factory.cpp.
58{
59 return std::make_unique<standard_queue>();
60}
◆ create_standard_queue()
| static auto kcenon::thread::queue_factory::create_standard_queue |
( |
| ) |
-> std::shared_ptr<job_queue> |
|
inlinestaticnodiscard |
Create standard job_queue.
- Returns
- Shared pointer to new job_queue (same as std::make_shared<job_queue>())
Use this when you need:
- Exact size() and empty() checks
- Batch operations
- Blocking dequeue with condition variable wait
- Note
- For bounded queue functionality, use:
auto bounded = std::make_shared<job_queue>(1000);
- See also
- create_adaptive_queue() for the recommended default choice
Definition at line 100 of file queue_factory.h.
100 {
101 return std::make_shared<job_queue>();
102 }
Referenced by practical_use_cases(), and simple_factory_usage().
The documentation for this class was generated from the following files: