class
#include <muu/thread_pool.h>
thread_pool A thread pool.
Constructors, destructors, conversion operators
-
thread_pool(size_t worker_count = 0,
size_t task_queue_size = 0,
string_
param name = {}) explicit - Constructs a thread pool.
-
thread_pool(string_
param name) explicit - Constructs a thread pool.
-
thread_pool(thread_
pool&& other) noexcept - Move constructor.
- ~thread_pool() noexcept
- Destructor.
Public functions
- auto capacity() const -> size_t noexcept
- The maximum tasks that may be enqueued without blocking.
-
template <typename Task>auto enqueue(Task&& task) -> thread_
pool& noexcept - Enqueues a task.
-
template <typename T, typename Task>auto for_each(T start, T end, Task&& task) -> thread_
pool& noexcept - Enqueues a task to execute once for every value in a range.
-
template <typename T, typename Task>auto for_each(T end, Task&& task) -> thread_
pool& noexcept - Enqueues a task to execute once for every value in a range.
-
template <typename Iter, typename Task>auto for_each(Iter begin, Iter end, Task&& task) -> thread_
pool& noexcept - Enqueues a task to execute on every element in a collection.
-
template <typename T, typename Task>auto for_each(T&& collection, Task&& task) -> thread_
pool& noexcept - Enqueues a task to execute on every element in a collection.
-
auto operator=(thread_
pool&& rhs) -> thread_ pool& noexcept - Move-assignment operator.
- void wait() noexcept
- Waits for the thread pool to finish all of its current work.
- auto workers() const -> size_t noexcept
- The number of worker threads in the thread pool.
Function documentation
muu:: thread_pool:: thread_pool(size_t worker_count = 0,
size_t task_queue_size = 0,
string_ param name = {}) explicit
Constructs a thread pool.
Parameters | |
---|---|
worker_count | The number of worker threads in the pool. Leave as 0 for 'automatic'. |
task_queue_size | Max tasks that can be stored in the internal queue without blocking. Leave as 0 for 'automatic'. |
name | The name of your threadpool (for debugging purposes). |
muu:: thread_pool:: thread_pool(string_ param name) explicit
Constructs a thread pool.
Parameters | |
---|---|
name | The name of your thread pool (for debugging purposes). |
muu:: thread_pool:: ~thread_pool() noexcept
Destructor.
template <typename Task>
thread_ pool& muu:: thread_pool:: enqueue(Task&& task) noexcept
Enqueues a task.
Template parameters | |
---|---|
Task | The type of the task being enqueued. |
Parameters | |
task | The task to enqueue. |
Returns | A reference to the thread pool. |
Tasks must be callables with no parameters, or one parameter to recieve the index of the worker invoking the task:
pool.enqueue([](size_t worker_index) noexcept { // worker_index is in the range [0, pool.workers() - 1] }); pool.enqueue([]() noexcept { //... });
template <typename T, typename Task>
thread_ pool& muu:: thread_pool:: for_each(T start,
T end,
Task&& task) noexcept
Enqueues a task to execute once for every value in a range.
Template parameters | |
---|---|
T | An integer or enum type. |
Task | The type of the task being enqueued. |
Parameters | |
start | The start of the value range (inclusive). |
end | The end of the value range (exclusive). |
task | The task to enqueue. |
Returns | A reference to the threadpool. |
Tasks must be callables which accept at most two arguments:
Argument 0: The current value from the range
Argument 1: The task's batch (in the range [0, pool.workers() - 1]
)
pool.for_each(0, 10, [](int i, size_t batch_index) noexcept { // i is in the range [0, 9] // batch_index is in the range [0, pool.workers() - 1] }); pool.for_each(0, 10, [](int i) noexcept { // i is in the range [0, 9] }); pool.for_each(0, 10, []() noexcept { // no args is OK too });
template <typename T, typename Task>
thread_ pool& muu:: thread_pool:: for_each(T end,
Task&& task) noexcept
Enqueues a task to execute once for every value in a range.
Template parameters | |
---|---|
T | An integer or enum type. |
Task | The type of the task being enqueued. |
Parameters | |
end | The end of the value range (exclusive). |
task | The task to enqueue. |
Returns | A reference to the threadpool. |
Tasks must be callables which accept at most two arguments:
Argument 0: The current value from the range
Argument 1: The task's batch (in the range [0, pool.workers() - 1]
)
pool.for_each(10, [](int i, size_t batch_index) noexcept { // i is in the range [0, 9] // batch_index is in the range [0, pool.workers() - 1] }); pool.for_each(10, [](int i) noexcept { // i is in the range [0, 9] }); pool.for_each(10, []() noexcept { // no args is OK too });
template <typename Iter, typename Task>
thread_ pool& muu:: thread_pool:: for_each(Iter begin,
Iter end,
Task&& task) noexcept
Enqueues a task to execute on every element in a collection.
Template parameters | |
---|---|
Iter | Collection iterator type. |
Task | The type of the task being enqueued. |
Parameters | |
begin | Iterator to the beginning of the collection. |
end | Iterator to the end of the collection. |
task | The task to execute for each member of the collection. |
Returns | A reference to the thread pool. |
Tasks must be callables which accept at most two arguments:
Argument 0: The current element from the collection
Argument 1: The task's batch (in the range [0, pool.workers() - 1]
)
std::array<int, 10> vals; pool.for_each(vals.begin(), vals.end(), [](int& i, size_t batch_index) noexcept { // i is one of the elements of vals // batch_index is in the range [0, pool.workers() - 1] }); pool.for_each(vals.begin(), vals.end(), [](int& i) noexcept { // i is one of the elements of vals }); pool.for_each(vals.begin(), vals.end(), []() noexcept { // no args is OK too (though unusual) });
template <typename T, typename Task>
thread_ pool& muu:: thread_pool:: for_each(T&& collection,
Task&& task) noexcept
Enqueues a task to execute on every element in a collection.
Template parameters | |
---|---|
T | Type of the collection. |
Task | The type of the task being enqueued. |
Parameters | |
collection | The collection. |
task | The task to execute for each member of the collection. |
Returns | A reference to the thread pool. |
Tasks must be callables which accept at most two arguments:
Argument 0: The current element from the collection
Argument 1: The task's batch (in the range [0, pool.workers() - 1]
)
std::array<int, 10> vals; pool.for_each(vals, [](int& i, size_t batch_index) noexcept { // i is one of the elements of vals // batch_index is in the range [0, pool.workers() - 1] }); pool.for_each(vals, [](int& i) noexcept { // i is one of the elements of vals }); pool.for_each(vals, []() noexcept { // no args is OK too (though unusual) });
void muu:: thread_pool:: wait() noexcept
Waits for the thread pool to finish all of its current work.