mirror of
https://github.com/bshoshany/thread-pool.git
synced 2026-07-29 15:03:00 +04:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 737c603610 | |||
| f7284c56db | |||
| 291ad92175 | |||
| 910f7cd95b | |||
| bc66b8a222 | |||
| 31efea058c | |||
| 41637a8d1e | |||
| b448742337 |
@@ -0,0 +1,56 @@
|
|||||||
|
# A C++17 Thread Pool for High-Performance Scientific Computing
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
* v2.0.0 (2021-08-14)
|
||||||
|
* From now on, version numbers will adhere to the [Semantic Versioning](https://semver.org/) specification in the format **major.minor.patch**.
|
||||||
|
* A file named `thread_pool_test.cpp` has been added to the package. It will perform automated tests of all aspects of the package, and benchmark some multithreaded matrix operations. Please run it on your system and [submit a bug report](https://github.com/bshoshany/thread-pool/issues) if any of the tests fail. In addition, the code is thoroughly documented, and is meant to serve as an extensive example of how to properly use the package.
|
||||||
|
* The package is now available through [vcpkg](https://github.com/microsoft/vcpkg). Instructions for how to install it have been added to `README.md`. See [this pull request](https://github.com/bshoshany/thread-pool/pull/18).
|
||||||
|
* The package now defines a macro `THREAD_POOL_VERSION`, which returns the version number and release date of the thread pool library as a string.
|
||||||
|
* `parallelize_loop()` has undergone some major changes (and is now incompatible with v1.x):
|
||||||
|
* The second argument is now the index **after** the last index, instead of the last index itself. This is more consistent with C++ conventions (e.g. standard library algorithms) where the range is always `[first, last)`. For example, for an array with `n` indices, instead of `parallelize_loop(0, n - 1, ...)` you should now write `parallelize_loop(0, n, ...)`.
|
||||||
|
* The `loop` function is now only called once per block, instead of once per index, as was the case before. This should provide a performance boost due to significantly reducing the number of function calls, and it also allows you to conserve resources by using them only once per block instead of once per index (an example can be found in the `random_matrix_generator` class in `thread_pool_test.cpp`). It also means that `loop` now takes two arguments: the first index in the block and the index after the last index in the block. Thus, `loop(start, end)` should typically involve a loop of the form `for (T i = start; i < end; i++)`.
|
||||||
|
* The first and last indices can now be of two different integer types. Previously, `parallelize_loop(0, i, ...)` did not work if `i` was not an `int`, because `0` was interpreted as `int`, and the two arguments had to be of the same type. Therefore, one had to use casting, e.g. `parallelize_loop((size_t)0, i)`, to make it work. Now this is no longer necessary; the common type is inferred automatically using `std::common_type_t`.
|
||||||
|
* v1.9 (2021-07-29)
|
||||||
|
* Fixed a bug in `reset()` which caused it to create the wrong number of threads.
|
||||||
|
* v1.8 (2021-07-28)
|
||||||
|
* The version history has become too long to be included in `README.md`, so I moved it to a separate file, `CHANGELOG.md`.
|
||||||
|
* A button to open this repository directly in Visual Studio Code has been added to the badges in `README.md`.
|
||||||
|
* An internal variable named `promise` has been renamed to `task_promise` to avoid any potential errors in case the user invokes `using namespace std`.
|
||||||
|
* `submit()` now catches exceptions thrown by the submitted task and forwards them to the future. See [this issue](https://github.com/bshoshany/thread-pool/issues/14).
|
||||||
|
* Eliminated compiler warnings that appeared when using the `-Weffc++` flag in GCC. See [this pull request](https://github.com/bshoshany/thread-pool/pull/17).
|
||||||
|
* v1.7 (2021-06-02)
|
||||||
|
* Fixed a bug in `parallelize_loop()` which prevented it from actually running loops in parallel, see [this issue](https://github.com/bshoshany/thread-pool/issues/11).
|
||||||
|
* v1.6 (2021-05-26)
|
||||||
|
* Since MSVC does not interpret `and` as `&&` by default, the previous release did not compile with MSVC unless the `/permissive-` or `/Za` compiler flags were used. This has been fixed in this version, and the code now successfully compiles with GCC, Clang, and MSVC. See [this pull request](https://github.com/bshoshany/thread-pool/pull/10).
|
||||||
|
* v1.5 (2021-05-07)
|
||||||
|
* This library now has a DOI for citation purposes. Information on how to cite it in publications has been added to the source code and to `README.md`.
|
||||||
|
* Added GitHub badges to `README.md`.
|
||||||
|
* v1.4 (2021-05-05)
|
||||||
|
* Added three new public member functions to monitor the tasks submitted to the pool:
|
||||||
|
* `get_tasks_queued()` gets the number of tasks currently waiting in the queue to be executed by the threads.
|
||||||
|
* `get_tasks_running()` gets the number of tasks currently being executed by the threads.
|
||||||
|
* `get_tasks_total()` gets the total number of unfinished tasks - either still in the queue, or running in a thread.
|
||||||
|
* Note that `get_tasks_running() == get_tasks_total() - get_tasks_queued()`.
|
||||||
|
* Renamed the private member variable `tasks_waiting` to `tasks_total` to make its purpose clearer.
|
||||||
|
* Added an option to temporarily pause the workers:
|
||||||
|
* When public member variable `paused` is set to `true`, the workers temporarily stop popping new tasks out of the queue, although any tasks already executed will keep running until they are done. Set to `false` again to resume popping tasks.
|
||||||
|
* While the workers are paused, `wait_for_tasks()` will wait for the running tasks instead of all tasks (otherwise it would wait forever).
|
||||||
|
* By utilizing the new pausing mechanism, `reset()` can now change the number of threads on-the-fly while there are still tasks waiting in the queue. The new thread pool will resume executing tasks from the queue once it is created.
|
||||||
|
* `parallelize_loop()` and `wait_for_tasks()` now have the same behavior as the worker function with regards to waiting for tasks to complete. If the relevant tasks are not yet complete, then before checking again, they will sleep for `sleep_duration` microseconds, unless that variable is set to zero, in which case they will call `std::this_thread::yield()`. This should improve performance and reduce CPU usage.
|
||||||
|
* Merged [this commit](https://github.com/bshoshany/thread-pool/pull/8): Fixed weird error when using MSVC and including `windows.h`.
|
||||||
|
* The `README.md` file has been reorganized and expanded.
|
||||||
|
* v1.3 (2021-05-03)
|
||||||
|
* Fixed [this issue](https://github.com/bshoshany/thread-pool/issues/3): Removed `std::move` from the `return` statement in `push_task()`. This previously generated a `-Wpessimizing-move` warning in Clang. The assembly code generated by the compiler seems to be the same before and after this change, presumably because the compiler eliminates the `std::move` automatically, but this change gets rid of the Clang warning.
|
||||||
|
* Fixed [this issue](https://github.com/bshoshany/thread-pool/issues/5): Removed a debugging message printed to `std::cout`, which was left in the code by mistake.
|
||||||
|
* Fixed [this issue](https://github.com/bshoshany/thread-pool/issues/6): `parallelize_loop()` no longer sends references for the variables `start` and `stop` when calling `push_task()`, which may lead to undefined behavior.
|
||||||
|
* A companion paper is now published at <a href="https://arxiv.org/abs/2105.00613">arXiv:2105.00613</a>, including additional information such as performance tests on systems with up to 80 hardware threads. The `README.md` has been updated, and it is now roughly identical in content to the paper.
|
||||||
|
* v1.2 (2021-04-29)
|
||||||
|
* The worker function, which controls the execution of tasks by each thread, now sleeps by default instead of yielding. Previously, when the worker could not find any tasks in the queue, it called `std::this_thread::yield()` and then tried again. However, this caused the workers to have high CPU usage when idle, [as reported by some users](https://github.com/bshoshany/thread-pool/issues/1). Now, when the worker function cannot find a task to run, it instead sleeps for a duration given by the public member variable `sleep_duration` (in microseconds) before checking the queue again. The default value is `1000` microseconds, which I found to be optimal in terms of both CPU usage and performance, but your own optimal value may be different.
|
||||||
|
* If the constructor is called with an argument of zero for the number of threads, then the default value, `std::thread::hardware_concurrency()`, is used instead.
|
||||||
|
* Added a simple helper class, `timer`, which can be used to measure execution time for benchmarking purposes.
|
||||||
|
* Improved and expanded the documentation.
|
||||||
|
* v1.1 (2021-04-24)
|
||||||
|
* Cosmetic changes only. Fixed a typo in the Doxygen comments and added a link to the GitHub repository.
|
||||||
|
* v1.0 (2021-01-15)
|
||||||
|
* Initial release.
|
||||||
+181
-75
@@ -3,15 +3,17 @@
|
|||||||
/**
|
/**
|
||||||
* @file thread_pool.hpp
|
* @file thread_pool.hpp
|
||||||
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||||
* @version 1.2
|
* @version 2.0.0
|
||||||
* @date 2021-04-29
|
* @date 2021-08-14
|
||||||
* @copyright Copyright (c) 2021 Barak Shoshany. Licensed under the MIT license. If you use this class in your code, please acknowledge the author and provide a link to the GitHub repository. Thank you!
|
* @copyright Copyright (c) 2021 Barak Shoshany. Licensed under the MIT license. If you use this library in published research, please cite it as follows:
|
||||||
|
* - Barak Shoshany, "A C++17 Thread Pool for High-Performance Scientific Computing", doi:10.5281/zenodo.4742687, arXiv:2105.00613 (May 2021)
|
||||||
*
|
*
|
||||||
* @brief A simple but powerful C++17 thread pool class.
|
* @brief A C++17 thread pool for high-performance scientific computing.
|
||||||
* @details This class was built from scratch with maximum performance in mind, and is suitable for use in high-performance computing clusters with a very large number of CPU cores. It is compact and self-contained, and includes features such as automatic generation of futures and easy parallelization of loops. Two helper classes enable synchronizing printing to an output stream by different threads and measuring execution time for benchmarking purposes. Please visit the GitHub repository at https://github.com/bshoshany/thread-pool for documentation and updates, or to submit feature requests and bug reports.
|
* @details A modern C++17-compatible thread pool implementation, built from scratch with high-performance scientific computing in mind. The thread pool is implemented as a single lightweight and self-contained class, and does not have any dependencies other than the C++17 standard library, thus allowing a great degree of portability. In particular, this implementation does not utilize OpenMP or any other high-level multithreading APIs, and thus gives the programmer precise low-level control over the details of the parallelization, which permits more robust optimizations. The thread pool was extensively tested on both AMD and Intel CPUs with up to 40 cores and 80 threads. Other features include automatic generation of futures and easy parallelization of loops. Two helper classes enable synchronizing printing to an output stream by different threads and measuring execution time for benchmarking purposes. Please visit the GitHub repository at https://github.com/bshoshany/thread-pool for documentation and updates, or to submit feature requests and bug reports.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <algorithm> // std::max
|
#define THREAD_POOL_VERSION "v2.0.0 (2021-08-14)"
|
||||||
|
|
||||||
#include <atomic> // std::atomic
|
#include <atomic> // std::atomic
|
||||||
#include <chrono> // std::chrono
|
#include <chrono> // std::chrono
|
||||||
#include <cstdint> // std::int_fast64_t, std::uint_fast32_t
|
#include <cstdint> // std::int_fast64_t, std::uint_fast32_t
|
||||||
@@ -22,17 +24,19 @@
|
|||||||
#include <mutex> // std::mutex, std::scoped_lock
|
#include <mutex> // std::mutex, std::scoped_lock
|
||||||
#include <queue> // std::queue
|
#include <queue> // std::queue
|
||||||
#include <thread> // std::this_thread, std::thread
|
#include <thread> // std::this_thread, std::thread
|
||||||
#include <type_traits> // std::decay_t, std::enable_if_t, std::is_void_v, std::invoke_result_t
|
#include <type_traits> // std::common_type_t, std::decay_t, std::enable_if_t, std::is_void_v, std::invoke_result_t
|
||||||
#include <utility> // std::move, std::swap
|
#include <utility> // std::move
|
||||||
|
|
||||||
// ================================== Begin class thread_pool ================================== //
|
// ============================================================================================= //
|
||||||
|
// Begin class thread_pool //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A simple but powerful thread pool class. The user submits tasks to be executed into a queue. Whenever a thread becomes available, it pops a task from the queue and executes it. Each task is automatically assigned a future, which can be used to wait for the task to finish executing and/or obtain its eventual return value.
|
* @brief A C++17 thread pool class. The user submits tasks to be executed into a queue. Whenever a thread becomes available, it pops a task from the queue and executes it. Each task is automatically assigned a future, which can be used to wait for the task to finish executing and/or obtain its eventual return value.
|
||||||
*/
|
*/
|
||||||
class thread_pool
|
class thread_pool
|
||||||
{
|
{
|
||||||
typedef std::uint_fast32_t ui32;
|
typedef std::uint_fast32_t ui32;
|
||||||
|
typedef std::uint_fast64_t ui64;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// ============================
|
// ============================
|
||||||
@@ -51,7 +55,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destruct the thread pool. Waits for all submitted tasks to be completed, then destroys all threads.
|
* @brief Destruct the thread pool. Waits for all tasks to complete, then destroys all threads. Note that if the variable paused is set to true, then any tasks still in the queue will never be executed.
|
||||||
*/
|
*/
|
||||||
~thread_pool()
|
~thread_pool()
|
||||||
{
|
{
|
||||||
@@ -64,6 +68,37 @@ public:
|
|||||||
// Public member functions
|
// Public member functions
|
||||||
// =======================
|
// =======================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the number of tasks currently waiting in the queue to be executed by the threads.
|
||||||
|
*
|
||||||
|
* @return The number of queued tasks.
|
||||||
|
*/
|
||||||
|
ui64 get_tasks_queued() const
|
||||||
|
{
|
||||||
|
const std::scoped_lock lock(queue_mutex);
|
||||||
|
return tasks.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the number of tasks currently being executed by the threads.
|
||||||
|
*
|
||||||
|
* @return The number of running tasks.
|
||||||
|
*/
|
||||||
|
ui32 get_tasks_running() const
|
||||||
|
{
|
||||||
|
return tasks_total - (ui32)get_tasks_queued();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the total number of unfinished tasks - either still in the queue, or running in a thread.
|
||||||
|
*
|
||||||
|
* @return The total number of tasks.
|
||||||
|
*/
|
||||||
|
ui32 get_tasks_total() const
|
||||||
|
{
|
||||||
|
return tasks_total;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the number of threads in the pool.
|
* @brief Get the number of threads in the pool.
|
||||||
*
|
*
|
||||||
@@ -75,45 +110,55 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Parallelize a loop by splitting it into blocks, submitting each block separately to the thread pool, and waiting for all blocks to finish executing. The loop will be equivalent to: for (T i = first_index; i <= last_index; i++) loop(i);
|
* @brief Parallelize a loop by splitting it into blocks, submitting each block separately to the thread pool, and waiting for all blocks to finish executing. The user supplies a loop function, which will be called once per block and should iterate over the block's range.
|
||||||
*
|
*
|
||||||
* @tparam T The type of the loop index. Should be a signed or unsigned integer.
|
* @tparam T1 The type of the first index in the loop. Should be a signed or unsigned integer.
|
||||||
|
* @tparam T2 The type of the index after the last index in the loop. Should be a signed or unsigned integer. If T1 is not the same as T2, a common type will be automatically inferred.
|
||||||
* @tparam F The type of the function to loop through.
|
* @tparam F The type of the function to loop through.
|
||||||
* @param first_index The first index in the loop (inclusive).
|
* @param first_index The first index in the loop.
|
||||||
* @param last_index The last index in the loop (inclusive).
|
* @param index_after_last The index after the last index in the loop. The loop will iterate from first_index to (index_after_last - 1) inclusive. In other words, it will be equivalent to "for (T i = first_index; i < index_after_last; i++)". Note that if first_index == index_after_last, the function will terminate without doing anything.
|
||||||
* @param loop The function to loop through. Should take exactly one argument, the loop index.
|
* @param loop The function to loop through. Will be called once per block. Should take exactly two arguments: the first index in the block and the index after the last index in the block. loop(start, end) should typically involve a loop of the form "for (T i = start; i < end; i++)".
|
||||||
* @param num_tasks The maximum number of tasks to split the loop into. The default is to use the number of threads in the pool.
|
* @param num_blocks The maximum number of blocks to split the loop into. The default is to use the number of threads in the pool.
|
||||||
*/
|
*/
|
||||||
template <typename T, typename F>
|
template <typename T1, typename T2, typename F>
|
||||||
void parallelize_loop(T first_index, T last_index, const F &loop, ui32 num_tasks = 0)
|
void parallelize_loop(const T1 &first_index, const T2 &index_after_last, const F &loop, ui32 num_blocks = 0)
|
||||||
{
|
{
|
||||||
if (num_tasks == 0)
|
typedef std::common_type_t<T1, T2> T;
|
||||||
num_tasks = thread_count;
|
T the_first_index = (T)first_index;
|
||||||
if (last_index < first_index)
|
T last_index = (T)index_after_last;
|
||||||
std::swap(last_index, first_index);
|
if (the_first_index == last_index)
|
||||||
size_t total_size = last_index - first_index + 1;
|
return;
|
||||||
size_t block_size = total_size / num_tasks;
|
if (last_index < the_first_index)
|
||||||
|
{
|
||||||
|
T temp = last_index;
|
||||||
|
last_index = the_first_index;
|
||||||
|
the_first_index = temp;
|
||||||
|
}
|
||||||
|
last_index--;
|
||||||
|
if (num_blocks == 0)
|
||||||
|
num_blocks = thread_count;
|
||||||
|
ui64 total_size = (ui64)(last_index - the_first_index + 1);
|
||||||
|
ui64 block_size = (ui64)(total_size / num_blocks);
|
||||||
if (block_size == 0)
|
if (block_size == 0)
|
||||||
{
|
{
|
||||||
block_size = 1;
|
block_size = 1;
|
||||||
num_tasks = std::max((ui32)1, (ui32)total_size);
|
num_blocks = (ui32)total_size > 1 ? (ui32)total_size : 1;
|
||||||
}
|
}
|
||||||
std::atomic<ui32> blocks_running = 0;
|
std::atomic<ui32> blocks_running = 0;
|
||||||
for (ui32 t = 0; t < num_tasks; t++)
|
for (ui32 t = 0; t < num_blocks; t++)
|
||||||
{
|
{
|
||||||
T start = (T)(t * block_size + first_index);
|
T start = ((T)(t * block_size) + the_first_index);
|
||||||
T end = (t == num_tasks - 1) ? last_index : (T)((t + 1) * block_size + first_index - 1);
|
T end = (t == num_blocks - 1) ? last_index + 1 : ((T)((t + 1) * block_size) + the_first_index);
|
||||||
std::cout << start << '-' << end << '\n';
|
|
||||||
blocks_running++;
|
blocks_running++;
|
||||||
push_task([&start, &end, &loop, &blocks_running] {
|
push_task([start, end, &loop, &blocks_running]
|
||||||
for (T i = start; i <= end; i++)
|
{
|
||||||
loop(i);
|
loop(start, end);
|
||||||
blocks_running--;
|
blocks_running--;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
while (blocks_running != 0)
|
while (blocks_running != 0)
|
||||||
{
|
{
|
||||||
std::this_thread::yield();
|
sleep_or_yield();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,10 +171,10 @@ public:
|
|||||||
template <typename F>
|
template <typename F>
|
||||||
void push_task(const F &task)
|
void push_task(const F &task)
|
||||||
{
|
{
|
||||||
tasks_waiting++;
|
tasks_total++;
|
||||||
{
|
{
|
||||||
const std::scoped_lock lock(queue_mutex);
|
const std::scoped_lock lock(queue_mutex);
|
||||||
tasks.push(std::move(std::function<void()>(task)));
|
tasks.push(std::function<void()>(task));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,21 +190,25 @@ public:
|
|||||||
template <typename F, typename... A>
|
template <typename F, typename... A>
|
||||||
void push_task(const F &task, const A &...args)
|
void push_task(const F &task, const A &...args)
|
||||||
{
|
{
|
||||||
push_task([task, args...] { task(args...); });
|
push_task([task, args...]
|
||||||
|
{ task(args...); });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reset the number of threads in the pool. Waits for all submitted tasks to be completed, then destroys all threads and creates a new thread pool with the new number of threads.
|
* @brief Reset the number of threads in the pool. Waits for all currently running tasks to be completed, then destroys all threads in the pool and creates a new thread pool with the new number of threads. Any tasks that were waiting in the queue before the pool was reset will then be executed by the new threads. If the pool was paused before resetting it, the new pool will be paused as well.
|
||||||
*
|
*
|
||||||
* @param _thread_count The number of threads to use. The default value is the total number of hardware threads available, as reported by the implementation. With a hyperthreaded CPU, this will be twice the number of CPU cores. If the argument is zero, the default value will be used instead.
|
* @param _thread_count The number of threads to use. The default value is the total number of hardware threads available, as reported by the implementation. With a hyperthreaded CPU, this will be twice the number of CPU cores. If the argument is zero, the default value will be used instead.
|
||||||
*/
|
*/
|
||||||
void reset(const ui32 &_thread_count = std::thread::hardware_concurrency())
|
void reset(const ui32 &_thread_count = std::thread::hardware_concurrency())
|
||||||
{
|
{
|
||||||
|
bool was_paused = paused;
|
||||||
|
paused = true;
|
||||||
wait_for_tasks();
|
wait_for_tasks();
|
||||||
running = false;
|
running = false;
|
||||||
destroy_threads();
|
destroy_threads();
|
||||||
thread_count = _thread_count ? _thread_count : std::thread::hardware_concurrency();
|
thread_count = _thread_count ? _thread_count : std::thread::hardware_concurrency();
|
||||||
threads.reset(new std::thread[_thread_count ? _thread_count : std::thread::hardware_concurrency()]);
|
threads.reset(new std::thread[thread_count]);
|
||||||
|
paused = was_paused;
|
||||||
running = true;
|
running = true;
|
||||||
create_threads();
|
create_threads();
|
||||||
}
|
}
|
||||||
@@ -176,11 +225,25 @@ public:
|
|||||||
template <typename F, typename... A, typename = std::enable_if_t<std::is_void_v<std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>>>
|
template <typename F, typename... A, typename = std::enable_if_t<std::is_void_v<std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>>>
|
||||||
std::future<bool> submit(const F &task, const A &...args)
|
std::future<bool> submit(const F &task, const A &...args)
|
||||||
{
|
{
|
||||||
std::shared_ptr<std::promise<bool>> promise(new std::promise<bool>);
|
std::shared_ptr<std::promise<bool>> task_promise(new std::promise<bool>);
|
||||||
std::future<bool> future = promise->get_future();
|
std::future<bool> future = task_promise->get_future();
|
||||||
push_task([task, args..., promise] {
|
push_task([task, args..., task_promise]
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
task(args...);
|
task(args...);
|
||||||
promise->set_value(true);
|
task_promise->set_value(true);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
task_promise->set_exception(std::current_exception());
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
@@ -198,22 +261,46 @@ public:
|
|||||||
template <typename F, typename... A, typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>, typename = std::enable_if_t<!std::is_void_v<R>>>
|
template <typename F, typename... A, typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>, typename = std::enable_if_t<!std::is_void_v<R>>>
|
||||||
std::future<R> submit(const F &task, const A &...args)
|
std::future<R> submit(const F &task, const A &...args)
|
||||||
{
|
{
|
||||||
std::shared_ptr<std::promise<R>> promise(new std::promise<R>);
|
std::shared_ptr<std::promise<R>> task_promise(new std::promise<R>);
|
||||||
std::future<R> future = promise->get_future();
|
std::future<R> future = task_promise->get_future();
|
||||||
push_task([task, args..., promise] {
|
push_task([task, args..., task_promise]
|
||||||
promise->set_value(task(args...));
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
task_promise->set_value(task(args...));
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
task_promise->set_exception(std::current_exception());
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Wait for all submitted tasks to be completed - both those that are currently being executed by threads, and those that are still waiting in the queue. To wait for a specific task, use submit() instead, and call the wait() member function of the generated future.
|
* @brief Wait for tasks to be completed. Normally, this function waits for all tasks, both those that are currently running in the threads and those that are still waiting in the queue. However, if the variable paused is set to true, this function only waits for the currently running tasks (otherwise it would wait forever). To wait for a specific task, use submit() instead, and call the wait() member function of the generated future.
|
||||||
*/
|
*/
|
||||||
void wait_for_tasks()
|
void wait_for_tasks()
|
||||||
{
|
{
|
||||||
while (tasks_waiting != 0)
|
while (true)
|
||||||
{
|
{
|
||||||
std::this_thread::yield();
|
if (!paused)
|
||||||
|
{
|
||||||
|
if (tasks_total == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (get_tasks_running() == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sleep_or_yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,6 +308,11 @@ public:
|
|||||||
// Public data
|
// Public data
|
||||||
// ===========
|
// ===========
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief An atomic variable indicating to the workers to pause. When set to true, the workers temporarily stop popping new tasks out of the queue, although any tasks already executed will keep running until they are done. Set to false again to resume popping tasks.
|
||||||
|
*/
|
||||||
|
std::atomic<bool> paused = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The duration, in microseconds, that the worker function should sleep for when it cannot find any tasks in the queue. If set to 0, then instead of sleeping, the worker function will execute std::this_thread::yield() if there are no tasks in the queue. The default value is 1000.
|
* @brief The duration, in microseconds, that the worker function should sleep for when it cannot find any tasks in the queue. If set to 0, then instead of sleeping, the worker function will execute std::this_thread::yield() if there are no tasks in the queue. The default value is 1000.
|
||||||
*/
|
*/
|
||||||
@@ -272,6 +364,18 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sleep for sleep_duration microseconds. If that variable is set to zero, yield instead.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void sleep_or_yield()
|
||||||
|
{
|
||||||
|
if (sleep_duration)
|
||||||
|
std::this_thread::sleep_for(std::chrono::microseconds(sleep_duration));
|
||||||
|
else
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A worker function to be assigned to each thread in the pool. Continuously pops tasks out of the queue and executes them, as long as the atomic variable running is set to true.
|
* @brief A worker function to be assigned to each thread in the pool. Continuously pops tasks out of the queue and executes them, as long as the atomic variable running is set to true.
|
||||||
*/
|
*/
|
||||||
@@ -280,17 +384,14 @@ private:
|
|||||||
while (running)
|
while (running)
|
||||||
{
|
{
|
||||||
std::function<void()> task;
|
std::function<void()> task;
|
||||||
if (pop_task(task))
|
if (!paused && pop_task(task))
|
||||||
{
|
{
|
||||||
task();
|
task();
|
||||||
tasks_waiting--;
|
tasks_total--;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (sleep_duration)
|
sleep_or_yield();
|
||||||
std::this_thread::sleep_for(std::chrono::microseconds(sleep_duration));
|
|
||||||
else
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -300,24 +401,19 @@ private:
|
|||||||
// ============
|
// ============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief An atomic variable indicating to the workers to keep running.
|
* @brief A mutex to synchronize access to the task queue by different threads.
|
||||||
|
*/
|
||||||
|
mutable std::mutex queue_mutex = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief An atomic variable indicating to the workers to keep running. When set to false, the workers permanently stop working.
|
||||||
*/
|
*/
|
||||||
std::atomic<bool> running = true;
|
std::atomic<bool> running = true;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief An atomic variable to keep track of how many tasks are currently waiting to finish - either still in the queue, or running in a thread.
|
|
||||||
*/
|
|
||||||
std::atomic<ui32> tasks_waiting = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief A mutex to synchronize access to the task queue by different threads.
|
|
||||||
*/
|
|
||||||
mutable std::mutex queue_mutex;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A queue of tasks to be executed by the threads.
|
* @brief A queue of tasks to be executed by the threads.
|
||||||
*/
|
*/
|
||||||
std::queue<std::function<void()>> tasks;
|
std::queue<std::function<void()>> tasks = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The number of threads in the pool.
|
* @brief The number of threads in the pool.
|
||||||
@@ -328,11 +424,18 @@ private:
|
|||||||
* @brief A smart pointer to manage the memory allocated for the threads.
|
* @brief A smart pointer to manage the memory allocated for the threads.
|
||||||
*/
|
*/
|
||||||
std::unique_ptr<std::thread[]> threads;
|
std::unique_ptr<std::thread[]> threads;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief An atomic variable to keep track of the total number of unfinished tasks - either still in the queue, or running in a thread.
|
||||||
|
*/
|
||||||
|
std::atomic<ui32> tasks_total = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// =================================== End class thread_pool =================================== //
|
// End class thread_pool //
|
||||||
|
// ============================================================================================= //
|
||||||
|
|
||||||
// ================================= Begin class synced_stream ================================= //
|
// ============================================================================================= //
|
||||||
|
// Begin class synced_stream //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A helper class to synchronize printing to an output stream by different threads.
|
* @brief A helper class to synchronize printing to an output stream by different threads.
|
||||||
@@ -377,7 +480,7 @@ private:
|
|||||||
/**
|
/**
|
||||||
* @brief A mutex to synchronize printing.
|
* @brief A mutex to synchronize printing.
|
||||||
*/
|
*/
|
||||||
mutable std::mutex stream_mutex;
|
mutable std::mutex stream_mutex = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The output stream to print to.
|
* @brief The output stream to print to.
|
||||||
@@ -385,9 +488,11 @@ private:
|
|||||||
std::ostream &out_stream;
|
std::ostream &out_stream;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ================================== End class synced_stream ================================== //
|
// End class synced_stream //
|
||||||
|
// ============================================================================================= //
|
||||||
|
|
||||||
// ===================================== Begin class timer ===================================== //
|
// ============================================================================================= //
|
||||||
|
// Begin class timer //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A helper class to measure execution time for benchmarking purposes.
|
* @brief A helper class to measure execution time for benchmarking purposes.
|
||||||
@@ -435,4 +540,5 @@ private:
|
|||||||
std::chrono::duration<double> elapsed_time = std::chrono::duration<double>::zero();
|
std::chrono::duration<double> elapsed_time = std::chrono::duration<double>::zero();
|
||||||
};
|
};
|
||||||
|
|
||||||
// ====================================== End class timer ====================================== //
|
// End class timer //
|
||||||
|
// ============================================================================================= //
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user