mirror of
https://github.com/bshoshany/thread-pool.git
synced 2026-07-29 23:12:59 +04:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 910f7cd95b | |||
| bc66b8a222 |
@@ -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`.
|
||||||
|
|||||||
+12
-8
@@ -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,17 +136,18 @@ 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++)
|
for (T i = start; i <= end; i++)
|
||||||
loop(i);
|
loop(i);
|
||||||
blocks_running--;
|
blocks_running--;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
while (blocks_running != 0)
|
while (blocks_running != 0)
|
||||||
{
|
{
|
||||||
sleep_or_yield();
|
sleep_or_yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Push a function with no arguments or return value into the task queue.
|
* @brief Push a function with no arguments or return value into the task queue.
|
||||||
@@ -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,7 +214,8 @@ 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...);
|
task(args...);
|
||||||
promise->set_value(true);
|
promise->set_value(true);
|
||||||
});
|
});
|
||||||
@@ -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--;
|
||||||
|
|||||||
Reference in New Issue
Block a user