1
0
mirror of https://github.com/bshoshany/thread-pool.git synced 2026-07-29 15:03:00 +04:00

2 Commits

Author SHA1 Message Date
Barak Shoshany 910f7cd95b Updated to v1.7 2021-06-02 10:38:24 -04:00
Barak Shoshany bc66b8a222 Updated to v1.6 2021-05-26 09:00:51 -04:00
3 changed files with 529 additions and 521 deletions
+5 -1
View File
@@ -75,7 +75,7 @@ As demonstrated in the performance tests [below](#performance-tests), using our
* **Fast:** * **Fast:**
* Built from scratch with maximum performance in mind. * Built from scratch with maximum performance in mind.
* Suitable for use in high-performance computing clusters with a very large number of CPU cores. * Suitable for use in high-performance computing nodes with a very large number of CPU cores.
* Compact code, to reduce both compilation time and binary size. * Compact code, to reduce both compilation time and binary size.
* Reusing threads avoids the overhead of creating and destroying them for individual tasks. * Reusing threads avoids the overhead of creating and destroying them for individual tasks.
* A task queue ensures that there are never more threads running in parallel than allowed by the hardware. * A task queue ensures that there are never more threads running in parallel than allowed by the hardware.
@@ -701,6 +701,10 @@ An interesting point to notice is that for **single-threaded** calculations (1 b
<a id="markdown-version-history" name="version-history"></a> <a id="markdown-version-history" name="version-history"></a>
## Version history ## Version history
* Version 1.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).
* Version 1.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).
* Version 1.5 (2021-05-07) * Version 1.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`. * 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`. * Added GitHub badges to `README.md`.
+22 -18
View File
@@ -3,8 +3,8 @@
/** /**
* @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.5 * @version 1.7
* @date 2021-05-07 * @date 2021-06-02
* @copyright Copyright (c) 2021 Barak Shoshany. Licensed under the MIT license. If you use this library in published research, please cite it as follows: * @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) * - Barak Shoshany, "A C++17 Thread Pool for High-Performance Scientific Computing", doi:10.5281/zenodo.4742687, arXiv:2105.00613 (May 2021)
* *
@@ -136,15 +136,16 @@ public:
T start = (T)(t * block_size + first_index); T start = (T)(t * block_size + first_index);
T end = (t == num_tasks - 1) ? last_index : (T)((t + 1) * block_size + first_index - 1); T end = (t == num_tasks - 1) ? last_index : (T)((t + 1) * block_size + first_index - 1);
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); for (T i = start; i <= end; i++)
blocks_running--; loop(i);
}); blocks_running--;
while (blocks_running != 0) });
{ }
sleep_or_yield(); while (blocks_running != 0)
} {
sleep_or_yield();
} }
} }
@@ -176,7 +177,8 @@ 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...); });
} }
/** /**
@@ -212,10 +214,11 @@ public:
{ {
std::shared_ptr<std::promise<bool>> promise(new std::promise<bool>); std::shared_ptr<std::promise<bool>> promise(new std::promise<bool>);
std::future<bool> future = promise->get_future(); std::future<bool> future = promise->get_future();
push_task([task, args..., promise] { push_task([task, args..., promise]
task(args...); {
promise->set_value(true); task(args...);
}); promise->set_value(true);
});
return future; return future;
} }
@@ -234,7 +237,8 @@ public:
{ {
std::shared_ptr<std::promise<R>> promise(new std::promise<R>); std::shared_ptr<std::promise<R>> promise(new std::promise<R>);
std::future<R> future = promise->get_future(); std::future<R> future = promise->get_future();
push_task([task, args..., promise] { promise->set_value(task(args...)); }); push_task([task, args..., promise]
{ promise->set_value(task(args...)); });
return future; return future;
} }
@@ -339,7 +343,7 @@ private:
while (running) while (running)
{ {
std::function<void()> task; std::function<void()> task;
if (!paused and pop_task(task)) if (!paused && pop_task(task))
{ {
task(); task();
tasks_total--; tasks_total--;