1
0
mirror of https://github.com/bshoshany/thread-pool.git synced 2026-07-21 19:13:00 +04:00

Updated to v4.0.1

This commit is contained in:
Barak Shoshany
2023-12-28 12:57:15 -05:00
parent ff95929999
commit 6790920f61
6 changed files with 77 additions and 40 deletions
+7
View File
@@ -6,6 +6,7 @@ Website: <https://baraksh.com/>\
GitHub: <https://github.com/bshoshany> GitHub: <https://github.com/bshoshany>
* [Version history](#version-history) * [Version history](#version-history)
* [v4.0.1 (2023-12-28)](#v401-2023-12-28)
* [v4.0.0 (2023-12-27)](#v400-2023-12-27) * [v4.0.0 (2023-12-27)](#v400-2023-12-27)
* [v3.5.0 (2023-05-25)](#v350-2023-05-25) * [v3.5.0 (2023-05-25)](#v350-2023-05-25)
* [v3.4.0 (2023-05-12)](#v340-2023-05-12) * [v3.4.0 (2023-05-12)](#v340-2023-05-12)
@@ -27,6 +28,12 @@ GitHub: <https://github.com/bshoshany>
## Version history ## Version history
### v4.0.1 (2023-12-28)
* Fixed linkage issue caused by the global variables `BS::this_thread::get_index` and `BS::this_thread::get_pool` not being defined as `inline`. See [134](https://github.com/bshoshany/thread-pool/issues/134) and [137](https://github.com/bshoshany/thread-pool/issues/137).
* Fixed redundant cast in the `BS::thread_pool::blocks` class, and added `-Wuseless-cast` to the GCC warning flags in `BS_thread_pool_test.ps1` to catch similar issues in the future. See [133](https://github.com/bshoshany/thread-pool/pull/133).
* Each of the three files `BS_thread_pool_test.cpp`, `BS_thread_pool.hpp`, and `BS_thread_pool_utils.hpp` now contains three macros indicating the major, minor, and patch version of the file. In addition, `BS_thread_pool_test.cpp` now checks whether the versions of all three files match, and aborts compilation if they do not.
### v4.0.0 (2023-12-27) ### v4.0.0 (2023-12-27)
* A major new release with numerous changes, additions, fixes, and improvements. Many frequently requested features have been added, and performance has been optimized. Please note that code written using previous releases will need to be modified to work with the new release. The changes needed to migrate to the new API are explicitly indicated below for your convenience. * A major new release with numerous changes, additions, fixes, and improvements. Many frequently requested features have been added, and performance has been optimized. Please note that code written using previous releases will need to be modified to work with the new release. The changes needed to migrate to the new API are explicitly indicated below for your convenience.
+27 -15
View File
@@ -19,7 +19,7 @@ Email: <baraksh@gmail.com>\
Website: <https://baraksh.com/>\ Website: <https://baraksh.com/>\
GitHub: <https://github.com/bshoshany> GitHub: <https://github.com/bshoshany>
This is the complete documentation for v4.0.0 of the library, released on 2023-12-27. This is the complete documentation for v4.0.1 of the library, released on 2023-12-28.
* [Introduction](#introduction) * [Introduction](#introduction)
* [Motivation](#motivation) * [Motivation](#motivation)
@@ -220,19 +220,25 @@ It is generally unnecessary to change the number of threads in the pool after it
### Finding the version of the library ### Finding the version of the library
If desired, the version of this library may be read during compilation time from the macro `BS_THREAD_POOL_VERSION`. The value will be a string containing the version number and release date. For example: If desired, the version of this library may be read during compilation time from the following three macros:
```cpp * `BS_THREAD_POOL_VERSION_MAJOR` - indicates the major version.
std::cout << "Thread pool library version is " << BS_THREAD_POOL_VERSION << ".\n"; * `BS_THREAD_POOL_VERSION_MINOR` - indicates the minor version.
* `BS_THREAD_POOL_VERSION_PATCH` - indicates the patch version.
```cpp
std::cout << "Thread pool library version is " << BS_THREAD_POOL_VERSION_MAJOR << '.' << BS_THREAD_POOL_VERSION_MINOR << '.' << BS_THREAD_POOL_VERSION_PATCH << ".\n";
``` ```
Sample output: Sample output:
```none ```none
Thread pool library version is v4.0.0 (2023-12-27). Thread pool library version is 4.0.1.
``` ```
This can be used, for example, to allow the same code to work with several incompatible versions of the library. This can be used, for example, to allow the same code base to work with several incompatible versions of the library using `#if` directives.
**Note:** This feature is only available starting with v4.0.1. Earlier releases of this library do not define these macros.
## Submitting tasks to the queue ## Submitting tasks to the queue
@@ -948,7 +954,13 @@ Aside from using `BS::multi_future<T>` to track the execution of parallelized lo
## Utility classes ## Utility classes
The optional header file `BS_thread_pool_utils.hpp` contains several useful utility classes. These are not necessary for using the thread pool itself; `BS_thread_pool.hpp` is the only header file required. However, the utility classes can make writing multithreading code more convenient. The version of the utilities header file can be found by checking the macro `BS_THREAD_POOL_UTILS_VERSION`. The optional header file `BS_thread_pool_utils.hpp` contains several useful utility classes. These are not necessary for using the thread pool itself; `BS_thread_pool.hpp` is the only header file required. However, the utility classes can make writing multithreading code more convenient.
As with [the main header file](#finding-the-version-of-the-library), the version of the utilities header file can be found by checking three macros:
* `BS_THREAD_POOL_UTILS_VERSION_MAJOR` - indicates the major version.
* `BS_THREAD_POOL_UTILS_VERSION_MINOR` - indicates the minor version.
* `BS_THREAD_POOL_UTILS_VERSION_PATCH` - indicates the patch version.
### Synchronizing printing to a stream with `BS::synced_stream` ### Synchronizing printing to a stream with `BS::synced_stream`
@@ -1640,11 +1652,11 @@ BS::thread_pool pool(1);
int main() int main()
{ {
pool.detach_task( [] { sync_out.println("This task will execute third."); }, BS::pr::normal); pool.detach_task([] { sync_out.println("This task will execute third."); }, BS::pr::normal);
pool.detach_task( [] { sync_out.println("This task will execute fifth."); }, BS::pr::lowest); pool.detach_task([] { sync_out.println("This task will execute fifth."); }, BS::pr::lowest);
pool.detach_task( [] { sync_out.println("This task will execute second."); }, BS::pr::high); pool.detach_task([] { sync_out.println("This task will execute second."); }, BS::pr::high);
pool.detach_task( [] { sync_out.println("This task will execute first."); }, BS::pr::highest); pool.detach_task([] { sync_out.println("This task will execute first."); }, BS::pr::highest);
pool.detach_task( [] { sync_out.println("This task will execute fourth."); }, BS::pr::low); pool.detach_task([] { sync_out.println("This task will execute fourth."); }, BS::pr::low);
} }
``` ```
@@ -1771,7 +1783,7 @@ If you are using the [Conan](https://conan.io/) C/C++ package manager, you can e
```ini ```ini
[requires] [requires]
bshoshany-thread-pool/4.0.0 bshoshany-thread-pool/4.0.1
``` ```
To update the package to the latest version, simply change the version number. Please refer to [this package's page on ConanCenter](https://conan.io/center/recipes/bshoshany-thread-pool) for more information. To update the package to the latest version, simply change the version number. Please refer to [this package's page on ConanCenter](https://conan.io/center/recipes/bshoshany-thread-pool) for more information.
@@ -1798,7 +1810,7 @@ If you are using [CMake](https://cmake.org/), you can install `BS::thread_pool`
CPMAddPackage( CPMAddPackage(
NAME BS_thread_pool NAME BS_thread_pool
GITHUB_REPOSITORY bshoshany/thread-pool GITHUB_REPOSITORY bshoshany/thread-pool
VERSION 4.0.0) VERSION 4.0.1)
add_library(BS_thread_pool INTERFACE) add_library(BS_thread_pool INTERFACE)
target_include_directories(BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR}/include) target_include_directories(BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR}/include)
``` ```
@@ -1833,7 +1845,7 @@ include(${CPM_DOWNLOAD_LOCATION})
CPMAddPackage( CPMAddPackage(
NAME BS_thread_pool NAME BS_thread_pool
GITHUB_REPOSITORY bshoshany/thread-pool GITHUB_REPOSITORY bshoshany/thread-pool
VERSION 4.0.0) VERSION 4.0.1)
add_library(BS_thread_pool INTERFACE) add_library(BS_thread_pool INTERFACE)
target_include_directories(BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR}/include) target_include_directories(BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR}/include)
add_executable(my_project main.cpp) add_executable(my_project main.cpp)
+10 -8
View File
@@ -3,8 +3,8 @@
/** /**
* @file BS_thread_pool.hpp * @file BS_thread_pool.hpp
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com) * @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
* @version 4.0.0 * @version 4.0.1
* @date 2023-12-27 * @date 2023-12-28
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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) * @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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 BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This header file contains the main thread pool class and some additional classes and definitions. No other files are needed in order to use the thread pool itself. * @brief BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This header file contains the main thread pool class and some additional classes and definitions. No other files are needed in order to use the thread pool itself.
@@ -31,8 +31,10 @@
* @brief A namespace used by Barak Shoshany's projects. * @brief A namespace used by Barak Shoshany's projects.
*/ */
namespace BS { namespace BS {
// A macro containing the version of the thread pool library. // Macros indicating the version of the thread pool library.
#define BS_THREAD_POOL_VERSION "v4.0.0 (2023-12-27)" #define BS_THREAD_POOL_VERSION_MAJOR 4
#define BS_THREAD_POOL_VERSION_MINOR 0
#define BS_THREAD_POOL_VERSION_PATCH 1
class thread_pool; class thread_pool;
@@ -138,12 +140,12 @@ namespace this_thread {
/** /**
* @brief A `thread_local` object used to obtain information about the index of the current thread. * @brief A `thread_local` object used to obtain information about the index of the current thread.
*/ */
thread_local thread_info_index get_index; inline thread_local thread_info_index get_index;
/** /**
* @brief A `thread_local` object used to obtain information about the thread pool that owns the current thread. * @brief A `thread_local` object used to obtain information about the thread pool that owns the current thread.
*/ */
thread_local thread_info_pool get_pool; inline thread_local thread_info_pool get_pool;
} // namespace this_thread } // namespace this_thread
/** /**
@@ -952,8 +954,8 @@ private:
const size_t total_size = static_cast<size_t>(index_after_last - first_index); const size_t total_size = static_cast<size_t>(index_after_last - first_index);
if (num_blocks > total_size) if (num_blocks > total_size)
num_blocks = total_size; num_blocks = total_size;
block_size = static_cast<size_t>(total_size / num_blocks); block_size = total_size / num_blocks;
remainder = static_cast<size_t>(total_size % num_blocks); remainder = total_size % num_blocks;
if (block_size == 0) if (block_size == 0)
{ {
block_size = 1; block_size = 1;
+6 -4
View File
@@ -3,8 +3,8 @@
/** /**
* @file BS_thread_pool_utils.hpp * @file BS_thread_pool_utils.hpp
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com) * @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
* @version 4.0.0 * @version 4.0.1
* @date 2023-12-27 * @date 2023-12-28
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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) * @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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 BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This header file contains independent utility classes that are part of the library, but are not needed to use the thread pool itself. * @brief BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This header file contains independent utility classes that are part of the library, but are not needed to use the thread pool itself.
@@ -24,8 +24,10 @@
* @brief A namespace used by Barak Shoshany's projects. * @brief A namespace used by Barak Shoshany's projects.
*/ */
namespace BS { namespace BS {
// A macro containing the version of the thread pool utilities library. // Macros indicating the version of the thread pool utilities library.
#define BS_THREAD_POOL_UTILS_VERSION "v4.0.0 (2023-12-27)" #define BS_THREAD_POOL_UTILS_VERSION_MAJOR 4
#define BS_THREAD_POOL_UTILS_VERSION_MINOR 0
#define BS_THREAD_POOL_UTILS_VERSION_PATCH 1
/** /**
* @brief A utility class to allow simple signalling between threads. * @brief A utility class to allow simple signalling between threads.
+20 -6
View File
@@ -1,8 +1,8 @@
/** /**
* @file BS_thread_pool_test.cpp * @file BS_thread_pool_test.cpp
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com) * @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
* @version 4.0.0 * @version 4.0.1
* @date 2023-12-27 * @date 2023-12-28
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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) * @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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 BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This program tests all aspects of the library, but is not needed in order to use the library. * @brief BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This program tests all aspects of the library, but is not needed in order to use the library.
@@ -49,6 +49,19 @@
#include "BS_thread_pool.hpp" #include "BS_thread_pool.hpp"
#include "BS_thread_pool_utils.hpp" #include "BS_thread_pool_utils.hpp"
// Macros indicating the version of the thread pool test program.
#define BS_THREAD_POOL_TEST_VERSION_MAJOR 4
#define BS_THREAD_POOL_TEST_VERSION_MINOR 0
#define BS_THREAD_POOL_TEST_VERSION_PATCH 1
#if (BS_THREAD_POOL_TEST_VERSION_MAJOR != BS_THREAD_POOL_VERSION_MAJOR || BS_THREAD_POOL_TEST_VERSION_MINOR != BS_THREAD_POOL_VERSION_MINOR || BS_THREAD_POOL_TEST_VERSION_PATCH != BS_THREAD_POOL_VERSION_PATCH)
#error The versions of BS_thread_pool_test.cpp and BS_thread_pool.hpp do not match. Aborting compilation.
#endif
#if (BS_THREAD_POOL_TEST_VERSION_MAJOR != BS_THREAD_POOL_UTILS_VERSION_MAJOR || BS_THREAD_POOL_TEST_VERSION_MINOR != BS_THREAD_POOL_UTILS_VERSION_MINOR || BS_THREAD_POOL_TEST_VERSION_PATCH != BS_THREAD_POOL_UTILS_VERSION_PATCH)
#error The versions of BS_thread_pool_test.cpp and BS_thread_pool_utils.hpp do not match. Aborting compilation.
#endif
using int64 = std::int_fast64_t; using int64 = std::int_fast64_t;
using std::size_t; using std::size_t;
@@ -2248,8 +2261,9 @@ void show_intro()
dual_println("GitHub: https://github.com/bshoshany/thread-pool"); dual_println("GitHub: https://github.com/bshoshany/thread-pool");
dual_println(); dual_println();
dual_println("Thread pool library version is ", BS_THREAD_POOL_VERSION, "."); dual_println("Thread pool library version is ", BS_THREAD_POOL_VERSION_MAJOR, '.', BS_THREAD_POOL_VERSION_MINOR, '.', BS_THREAD_POOL_VERSION_PATCH, '.');
dual_println("Hardware concurrency is ", std::thread::hardware_concurrency(), "."); dual_println("Thread pool utilities library version is ", BS_THREAD_POOL_UTILS_VERSION_MAJOR, '.', BS_THREAD_POOL_UTILS_VERSION_MINOR, '.', BS_THREAD_POOL_UTILS_VERSION_PATCH, '.');
dual_println("Hardware concurrency is ", std::thread::hardware_concurrency(), '.');
dual_println(); dual_println();
dual_print("Native handles are "); dual_print("Native handles are ");
@@ -2279,8 +2293,8 @@ void show_intro()
dual_println(); dual_println();
dual_println("Detected OS: ", detect_os(), "."); dual_println("Detected OS: ", detect_os(), '.');
dual_println("Detected compiler: ", detect_compiler(), "."); dual_println("Detected compiler: ", detect_compiler(), '.');
dual_println(); dual_println();
dual_println("Important: Please do not run any other applications, especially multithreaded applications, in parallel with this test!"); dual_println("Important: Please do not run any other applications, especially multithreaded applications, in parallel with this test!");
+7 -7
View File
@@ -2,7 +2,7 @@
# BS_thread_pool_test.ps1 # BS_thread_pool_test.ps1
# By Barak Shoshany (baraksh@gmail.com) (http://baraksh.com) # By Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
# v4.0.0, 2023-12-27 # v4.0.1, 2023-12-28
# Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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) # Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. 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)
# #
# BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This script compiles and runs the bundled test program with different compilers. # BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This script compiles and runs the bundled test program with different compilers.
@@ -119,10 +119,10 @@ $ExePrefix = 'BS_thread_pool_test_'
$Executables = @() $Executables = @()
Function Build-ClangGCC([String] $Compiler, [String] $ExeSuffix, [String] $Macros = '') Function Build-ClangGCC([String] $Compiler, [String] $ExeSuffix, [String] $ExtraFlags = '')
{ {
$FullExe = Join-Path $BuildFolder "$ExePrefix$ExeSuffix$Extension" $FullExe = Join-Path $BuildFolder "$ExePrefix$ExeSuffix$Extension"
$Command = "$Compiler $SourceFile$PThread -I../include -std=c++17 -O3 -march=native -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow -o $FullExe $Macros" $Command = "$Compiler $SourceFile$PThread -I../include -std=c++17 -O3 -march=native -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow -o $FullExe $ExtraFlags"
Write-Command $Command Write-Command $Command
Invoke-Expression $Command Invoke-Expression $Command
If ($LASTEXITCODE) If ($LASTEXITCODE)
@@ -153,8 +153,8 @@ Write-Host
If (Get-Command 'g++' -ErrorAction SilentlyContinue) If (Get-Command 'g++' -ErrorAction SilentlyContinue)
{ {
Write-Text 'Compiling with GCC...' Write-Text 'Compiling with GCC...'
Build-ClangGCC 'g++' 'gcc' Build-ClangGCC 'g++' 'gcc' '-Wuseless-cast'
Build-ClangGCC 'g++' 'gcc_light' '-DBS_THREAD_POOL_LIGHT_TEST' Build-ClangGCC 'g++' 'gcc_light' '-Wuseless-cast -DBS_THREAD_POOL_LIGHT_TEST'
} }
Else Else
{ {
@@ -163,12 +163,12 @@ Else
Write-Host Write-Host
Function Build-MSVC([String] $ExeSuffix, [String] $Macros = '') Function Build-MSVC([String] $ExeSuffix, [String] $ExtraFlags = '')
{ {
$MSVCName = "$ExePrefix$ExeSuffix" $MSVCName = "$ExePrefix$ExeSuffix"
$FullExe = Join-Path $BuildFolder "$MSVCName$Extension" $FullExe = Join-Path $BuildFolder "$MSVCName$Extension"
$FullObj = Join-Path $BuildFolder "$MSVCName.obj" $FullObj = Join-Path $BuildFolder "$MSVCName.obj"
$Env:BS_THREAD_POOL_MSVC_COMMAND = "cl $SourceFile /I../include /std:c++17 /permissive- /O2 /W4 /EHsc /Fe:$FullExe /Fo:$FullObj $Macros" $Env:BS_THREAD_POOL_MSVC_COMMAND = "cl $SourceFile /I../include /std:c++17 /permissive- /O2 /W4 /EHsc /Fe:$FullExe /Fo:$FullObj $ExtraFlags"
Write-Command $Env:BS_THREAD_POOL_MSVC_COMMAND Write-Command $Env:BS_THREAD_POOL_MSVC_COMMAND
pwsh -Command { pwsh -Command {
$CurrentDir = Get-Location $CurrentDir = Get-Location