From aa3fbfbe80762fe3ac90e2bf05e153b92536277a Mon Sep 17 00:00:00 2001 From: Barak Shoshany Date: Thu, 19 Dec 2024 21:02:25 -0500 Subject: [PATCH] Updated to v5.0.0 --- .clang-format | 5 +- .clang-tidy | 61 +- .github/ISSUE_TEMPLATE/bug_report.md | 2 - .github/ISSUE_TEMPLATE/failed-tests.md | 2 - .github/ISSUE_TEMPLATE/feature_request.md | 2 - .github/pull_request_template.md | 4 +- .vscode-linux/c_cpp_properties.json | 110 + .vscode-linux/launch.json | 59 + .vscode-linux/tasks.json | 532 ++++ .vscode-macos/c_cpp_properties.json | 44 + .vscode-macos/launch.json | 32 + .vscode-macos/tasks.json | 298 ++ .vscode-windows/c_cpp_properties.json | 158 ++ .vscode-windows/launch.json | 89 + .vscode-windows/tasks.json | 766 ++++++ CHANGELOG.md | 151 +- CITATION.cff | 29 +- README.md | 2545 ++++++++++++----- compile_cpp.yaml | 25 + include/BS_thread_pool.hpp | 2503 ++++++++++++----- include/BS_thread_pool_utils.hpp | 203 -- modules/BS.thread_pool.cppm | 56 + pyproject.toml | 382 +++ scripts/clear_folder.py | 63 + scripts/compile_cpp.py | 409 +++ scripts/test_all.py | 87 + tests/BS_thread_pool_test.cpp | 3037 ++++++++++++++------- tests/BS_thread_pool_test.ps1 | 232 -- 28 files changed, 9217 insertions(+), 2669 deletions(-) create mode 100644 .vscode-linux/c_cpp_properties.json create mode 100644 .vscode-linux/launch.json create mode 100644 .vscode-linux/tasks.json create mode 100644 .vscode-macos/c_cpp_properties.json create mode 100644 .vscode-macos/launch.json create mode 100644 .vscode-macos/tasks.json create mode 100644 .vscode-windows/c_cpp_properties.json create mode 100644 .vscode-windows/launch.json create mode 100644 .vscode-windows/tasks.json create mode 100644 compile_cpp.yaml delete mode 100644 include/BS_thread_pool_utils.hpp create mode 100644 modules/BS.thread_pool.cppm create mode 100644 pyproject.toml create mode 100644 scripts/clear_folder.py create mode 100644 scripts/compile_cpp.py create mode 100644 scripts/test_all.py delete mode 100644 tests/BS_thread_pool_test.ps1 diff --git a/.clang-format b/.clang-format index 1ac2b7b..53dfbad 100644 --- a/.clang-format +++ b/.clang-format @@ -11,7 +11,7 @@ AlignEscapedNewlines: Left AlignOperands: DontAlign AlignTrailingComments: Kind: Always - OverEmptyLines: 2 + OverEmptyLines: 0 AllowAllArgumentsOnNextLine: false AllowAllParametersOfDeclarationOnNextLine: false AllowBreakBeforeNoexceptSpecifier: Never @@ -117,6 +117,7 @@ ReflowComments: true RemoveBracesLLVM: false SeparateDefinitionBlocks: Always ShortNamespaceLines: 1 +SkipMacroDefinitionBody: true SortIncludes: CaseSensitive SortJavaStaticImport: Before SortUsingDeclarations: true @@ -152,7 +153,7 @@ SpacesInLineCommentPrefix: Minimum: 1 SpacesInParentheses: false SpacesInSquareBrackets: false -Standard: c++17 +Standard: Latest TabWidth: 4 UseCRLF: false UseTab: Never diff --git a/.clang-tidy b/.clang-tidy index 74d9700..ea4b7ac 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,45 +1,56 @@ ---- CheckOptions: - misc-const-correctness.WarnPointersAsValues: true - misc-include-cleaner.IgnoreHeaders: time.h + misc-const-correctness.WarnPointersAsValues: true + misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: true + readability-magic-numbers.IgnoredIntegerValues: 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 Checks: > - -*, - bugprone-*, + *, + -abseil-*, + -altera-*, + -android-*, + -boost-*, -bugprone-easily-swappable-parameters, -bugprone-empty-catch, - -bugprone-exception-escape, - cert-*, -cert-err58-cpp, - concurrency-*, - -concurrency-mt-unsafe, - cppcoreguidelines-*, -cppcoreguidelines-avoid-c-arrays, -cppcoreguidelines-avoid-do-while, + -cppcoreguidelines-avoid-magic-numbers, -cppcoreguidelines-avoid-non-const-global-variables, - -cppcoreguidelines-interfaces-global-init, -cppcoreguidelines-macro-usage, - -cppcoreguidelines-missing-std-forward, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, - -cppcoreguidelines-special-member-functions, - hicpp-*, + -cppcoreguidelines-pro-bounds-constant-array-index, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-pro-type-vararg, + -darwin-*, + -fuchsia-*, + -google-*, -hicpp-avoid-c-arrays, -hicpp-braces-around-statements, - -hicpp-named-parameter, -hicpp-no-array-decay, - -hicpp-special-member-functions, + -hicpp-signed-bitwise, -hicpp-use-auto, - misc-*, + -hicpp-vararg, + -linuxkernel-*, + -llvm-*, + -llvmlibc-*, -misc-definitions-in-headers, - modernize-*, + -misc-use-internal-linkage, + -modernize-avoid-bind, -modernize-avoid-c-arrays, -modernize-use-auto, + -modernize-use-constraints, + -modernize-use-designated-initializers, + -modernize-use-ranges, + -modernize-use-std-numbers, -modernize-use-trailing-return-type, - performance-*, - portability-*, - readability-*, + -mpi-*, + -objc-*, + -openmp-*, + -performance-enum-size, + -readability-avoid-nested-conditional-operator, -readability-braces-around-statements, - -readability-const-return-type, - -readability-named-parameter, - -readability-redundant-member-init, - -readability-use-anyofallof, + -readability-function-cognitive-complexity, + -readability-identifier-length, + -zircon-*, +HeaderFileExtensions: [h, hpp, cppm] HeaderFilterRegex: .* diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 1f02ed3..1fd9cab 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -3,8 +3,6 @@ name: Bug report about: Found a bug? Report it here. title: "[BUG]" labels: bug -assignees: bshoshany - --- **Describe the bug** diff --git a/.github/ISSUE_TEMPLATE/failed-tests.md b/.github/ISSUE_TEMPLATE/failed-tests.md index 1d20f23..f6e59b9 100644 --- a/.github/ISSUE_TEMPLATE/failed-tests.md +++ b/.github/ISSUE_TEMPLATE/failed-tests.md @@ -3,8 +3,6 @@ name: Failed tests about: The provided automated tests failed on your system? Report it here. title: "[TEST]" labels: bug -assignees: bshoshany - --- **System information** diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 7137cea..ac7e0a2 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -3,8 +3,6 @@ name: Feature request about: Want a new feature? Suggest it here. title: "[REQ]" labels: enhancement -assignees: bshoshany - --- **Describe the new feature** diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 14d4b0e..0e18f48 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,6 +1,6 @@ **Pull request policy (please read)** -> Contributions are always welcome. However, I release my projects in cumulative updates after editing and testing them locally on my system, so my policy is not to accept any pull requests. If you open a pull request, and I decide to incorporate your suggestion into the project, I will first modify your code to comply with the project's coding conventions (formatting, syntax, naming, comments, programming practices, etc.), and perform some tests to ensure that the change doesn't break anything. I will then merge it into the next release of the project, possibly together with some other changes. The new release will also include a note in `CHANGELOG.md` with a link to your pull request, and modifications to the documentation in `README.md` as needed. +> Contributions are always welcome. However, I release my projects in cumulative updates after editing and testing them locally on my system, so **my policy is to never accept any pull requests**. If you open a pull request, and I decide to incorporate your suggestion into the project, I will first modify your code to comply with the project's coding conventions (formatting, syntax, naming, comments, programming practices, etc.), and perform some tests to ensure that the change doesn't break anything. I will then merge it into the next release of the project, possibly together with some other changes. The new release will also include a note in `CHANGELOG.md` with a link to your pull request, and modifications to the documentation in `README.md` as needed. **Describe the changes** @@ -16,7 +16,7 @@ Have you linted your code using the `.clang-tidy` file attached to this project? **Testing** -Have you tested the new code using the provided automated test program `BS_thread_pool_test.cpp` (preferably with the provided multi-compiler test script `BS_thread_pool_test.ps1`) and/or performed any other tests to ensure that the new code works correctly? +Have you tested the new code using the provided automated test program `BS_thread_pool_test.cpp` (preferably with the provided test script `test_all.py`) and/or performed any other tests to ensure that the new code works correctly? If so, please provide information about the test system(s): diff --git a/.vscode-linux/c_cpp_properties.json b/.vscode-linux/c_cpp_properties.json new file mode 100644 index 0000000..b86215e --- /dev/null +++ b/.vscode-linux/c_cpp_properties.json @@ -0,0 +1,110 @@ +{ + "configurations": [ + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerArgs": [ + "-stdlib=libc++" + ], + "compilerPath": "/usr/bin/clang++", + "cppStandard": "c++17", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "linux-clang-x64", + "name": "Clang C++17" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerArgs": [ + "-stdlib=libc++" + ], + "compilerPath": "/usr/bin/clang++", + "cppStandard": "c++20", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "linux-clang-x64", + "name": "Clang C++20" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerArgs": [ + "-stdlib=libc++" + ], + "compilerPath": "/usr/bin/clang++", + "cppStandard": "c++23", + "cStandard": "c23", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "linux-clang-x64", + "name": "Clang C++23" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "/usr/bin/g++", + "cppStandard": "c++17", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "linux-gcc-x64", + "name": "GCC C++17" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "/usr/bin/g++", + "cppStandard": "c++20", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "linux-gcc-x64", + "name": "GCC C++20" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "/usr/bin/g++", + "cppStandard": "c++23", + "cStandard": "c23", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "linux-gcc-x64", + "name": "GCC C++23" + } + ], + "version": 4 +} diff --git a/.vscode-linux/launch.json b/.vscode-linux/launch.json new file mode 100644 index 0000000..afe98e5 --- /dev/null +++ b/.vscode-linux/launch.json @@ -0,0 +1,59 @@ +{ + "configurations": [ + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++17)", + "preLaunchTask": "Build for debugging (Clang C++17)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp17", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++20)", + "preLaunchTask": "Build for debugging (Clang C++20)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp20", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++23)", + "preLaunchTask": "Build for debugging (Clang C++23)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp23", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (GCC C++17)", + "preLaunchTask": "Build for debugging (GCC C++17)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-gcc-cpp17", + "request": "launch", + "type": "cppdbg" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (GCC C++20)", + "preLaunchTask": "Build for debugging (GCC C++20)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-gcc-cpp20", + "request": "launch", + "type": "cppdbg" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (GCC C++23)", + "preLaunchTask": "Build for debugging (GCC C++23)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-gcc-cpp23", + "request": "launch", + "type": "cppdbg" + } + ], + "version": "0.2.0" +} diff --git a/.vscode-linux/tasks.json b/.vscode-linux/tasks.json new file mode 100644 index 0000000..dc945a2 --- /dev/null +++ b/.vscode-linux/tasks.json @@ -0,0 +1,532 @@ +{ + "tasks": [ + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++17", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (GCC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++20", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (GCC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++23", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (GCC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++17", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (GCC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++20", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (GCC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++23", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (GCC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++17", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (GCC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++20", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (GCC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++23", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using g++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (GCC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/clear_folder.py", + "../build" + ], + "command": "python3", + "detail": "Delete all files in the build folder.", + "group": "test", + "label": "Clear build folder", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + }, + { + "args": [ + "scripts/clear_folder.py", + "../temp" + ], + "command": "python3", + "detail": "Delete all files in the temp folder.", + "group": "test", + "label": "Clear temp folder", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + }, + { + "args": [ + "scripts/test_all.py" + ], + "command": "python3", + "detail": "Compile and run the test program with all available compilers and all relevant C++ standards.", + "group": "test", + "label": "Run all tests", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + } + ], + "version": "2.0.0" +} diff --git a/.vscode-macos/c_cpp_properties.json b/.vscode-macos/c_cpp_properties.json new file mode 100644 index 0000000..c5c0821 --- /dev/null +++ b/.vscode-macos/c_cpp_properties.json @@ -0,0 +1,44 @@ +{ + "configurations": [ + { + "compilerPath": "/usr/local/opt/llvm/bin/clang++", + "cppStandard": "c++17", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "macos-clang-x64", + "name": "Clang C++17" + }, + { + "compilerPath": "/usr/local/opt/llvm/bin/clang++", + "cppStandard": "c++20", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "macos-clang-x64", + "name": "Clang C++20" + }, + { + "compilerPath": "/usr/local/opt/llvm/bin/clang++", + "cppStandard": "c++23", + "cStandard": "c23", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "macos-clang-x64", + "name": "Clang C++23" + } + ], + "version": 4 +} diff --git a/.vscode-macos/launch.json b/.vscode-macos/launch.json new file mode 100644 index 0000000..e26fabc --- /dev/null +++ b/.vscode-macos/launch.json @@ -0,0 +1,32 @@ +{ + "configurations": [ + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++17)", + "preLaunchTask": "Build for debugging (Clang C++17)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp17", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++20)", + "preLaunchTask": "Build for debugging (Clang C++20)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp20", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++23)", + "preLaunchTask": "Build for debugging (Clang C++23)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp23", + "request": "launch", + "type": "lldb" + } + ], + "version": "0.2.0" +} diff --git a/.vscode-macos/tasks.json b/.vscode-macos/tasks.json new file mode 100644 index 0000000..dd5d118 --- /dev/null +++ b/.vscode-macos/tasks.json @@ -0,0 +1,298 @@ +{ + "tasks": [ + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=debug", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=release", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=release", + "-r", + "-v" + ], + "command": "python3", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/clear_folder.py", + "../build" + ], + "command": "python3", + "detail": "Delete all files in the build folder.", + "group": "test", + "label": "Clear build folder", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + }, + { + "args": [ + "scripts/clear_folder.py", + "../temp" + ], + "command": "python3", + "detail": "Delete all files in the temp folder.", + "group": "test", + "label": "Clear temp folder", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + }, + { + "args": [ + "scripts/test_all.py" + ], + "command": "python3", + "detail": "Compile and run the test program with all available compilers and all relevant C++ standards.", + "group": "test", + "label": "Run all tests", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + } + ], + "version": "2.0.0" +} diff --git a/.vscode-windows/c_cpp_properties.json b/.vscode-windows/c_cpp_properties.json new file mode 100644 index 0000000..9e79264 --- /dev/null +++ b/.vscode-windows/c_cpp_properties.json @@ -0,0 +1,158 @@ +{ + "configurations": [ + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerArgs": [ + "-stdlib=libc++" + ], + "compilerPath": "C:/msys64/clang64/bin/clang++.exe", + "cppStandard": "c++17", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-clang-x64", + "name": "Clang C++17" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerArgs": [ + "-stdlib=libc++" + ], + "compilerPath": "C:/msys64/clang64/bin/clang++.exe", + "cppStandard": "c++20", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-clang-x64", + "name": "Clang C++20" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerArgs": [ + "-stdlib=libc++" + ], + "compilerPath": "C:/msys64/clang64/bin/clang++.exe", + "cppStandard": "c++23", + "cStandard": "c23", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-clang-x64", + "name": "Clang C++23" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "C:/msys64/ucrt64/bin/g++.exe", + "cppStandard": "c++17", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-gcc-x64", + "name": "GCC C++17" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "C:/msys64/ucrt64/bin/g++.exe", + "cppStandard": "c++20", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-gcc-x64", + "name": "GCC C++20" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "C:/msys64/ucrt64/bin/g++.exe", + "cppStandard": "c++23", + "cStandard": "c23", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-gcc-x64", + "name": "GCC C++23" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe", + "cppStandard": "c++17", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-msvc-x64", + "name": "MSVC C++17" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe", + "cppStandard": "c++20", + "cStandard": "c17", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-msvc-x64", + "name": "MSVC C++20" + }, + { + "browse": { + "limitSymbolsToIncludedHeaders": true + }, + "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe", + "cppStandard": "c++23", + "cStandard": "c23", + "defines": [ + "BS_THREAD_POOL_NATIVE_EXTENSIONS" + ], + "includePath": [ + "${workspaceFolder}/**" + ], + "intelliSenseMode": "windows-msvc-x64", + "name": "MSVC C++23" + } + ], + "version": 4 +} diff --git a/.vscode-windows/launch.json b/.vscode-windows/launch.json new file mode 100644 index 0000000..a869852 --- /dev/null +++ b/.vscode-windows/launch.json @@ -0,0 +1,89 @@ +{ + "configurations": [ + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++17)", + "preLaunchTask": "Build for debugging (Clang C++17)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp17", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++20)", + "preLaunchTask": "Build for debugging (Clang C++20)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp20", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (Clang C++23)", + "preLaunchTask": "Build for debugging (Clang C++23)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-clang-cpp23", + "request": "launch", + "type": "lldb" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (GCC C++17)", + "preLaunchTask": "Build for debugging (GCC C++17)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-gcc-cpp17", + "request": "launch", + "type": "cppdbg" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (GCC C++20)", + "preLaunchTask": "Build for debugging (GCC C++20)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-gcc-cpp20", + "request": "launch", + "type": "cppdbg" + }, + { + "args": [], + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (GCC C++23)", + "preLaunchTask": "Build for debugging (GCC C++23)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-gcc-cpp23", + "request": "launch", + "type": "cppdbg" + }, + { + "args": [], + "console": "integratedTerminal", + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (MSVC C++17)", + "preLaunchTask": "Build for debugging (MSVC C++17)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-msvc-cpp17", + "request": "launch", + "type": "cppvsdbg" + }, + { + "args": [], + "console": "integratedTerminal", + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (MSVC C++20)", + "preLaunchTask": "Build for debugging (MSVC C++20)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-msvc-cpp20", + "request": "launch", + "type": "cppvsdbg" + }, + { + "args": [], + "console": "integratedTerminal", + "cwd": "${workspaceFolder}${/}build", + "name": "Build and debug (MSVC C++23)", + "preLaunchTask": "Build for debugging (MSVC C++23)", + "program": "${workspaceFolder}${/}build${/}${fileBasenameNoExtension}_debug-msvc-cpp23", + "request": "launch", + "type": "cppvsdbg" + } + ], + "version": "0.2.0" +} diff --git a/.vscode-windows/tasks.json b/.vscode-windows/tasks.json new file mode 100644 index 0000000..ee2e307 --- /dev/null +++ b/.vscode-windows/tasks.json @@ -0,0 +1,766 @@ +{ + "tasks": [ + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++17", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (GCC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++20", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (GCC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++23", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (GCC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++17", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (MSVC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$msCompile" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++20", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (MSVC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$msCompile" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++23", + "-t=debug", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and debugging flags.", + "group": "build", + "label": "Build for debugging (MSVC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$msCompile" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++17", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (GCC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++20", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (GCC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++23", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and optimization flags.", + "group": "build", + "label": "Build optimized (GCC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$gcc" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++17", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and optimization flags.", + "group": "build", + "label": "Build optimized (MSVC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$msCompile" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++20", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and optimization flags.", + "group": "build", + "label": "Build optimized (MSVC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$msCompile" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++23", + "-t=release", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and optimization flags.", + "group": "build", + "label": "Build optimized (MSVC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "problemMatcher": [ + "$msCompile" + ], + "type": "cppbuild" + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++17", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++20", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=clang++", + "-s=c++23", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using clang++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (Clang C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++17", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (GCC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++20", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (GCC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=g++", + "-s=c++23", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using g++ with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (GCC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++17", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (MSVC C++17)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++20", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (MSVC C++20)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/compile_cpp.py", + "${file}", + "-c=cl", + "-s=c++23", + "-t=release", + "-r", + "-v" + ], + "command": "python", + "detail": "Compile active file using cl with warning and optimization flags and run the program.", + "group": "test", + "label": "Build optimized and run (MSVC C++23)", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + } + }, + { + "args": [ + "scripts/clear_folder.py", + "../build" + ], + "command": "python", + "detail": "Delete all files in the build folder.", + "group": "test", + "label": "Clear build folder", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + }, + { + "args": [ + "scripts/clear_folder.py", + "../temp" + ], + "command": "python", + "detail": "Delete all files in the temp folder.", + "group": "test", + "label": "Clear temp folder", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + }, + { + "args": [ + "scripts/test_all.py" + ], + "command": "python", + "detail": "Compile and run the test program with all available compilers and all relevant C++ standards.", + "group": "test", + "label": "Run all tests", + "presentation": { + "clear": false, + "echo": true, + "focus": false, + "panel": "shared", + "reveal": "always", + "revealProblems": "onProblem", + "showReuseMessage": true + }, + "type": "shell" + } + ], + "version": "2.0.0" +} diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce9116..0141d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ -# `BS::thread_pool`: a fast, lightweight, and easy-to-use C++17 thread pool library +# `BS::thread_pool`: a fast, lightweight, modern, and easy-to-use C++17 / C++20 / C++23 thread pool library -By Barak Shoshany\ +By **Barak Shoshany**\ Email: \ Website: \ GitHub: * [Version history](#version-history) + * [v5.0.0 (2024-12-19)](#v500-2024-12-19) * [v4.1.0 (2024-03-22)](#v410-2024-03-22) * [v4.0.1 (2023-12-28)](#v401-2023-12-28) * [v4.0.0 (2023-12-27)](#v400-2023-12-27) @@ -29,6 +30,152 @@ GitHub: ## Version history +### v5.0.0 (2024-12-19) + +* A major new release with many new features, improvements, bug fixes, and performance optimizations! Please note that code written using previous releases may 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. +* **Highlights:** + * Added support for C++20 and C++23, while maintaining full C++17 compatibility. In C++20, the library can now optionally be imported as a module using `import BS.thread_pool` on Clang, GCC, and MSVC. In C++23, both the library itself and the test program can now optionally import the C++ Standard Library as a module using `import std` on supported compilers and platforms. Extensive documentation has been added to `README.md` on how to use these features, to ease the transition. + * Optional features are now enabled via a bitmask template parameter instead of macros, using the flags `BS::tp::priority`, `BS::tp::pause`, and `BS::tp::wait_deadlock_checks`. This makes the optional features easier to use, allows multiple thread pools with different features to coexist, and makes the library compatible with C++20 modules. Exception handling is now disabled automatically if exceptions are disabled, instead of using a macro. + * Added optional native extensions for non-portable features using the operating system's native API: setting the priority and affinity for processes and threads, and setting thread names. These have been tested on the latest versions of Windows, Ubuntu, and macOS. + * This library is now back to being a true single-header library, with a single header file `BS_thread_pool.hpp`. The utility classes have been combined into the main header file. `BS::timer` has been removed, `BS::signaller` has been replaced with `BS::binary_semaphore` and `BS::counting_semaphore` (in C++17 mode only), and `BS::synced_stream` now supports multiple output streams. + * Cleanup functions can now be defined to complement the initialization functions. Both initialization and cleanup functions can now optionally take the index of the thread as an argument. + * Parallelization member functions no longer need type casting or template parameters if the start and end indices are of different types. + * The worker function no longer incorrectly reads shared variables while the mutex is unlocked. + * The type aliases `BS::this_thread::optional_index` and `BS::this_thread::optional_pool` have been removed. Instead, `BS::this_thread::get_index()` returns `std::optional`, and `BS::this_thread::get_pool()` returns `std::optional`. The latter must be cast to the correct instantiation of the `BS::thread_pool` class template before using any member functions. + * The thread pool version is now accessible using the object `BS::thread_pool_version`, a `constexpr struct` of type `BS::version` with the members `major`, `minor`, and `patch`. This works even if importing the library as a C++20 module, unlike the version macros. + * The type `priority_t`, used to set priorities, is now defined as `std::int8_t`, which means it takes values from -128 to +127. The pre-defined priorities in `BS::pr`, such as `BS::pr::highest` or `BS::pr::lowest`, have been updated accordingly. + * Exceptions thrown by detached tasks are now caught and prevented from propagating, so that they do not terminate the program. Exceptions thrown by submitted tasks are still rethrown when calling `get()` on the future, as before. + * Parallelization member functions no longer destruct objects prematurely under certain circumstances. + * The test program has been expanded with many new tests for both old and new features. It can also import both the thread pool module using `import BS.thread_pool` (in C++20 and later) and the C++ Standard Library module using `import std` (in C++23) if the appropriate macros are defined, and read default command line arguments from a `default_args.txt` file for debugging purposes. + * Added new and improved benchmarks using a highly-optimized multithreaded algorithm which generates a plot of the Mandelbrot set, utilizing a normalized iteration count algorithm and linear interpolation to create smooth coloring. + * The type `BS::concurrency_t` has been removed; use `std::size_t` instead. +* **C++20 and C++23 support:** + * This library now officially supports C++20 and C++23 in addition to C++17. If compiled with C++20 and/or C++23 support (e.g. using the compiler flag `-std=c++23` in Clang/GCC or `/std:c++latest` on MSVC), the library will make use of newly available features for maximum performance, reliability, and usability. + * To be clear, the library is still fully compatible with any C++17 standard-compliant compiler. I have no plans to remove C++17 support at the moment, as it is still [the most widely used C++ standard](https://www.jetbrains.com/lp/devecosystem-2023/cpp/) among developers, but that might change in the future. + * If C++20 features are available, the library can be imported as a module using `import BS.thread_pool`. This is now the officially recommended way to use the library, as it has many benefits, such as faster compilation times, better encapsulation, no namespace pollution, no include order issues, easier maintainability, simpler dependency management, and more. + * The module file itself is `BS.thread_pool.cppm`, located in the `modules` folder, and it is just a thin wrapper around the header file `BS_thread_pool.hpp`. + * The `constexpr` flag `BS::thread_pool_module` indicates whether the thread pool library was compiled as a module. + * To my knowledge, `BS::thread_pool` is one of the only popular C++ libraries that are [currently available as a C++20 module](https://arewemodulesyet.org/) (and certainly the only thread pool library). This feature has been tested with the latest versions of Clang, GCC, and MSVC. Unfortunately, C++20 modules are still (4 years later!) not fully implemented in all compilers, and each compiler implements them differently; for instructions on how to compile and import the `BS.thread_pool` module in each compiler, please see `README.md`. + * Known issues: + * GCC v14.2.0 (latest version at the time of writing) appears to have an internal compiler error when compiling programs containing modules (or at least, this particular module) with any optimization flags other than `-Og` enabled. Until this is fixed, if you wish to use compiler optimizations, please either include the library as a header file or use a different compiler. + * On macOS, Apple Clang v16.0.0 (latest version at the time of writing) does not support C++20 modules. Please either install the latest version of LLVM Clang using [Homebrew](https://formulae.brew.sh/formula/llvm), or include the library as a header file. + * Visual Studio Code's C/C++ extension v1.23.2 (latest version at the time of writing) does not yet support modules. My temporary solution for that, as demonstrated in the test program, is to define the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE` (see below) when compiling the test program, but not when editing in the IDE. If the macro is enabled, the module is imported via `import BS.thread_pool`, otherwise the header file is included using `#include "BS_thread_pool.hpp"` as usual. + * If C++23 features are available, both the library and the test program can now import the C++ Standard Library as a module using `import std`. To enable this, define the macro `BS_THREAD_POOL_IMPORT_STD` at compilation time. This is currently only officially supported by recent versions of MSVC with Microsoft STL or LLVM Clang (**not** Apple Clang) with LLVM libc++. It is not supported by GCC with any standard library, Clang with any standard library other than libc++, any compiler with GNU libstdc++, or any other compiler. + * If `BS_THREAD_POOL_IMPORT_STD` is defined, then you must also import the library itself as a module. If the library is included as a header file, this will force the program that included the header file to also import `std`, which is not desirable and can lead to compilation errors if the program `#include`s any Standard Library header files. + * Defining the macro before importing the module will not work, as modules cannot access macros defined in the program that imported them. Instead, define the macro as a compiler flag, e.g. `-D BS_THREAD_POOL_IMPORT_STD` (or `/D` for MSVC). + * The `constexpr` flag `BS::thread_pool_import_std` indicates whether the thread pool library was compiled with `import std`. Note that the flag will be `false` if `BS_THREAD_POOL_IMPORT_STD` is defined but the compiler or standard library does not support importing the C++ Standard Library as a module. + * If C++20 features are available, the pool will use `std::jthread` instead of `std::thread`. This allows considerable simplification and added safety, since the threads no longer need to be manually joined, and `std::stop_token` is used to stop the workers automatically when destructing the threads. This eliminates the need for the `destroy_threads()` member function, as well as the `workers_running` flag, which are now only used in C++17 mode. + * If C++20 features are available, the library will use concepts to enforce the signature of the initialization function and to selectively enable member functions related to pausing only if pausing is enabled. In C++17 mode, the library will use SFINAE to achieve essentially the same effect. + * If C++23 features are available, the task queue will use `std::move_only_function` instead of `std::function`. This allows `submit_task()` to work without using a shared pointer, which should increase performance. + * **API migration:** All of the C++20/C++23 features listed above are either automatically applied based on compiler settings or optional. If you are still using C++17, or if you are using C++20 or C++23 but do not wish to import the thread pool library and/or the C++ Standard Library as a module, no changes are needed. +* **Optional features overhaul:** + * All optional features are now enabled via a bitmask template parameter instead of macros. This works using `if constexpr`, `std::conditional_t`, and concepts (in C++20 and later) or SFINAE (in C++17). + * This change makes the optional features much easier and more intuitive to use, as you no longer need to define any macros before including the header file. + * Additionally, it allows you to have multiple thread pools in the same program with different optional features enabled or disabled. For example, you can have one pool with task priority enabled and another without. + * Most importantly, this makes it possible to import the library as a C++20 module, as macros cannot be read by imported modules. + * The bitmask flags are members of the `BS::tp` enumeration: + * `BS::tp::priority` enables task priority (previously enabled via the macro `BS_THREAD_POOL_ENABLE_PRIORITY`, which has been removed). + * `BS::tp::pause` enables pausing the pool (previously enabled via the macro `BS_THREAD_POOL_ENABLE_PAUSE`, which has been removed). + * `BS::tp::wait_deadlock_checks` enables deadlock checks in `wait()`/`wait_for()`/`wait_until()` (previously enabled via the macro `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK`, which has been removed). + * The default is `BS::tp::none`, which disables all optional features. + * Convenience aliases are defined as follows: + * `BS::light_thread_pool` disables all optional features (equivalent to `BS::thread_pool` with the default template parameter, that is, `BS::thread_pool`). + * `BS::priority_thread_pool` enables task priority (equivalent to `BS::thread_pool`). + * `BS::pause_thread_pool` enables pausing the pool (equivalent to `BS::thread_pool`). + * `BS::wdc_thread_pool` enables wait deadlock checks (equivalent to `BS::thread_pool`). + * There are no aliases with multiple features enabled; if this is desired, you must either pass the template parameter explicitly or define your own alias. Note that the parameter is a bitmask, so to enable multiple features, you need to use the bitwise OR operator `|`, e.g. `BS::thread_pool` to enable both task priority and pausing. + * The macro `BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING` has been removed. Exception handling is disabled automatically if exceptions are disabled, based on whether the feature-test macro `__cpp_exceptions` is defined. + * The exception thrown by wait deadlock checks is now `BS::wait_deadlock` instead of `BS::thread_pool::wait_deadlock`, to avoid having to deal with different template parameters. + * The macro `BS_THREAD_POOL_LIGHT_TEST` has been removed from the test program, as all optional features are now tested by enabling them selectively via the template parameter, so there is no need to compile with different macros. + * If for some reason you forgot which options you enabled when creating the pool, the `static constexpr` members `priority_enabled`, `pause_enabled`, and `wait_deadlock_checks_enabled` can be used to check if the corresponding features are enabled. + * **API migration:** + * `BS::thread_pool` can still be used without the template parameter, for backwards compatibility; this will create a thread pool with all optional features disabled. Therefore, if you did not use any of the optional features in existing code, no changes are needed. + * If your code uses any of the optional features by defining macros before including the header file, please remove these macros, and instead either use one of the convenience aliases above or define the template parameter explicitly using the `BS::tp` enumeration when creating the pool. + * If you use wait deadlock checks, you must now catch the exception `BS::wait_deadlock` instead of `BS::thread_pool::wait_deadlock`. +* **Native extensions:** + * While portability is one of my guiding principle when developing this library, non-portable features such as setting the thread priority using the operating system's native API are frequently requested by users. Starting with this release, the library includes native extensions, which are disabled by default. + * Currently, the extensions provide the following functions (please see `README.md` for details on how to use them): + * `BS::get_os_process_affinity()` and `BS::set_os_process_affinity()` to get and set the CPU affinity of the current process in a portable way. Should work on Windows and Linux, but not on macOS, as the native API does not allow it. + * `BS::get_os_process_priority()` and `BS::set_os_process_priority()` to get and set the priority of the current process in a portable way. Should work on Windows, Linux, and macOS. + * `BS::this_thread::get_os_thread_affinity()` and `BS::this_thread::set_os_thread_affinity()` to get and set the CPU affinity of the current thread in a portable way. Should work on Windows and Linux, but not on macOS, as the native API does not allow it. + * `BS::this_thread::get_os_thread_priority()` and `BS::this_thread::set_os_thread_priority()` to get and set the priority of the current thread in a portable way. Should work on Windows, Linux, and macOS. + * `BS::this_thread::get_os_thread_name()` and `BS::this_thread::set_os_thread_name()` to get and set the name of the current thread in a portable way, for debugging purposes. Should work on Windows, Linux, and macOS. + * The native extensions may be enabled by defining the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` at compilation time. + * Even if the macro is defined, the extensions are disabled automatically if a supported operating system (Windows, Linux, or macOS) is not detected. + * Note that if you are using the library as a C++20 module, defining the macro before importing the module will not work, as modules cannot access macros defined in the program that imported them. Instead, define the macro as a compiler flag, e.g. `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` (or `/D` for MSVC). + * The macro `BS_THREAD_POOL_ENABLE_NATIVE_HANDLES` has been removed. The thread pool member function `get_native_handles()` is now part of the native extensions, so it is enabled using the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS`. + * Please note that the native extensions have only been tested on Windows 11 23H2, Ubuntu 24.10, and macOS 15.1. They have not been tested on older versions of these operating systems, other Linux distributions, or any other operating systems, and are therefore not guaranteed to work on every system. If you encounter any issues, please report them on the GitHub repository. + * The test program only tests the native extensions if the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` is defined at compilation time. If importing the library as a module, please ensure that the macro is also enabled when compiling the module. + * The `constexpr` flag `BS::thread_pool_native_extensions` indicates whether the thread pool library was compiled with native extensions enabled. Note that the flag will be `false` if `BS_THREAD_POOL_NATIVE_EXTENSIONS` is defined but the operating system is unsupported. + * **API migration:** The native extensions are a brand new optional feature and do not require any changes to existing code. +* **Utility classes:** + * This library is now back to being a true single-header library, with a single header file `BS_thread_pool.hpp`. The utility classes (previously in a separate header `BS_thread_pool_utils.hpp`, which has been removed) have been combined into the main header file. + * The `BS::timer` class has been removed from the library, since it doesn't really have anything to do with multithreading directly. However, it is still available in the test program if you want to use it. + * The `BS::signaller` class has been removed from the library, and replaced with `BS::binary_semaphore` and `BS::counting_semaphore`, which are C++17 polyfills for the C++20 classes `std::binary_semaphore` and `std::counting_semaphore`. If C++20 features are available, the polyfills are not used, and instead are just aliases for the standard library classes. The reason is that semaphores can do the same thing that the signaller class was previously used for, but are much more versatile. + * The `BS::synced_stream` class now supports printing to more than one output stream. + * **API migration:** + * If you previously included the `BS_thread_pool_utils.hpp` header file, this is no longer needed. Only include the header `BS_thread_pool.hpp`, or better yet, in C++20 or later, import the library as a module using `import BS.thread_pool`. + * If you previously used the `BS::timer` class, it is no longer available in the header file, but if you still need it you can copy it into your program directly from the test program `BS_thread_pool_test.cpp`. + * If you previously used the `BS::signaller` class, you can replace it with `BS::binary_semaphore` or `BS::counting_semaphore`. Previously, you defined an object `BS::signaller signal`, and then used `signal.wait()` to wait for the signal, and `signal.ready()` to unblock all waiting threads. Now, you can define an object `BS::counting_semaphore signal(0)`, and use `signal.acquire()` to wait for the signal, and `signal.release(num_threads)` to unblock waiting threads; note that the number of threads to release must be passed explicitly, as the semaphore also allows you to unblock only some of them. Use `BS::binary_semaphore` if only one thread will be waiting at any given time. + * If you previously used the `BS::synced_stream` class, no changes are needed. +* **Cleanup and initialization functions:** + * Using the new `set_cleanup_func()` member function, it is now possible to provide the pool with a cleanup function to run in each thread right before it is destroyed, which will happen when the pool is destructed or reset. See [#152](https://github.com/bshoshany/thread-pool/issues/152). + * Both initialization and cleanup functions can now optionally take the index of the thread as an argument. + * Added a warning in the documentation that both initialization and cleanup functions must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. + * **API migration:** No changes to existing code are needed. +* **Parallelization index types:** + * All member functions which parallelize collections of tasks, namely `detach_blocks()`, `detach_loop()`, `detach_sequence()`, `submit_blocks()`, `submit_loop()`, and `submit_sequence()`, can now be called with start and end indices of different types. + * Previously, the indices had to be of the same type, or the template parameter had to be explicitly specified; this is no longer needed, as the library will automatically cast the indices to a suitable common type. + * This was already possible in v2.X.X and v3.X.X, where it was done using [`std::common_type`](https://en.cppreference.com/w/cpp/types/common_type), but I removed it in v4.X.X because `std::common_type` sometimes completely messed up the range of the loop. For example, the `std::common_type` of `int` and `unsigned int` is `unsigned int`, which means the loop will only use non-negative indices even if the `int` start index was negative, resulting in an integer overflow. + * Starting with v5.0.0, the library uses a custom type trait `BS::common_index_type` to determine the common type of the indices. The common type of two signed integers or two unsigned integers is the larger of the integers, while the common type of a signed and an unsigned integer is a signed integer that can hold the full ranges of both integers. This avoids messing up the indices, except in the case of `std::uint64_t`, where there is no fundamental signed type that can hold its entire range. In this case, we choose `std::uint64_t` as the common type, since the most common use case is where the indices go from 0 to `x` where `x` has been previously defined as `std::size_t`. This will fail if the first index is negative; in that case, the user must cast the indices explicitly. + * **API migration:** Existing code which uses type casting or explicit template parameters in parallelization functions does not need to be changed, but it can be simplified by removing the casting or template parameters. However, if one index is negative and the other is an unsigned 64-bit integer, casting is still needed (although you should probably not be doing this in the first place, as casting to either of the two types will result in potential narrowing or overflow). +* **`BS::this_thread`:** + * `BS::this_thread` is now a class instead of a namespace, since defining it as a namespace proved to be incompatible with C++20 modules (at least in some compilers). Defining it as a class also results in a simpler implementation. However, the functionality remains the same, and since it only has static methods, the call syntax for `BS::this_thread::get_index()` and `BS::this_thread::get_pool()` is unchanged. + * The type aliases `BS::this_thread::optional_index` and `BS::this_thread::optional_pool` have been removed. Instead, `BS::this_thread::get_index()` now returns the explicit type `std::optional`, and `BS::this_thread::get_pool()` returns `std::optional`. + * The rationale for this removal is that using `std::optional` explicitly provides more information about the type that is being returned, and most users are probably not using the explicit types anyway (either by using `auto` or by invoking the `std::optional` member functions directly on the returned object). + * Note that `BS::this_thread::get_pool()` now returns an optional `void*` instead of `BS::thread_pool*`. The reason for that is that `BS::thread_pool` is now a template. Once you obtain the pool pointer, you must cast it to the desired instantiation of the template if you want to use any member functions. Note that you have to cast it to the correct type; if you cast a pointer to a `BS::light_thread_pool` into a pointer to a `BS::priority_thread_pool`, for example, your program will have undefined behavior. + * **API migration:** + * If your code uses the type aliases, please replace `BS::this_thread::optional_index` with `std::optional` and `BS::this_thread::optional_pool` with `std::optional`. + * If your code uses `BS::this_thread::get_pool()`, you must now cast the returned pointer to the correct instantiation of the `BS::thread_pool` class template before using any member functions. +* **Determining the library version:** + * The library now defines the `constexpr` object `BS::thread_pool_version`, which can be used to check the version of the library at compilation time. This object is of type `BS::version`, with members `major`, `minor`, and `patch`, and all comparison operators defined as `constexpr`. It also has a `to_string()` member function and an `operator<<` overload for easy printing at runtime. For example, you can do `static_assert(BS::thread_pool_version == BS::version(5, 0, 0))`, or you can use it in `if constexpr` for conditional compilation. + * The version macros `BS_THREAD_POOL_VERSION_MAJOR`, `BS_THREAD_POOL_VERSION_MINOR`, and `BS_THREAD_POOL_VERSION_PATCH` are still defined, since they can be used in conditional code inclusion, and for backwards compatibility. However, since C++20 modules cannot export macros, `BS::thread_pool_version` is the only way to check the version of the thread pool library if you are importing it as a module. + * **API migration:** No changes needed in existing code; if you previously used the macros `BS_THREAD_POOL_VERSION_MAJOR`, `BS_THREAD_POOL_VERSION_MINOR`, and `BS_THREAD_POOL_VERSION_PATCH` to determine the version of the library when including it as a header file, you can still do so. However, if you wish to import the library as a C++20 module, you must use the object `BS::thread_pool_version` instead. +* **Task priority:** + * The type `priority_t`, used to set priorities, is now defined as `std::int8_t`, which means it takes values from -128 to +127. The pre-defined priorities in `BS::pr`, such as `BS::pr::highest` or `BS::pr::lowest`, have been updated accordingly (also, it is now an `enum` instead of a namespace). The old priority type `std::int16_t` was unnecessarily large; having fewer priority values means less bookkeeping in the priority queue, which should also improve performance. + * **API migration:** If you used the pre-defined priorities in `BS::pr`, no changes are needed. If you specified numerical priorities directly, you may need to adjust them to the new range of -128 to +127. +* **Miscellaneous:** + * Exceptions thrown by detached tasks are now caught and prevented from propagating, so that they do not terminate the program. Exceptions thrown by submitted tasks are still rethrown when calling `get()` on the future, as before. + * All member functions which parallelize collections of tasks, namely `detach_blocks()`, `detach_loop()`, `detach_sequence()`, `submit_blocks()`, `submit_loop()`, and `submit_sequence()`, now store the callable object inside an `std::shared_ptr`, and then pass that shared pointer to each subtask. Previously, the callable was passed using perfect forwarding, which under some circumstances resulted in mistakenly moving the callable during the first iteration of the loop, thus potentially destructing captured objects prematurely. The new shared pointer method resolves this issue, while also avoiding making copies of the callable. See [#149](https://github.com/bshoshany/thread-pool/issues/149). + * Fixed incorrect reading of shared variables while the mutex is unlocked in the worker function. See [#159](https://github.com/bshoshany/thread-pool/issues/159). + * Added documentation to `README.md` for all the new features. In addition, fixed some typos and other minor issues in the existing documentation. + * Added instructions in `README.md` for installing the library using CMake with `FetchContent` instead of CPM. See [#155](https://github.com/bshoshany/thread-pool/pull/155). + * The type `BS::concurrency_t` has been removed. In previous versions this type was defined to be the type of the value returned by `std::thread::hardware_concurrency()` (which is supposed to be `unsigned int`), for maximum portability. However, in practice this value is only used to indicate the size of arrays, so `std::size_t` is more appropriate, and this simplifies the code. + * **API migration:** If you used `BS::concurrency_t` in your code, please replace it with `std::size_t`. If you previously cast to/from these two types, you can now remove the cast. +* **Tests:** + * The test program `BS_thread_pool_test.cpp` will import the library as a C++20 module via `import BS.thread_pool` if the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE` is defined, C++20 or later is detected, and a supported compiler is used. + * The test program will also import the C++ Standard Library as a module using `import std` if the macro `BS_THREAD_POOL_IMPORT_STD` is defined during compilation, on supported compilers and platforms. + * The new test `check_copy()` checks that the callable object does not get copied when parallelized into multiple tasks. It will succeed on previous versions of the library, but not if perfect forwarding is removed. + * The new test `check_shared_ptr()` checks that captured shared pointers do not prematurely destruct. It will fail on previous versions. + * The new test `check_task_destruct()` checks that a task is destructed immediately after it executes, and therefore does not artificially extend the lifetime of any captured objects. + * The new test `check_common_index_type()` checks that the type trait `BS::common_index_type` (see above) works as expected. + * The new tests `check_os_process_priorities()`, `check_os_thread_priorities()`, `check_os_process_affinity()`, `check_os_thread_affinity()`, and `check_os_thread_names()` check the corresponding features of the native extensions. + * The new test `check_callables()` checks that different callable types are accepted by the thread pool. + * New command line argument: `stdout`, to print to the standard output, enabled by default. + * If the file `default_args.txt` exists in the same folder, the test program reads the default arguments from it (space separated in a single line). Command line arguments can still override these defaults. This is useful when debugging. + * The test program will now detect and log the OS, compiler, standard library, C++ standard, available C++ features, whether the thread pool was imported as a C++20 module, and whether the standard library was imported as a module. +* **Benchmarks:** + * Added new and improved benchmarks using a highly-optimized multithreaded algorithm which generates a plot of the Mandelbrot set, utilizing a normalized iteration count algorithm and linear interpolation to create smooth coloring. + * These benchmarks are heavily CPU-intensive, and much less limited by memory and cache compared to the benchmarks in previous versions (which used vector or matrix operations). This results in a much higher speedup factor due to multithreading, utilizing every core and thread to their fullest extent. This makes these benchmarks more useful for optimizing the library, since they are more sensitive to the thread pool's own performance. + * The full benchmarks are enabled using the command line argument `benchmarks`, which is enabled by default. The command line argument `plot` can be used to just plot the Mandelbrot set once, either instead of or in addition to doing the full benchmarks. This will plot the largest possible image that can be plotted in 5 seconds, and only measure the performance in pixels/ms for the entire plot. + * If you want to see the actual plot, pass the `save` command line argument. The plot is saved to a BMP file, since I didn't want to depend on any 3rd-party libraries. This is off by default, since that file can get quite large. +* **Development:** + * A Python script `compile_cpp.py` has been added to the repository, in the `scripts` folder. It can be used to compile any C++ source file with different compilers on different platforms. The compilation parameters can be configured using command line arguments and/or via an optional YAML configuration file `compile_cpp.yaml` which specifies defined macros, extra compiler flags (per compiler), include folders, modules, and the output folder. + * I wrote this script to make it easier for me to test the library with different combinations of compilers, standards, and platforms using the built-in Visual Studio Code tasks. I also included three `.vscode` folders (one for each OS) in the repository, with appropriate `c_cpp_properties.json`, `launch.json`, and `tasks.json` files that utilize this script, in case you want to use it in your own projects. However, note that this script is not meant to replace CMake or any full-fledged build system, it's just a convenient script for developing single-header libraries like this one or other small projects. + * The `compile_cpp.py` script also transparently handles C++20 modules and importing the C++ Standard Library as a module in C++23. Therefore, users of this library who wish to import it as a C+20 module may find this script particularly useful. + * Another Python script `test_all.py` in the `scripts` folder replaces the old PowerShell test script. Tests are now performed in C++17, C++20, and C++23 modes, using all compilers available in the system (Clang, GCC, and/or MSVC). Since there are so many tests, the test script now no longer performs the benchmarks, as that would take too long. + * A final Python script `clear_folder.py` in the `scripts` folder is used to clean up output and temporary folders, and integrates with VS Code tasks. + ### v4.1.0 (2024-03-22) * This library is now published in [SoftwareX](https://www.sciencedirect.com/journal/softwarex)! If you use it in published research, please cite it as follows: Barak Shoshany, *"A C++17 Thread Pool for High-Performance Scientific Computing"*, [doi:10.1016/j.softx.2024.101687](https://doi.org/10.1016/j.softx.2024.101687), [SoftwareX 26 (2024) 101687](https://www.sciencedirect.com/science/article/pii/S235271102400058X), [arXiv:2105.00613](https://arxiv.org/abs/2105.00613) diff --git a/CITATION.cff b/CITATION.cff index 5898faf..05e3a98 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -1,22 +1,25 @@ -cff-version: 1.2.0 -message: If you use this library in published research, please cite it as follows. authors: - - given-names: Barak + - email: baraksh@gmail.com family-names: Shoshany + given-names: Barak orcid: https://orcid.org/0000-0003-2222-127X - email: bshoshany@brocku.ca -title: A C++17 Thread Pool for High-Performance Scientific Computing +cff-version: 1.2.0 doi: 10.1016/j.softx.2024.101687 -url: https://github.com/bshoshany/thread-pool +license: MIT +message: If you use this library in published research, please cite it as follows. preferred-citation: - type: article authors: - - given-names: Barak - family-names: Shoshany - title: A C++17 Thread Pool for High-Performance Scientific Computing + - family-names: Shoshany + given-names: Barak + doi: 10.1016/j.softx.2024.101687 journal: SoftwareX + start: 101687 + title: A C++17 Thread Pool for High-Performance Scientific Computing + type: article + url: https://www.sciencedirect.com/science/article/pii/S235271102400058X volume: 26 year: 2024 - start: 101687 - doi: 10.1016/j.softx.2024.101687 - url: https://www.sciencedirect.com/science/article/pii/S235271102400058X +repository-code: https://github.com/bshoshany/thread-pool +title: A C++17 Thread Pool for High-Performance Scientific Computing +type: software +url: https://github.com/bshoshany/thread-pool diff --git a/README.md b/README.md index 859de63..4794b5a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![DOI: 10.1016/j.softx.2024.101687](https://img.shields.io/badge/DOI-10.1016%2Fj.softx.2024.101687-b31b1b)](https://doi.org/10.1016/j.softx.2024.101687) [![arXiv:2105.00613](https://img.shields.io/badge/arXiv-2105.00613-b31b1b)](https://arxiv.org/abs/2105.00613) [![License: MIT](https://img.shields.io/github/license/bshoshany/thread-pool)](https://github.com/bshoshany/thread-pool/blob/master/LICENSE.txt) -[![Language: C++17](https://img.shields.io/badge/Language-C%2B%2B17-yellow)](https://cppreference.com/) +[![Language: C++17 / C++20 / C++23](https://img.shields.io/badge/Language-C%2B%2B17%20%2F%20C%2B%2B20%20%2F%20C%2B%2B23-yellow)](https://cppreference.com/) [![GitHub stars](https://img.shields.io/github/stars/bshoshany/thread-pool?style=flat&color=009999)](https://github.com/bshoshany/thread-pool/stargazers) [![GitHub forks](https://img.shields.io/github/forks/bshoshany/thread-pool?style=flat&color=009999)](https://github.com/bshoshany/thread-pool/forks) [![GitHub release](https://img.shields.io/github/v/release/bshoshany/thread-pool?color=660099)](https://github.com/bshoshany/thread-pool/releases) @@ -11,24 +11,23 @@ [![Conan version](https://img.shields.io/conan/v/bshoshany-thread-pool?color=6600ff)](https://conan.io/center/recipes/bshoshany-thread-pool) [![Open in Visual Studio Code](https://img.shields.io/badge/Open_in_Visual_Studio_Code-007acc)](https://vscode.dev/github/bshoshany/thread-pool) -# `BS::thread_pool`: a fast, lightweight, and easy-to-use C++17 thread pool library +# `BS::thread_pool`: a fast, lightweight, modern, and easy-to-use C++17 / C++20 / C++23 thread pool library -By Barak Shoshany\ +By **Barak Shoshany**\ Email: \ Website: \ GitHub: -This is the complete documentation for v4.1.0 of the library, released on 2024-03-22. +This is the complete documentation for **v5.0.0** of the library, released on **2024-12-19**. * [Introduction](#introduction) * [Motivation](#motivation) * [Overview of features](#overview-of-features) - * [Compiling and compatibility](#compiling-and-compatibility) * [Getting started](#getting-started) * [Installing the library](#installing-the-library) + * [Compiling and compatibility](#compiling-and-compatibility) * [Constructors](#constructors) * [Getting and resetting the number of threads in the pool](#getting-and-resetting-the-number-of-threads-in-the-pool) - * [Finding the version of the library](#finding-the-version-of-the-library) * [Submitting tasks to the queue](#submitting-tasks-to-the-queue) * [Submitting tasks with no arguments and receiving a future](#submitting-tasks-with-no-arguments-and-receiving-a-future) * [Submitting tasks with arguments and receiving a future](#submitting-tasks-with-arguments-and-receiving-a-future) @@ -37,128 +36,195 @@ This is the complete documentation for v4.1.0 of the library, released on 2024-0 * [Class member functions as tasks](#class-member-functions-as-tasks) * [Parallelizing loops](#parallelizing-loops) * [Automatic parallelization of loops](#automatic-parallelization-of-loops) + * [Optimizing the number of blocks](#optimizing-the-number-of-blocks) + * [Common index types](#common-index-types) * [Parallelizing loops without futures](#parallelizing-loops-without-futures) * [Parallelizing individual indices vs. blocks](#parallelizing-individual-indices-vs-blocks) * [Loops with return values](#loops-with-return-values) * [Parallelizing sequences](#parallelizing-sequences) - * [More about `BS::multi_future`](#more-about-bsmulti_futuret) + * [More about `BS::multi_future`](#more-about-bsmulti_future) * [Utility classes](#utility-classes) * [Synchronizing printing to a stream with `BS::synced_stream`](#synchronizing-printing-to-a-stream-with-bssynced_stream) - * [Measuring execution time with `BS::timer`](#measuring-execution-time-with-bstimer) - * [Sending simple signals between threads with `BS::signaller`](#sending-simple-signals-between-threads-with-bssignaller) + * [Synchronizing tasks with `BS::counting_semaphore` and `BS::binary_semaphore`](#synchronizing-tasks-with-bscounting_semaphore-and-bsbinary_semaphore) * [Managing tasks](#managing-tasks) * [Monitoring the tasks](#monitoring-the-tasks) * [Purging tasks](#purging-tasks) * [Exception handling](#exception-handling) - * [Getting information about the threads](#getting-information-about-the-threads) - * [Thread pool initialization functions](#thread-pool-initialization-functions) + * [Getting information about the current thread](#getting-information-about-the-current-thread) + * [Thread initialization functions](#thread-initialization-functions) + * [Thread cleanup functions](#thread-cleanup-functions) * [Passing task arguments by constant reference](#passing-task-arguments-by-constant-reference) * [Optional features](#optional-features) + * [Enabling features](#enabling-features) + * [Setting task priority](#setting-task-priority) * [Pausing the pool](#pausing-the-pool) * [Avoiding wait deadlocks](#avoiding-wait-deadlocks) +* [Native extensions](#native-extensions) + * [Enabling the native extensions](#enabling-the-native-extensions) + * [Setting thread priority](#setting-thread-priority) + * [Setting thread affinity](#setting-thread-affinity) + * [Setting thread names](#setting-thread-names) + * [Setting process priority](#setting-process-priority) + * [Setting process affinity](#setting-process-affinity) * [Accessing native thread handles](#accessing-native-thread-handles) - * [Setting task priority](#setting-task-priority) * [Testing the library](#testing-the-library) * [Automated tests](#automated-tests) * [Performance tests](#performance-tests) + * [Finding the version of the library](#finding-the-version-of-the-library) +* [Importing the library as a C++20 module](#importing-the-library-as-a-c20-module) + * [Compiling the module](#compiling-the-module) + * [Compiling with `compile_cpp.py` using `import BS.thread_pool`](#compiling-with-compile_cpppy-using-import-bsthread_pool) + * [Compiling with Clang using `import BS.thread_pool`](#compiling-with-clang-using-import-bsthread_pool) + * [Compiling with GCC using `import BS.thread_pool`](#compiling-with-gcc-using-import-bsthread_pool) + * [Compiling with MSVC using `import BS.thread_pool`](#compiling-with-msvc-using-import-bsthread_pool) + * [Compiling with CMake using `import BS.thread_pool`](#compiling-with-cmake-using-import-bsthread_pool) +* [Importing the C++23 Standard Library as a module](#importing-the-c23-standard-library-as-a-module) + * [Enabling `import std`](#enabling-import-std) + * [Compiling with `compile_cpp.py` using `import std`](#compiling-with-compile_cpppy-using-import-std) + * [Compiling with Clang and LLVM libc++ using `import std`](#compiling-with-clang-and-llvm-libc-using-import-std) + * [Compiling with MSVC and Microsoft STL using `import std`](#compiling-with-msvc-and-microsoft-stl-using-import-std) + * [Compiling with CMake using `import std`](#compiling-with-cmake-using-import-std) * [Installing the library using package managers](#installing-the-library-using-package-managers) * [Installing using vcpkg](#installing-using-vcpkg) * [Installing using Conan](#installing-using-conan) * [Installing using Meson](#installing-using-meson) * [Installing using CMake with CPM](#installing-using-cmake-with-cpm) + * [Installing using CMake with `FetchContent`](#installing-using-cmake-with-fetchcontent) * [Complete library reference](#complete-library-reference) - * [Main thread pool header file (`BS_thread_pool.hpp`)](#main-thread-pool-header-file-bs_thread_poolhpp) - * [The `BS::thread_pool` class](#the-bsthread_pool-class) - * [Optional features for the `BS::thread_pool` class](#optional-features-for-the-bsthread_pool-class) - * [The `BS::this_thread` namespace](#the-bsthis_thread-namespace) - * [The `BS::multi_future` class](#the-bsmulti_futuret-class) - * [Utility header file (`BS_thread_pool_utils.hpp`)](#utility-header-file-bs_thread_pool_utilshpp) - * [The `BS::signaller` class](#the-bssignaller-class) - * [The `BS::synced_stream` class](#the-bssynced_stream-class) - * [The `BS::timer` class](#the-bstimer-class) + * [The `BS::thread_pool` class template](#the-bsthread_pool-class-template) + * [Optional features and the template parameter](#optional-features-and-the-template-parameter) + * [The `BS::this_thread` class](#the-bsthis_thread-class) + * [The native extensions](#the-native-extensions) + * [The `BS::multi_future` class](#the-bsmulti_future-class) + * [The `BS::synced_stream` class](#the-bssynced_stream-class) + * [The `BS::version` class](#the-bsversion-class) + * [Diagnostic variables](#diagnostic-variables) + * [All names exported by the C++20 module](#all-names-exported-by-the-c20-module) +* [Development tools](#development-tools) + * [The `compile_cpp.py` script](#the-compile_cpppy-script) + * [Other included tools](#other-included-tools) * [About the project](#about-the-project) - * [Issue and pull request policy](#issue-and-pull-request-policy) - * [Acknowledgements](#acknowledgements) + * [Bug reports and feature requests](#bug-reports-and-feature-requests) + * [Contribution and pull request policy](#contribution-and-pull-request-policy) * [Starring the repository](#starring-the-repository) + * [Acknowledgements](#acknowledgements) * [Copyright and citing](#copyright-and-citing) + * [About the author](#about-the-author) * [Learning more about C++](#learning-more-about-c) + * [Other projects to check out](#other-projects-to-check-out) ## Introduction ### Motivation -Multithreading is essential for modern high-performance computing. Since C\+\+11, the C++ standard library has included built-in low-level multithreading support using constructs such as `std::thread`. However, `std::thread` creates a new thread each time it is called, which can have a significant performance overhead. Furthermore, it is possible to create more threads than the hardware can handle simultaneously, potentially resulting in a substantial slowdown. +Multithreading is essential for modern high-performance computing. Since C++11, the C++ standard library has included built-in low-level multithreading support using constructs such as `std::thread`. However, `std::thread` creates a new thread each time it is called, which can have a significant performance overhead. Furthermore, it is possible to create more threads than the hardware can handle simultaneously, potentially resulting in a substantial slowdown. -The library presented here contains a C++ thread pool class, `BS::thread_pool`, which avoids these issues by creating a fixed pool of threads once and for all, and then continuously reusing the same threads to perform different tasks throughout the lifetime of the program. By default, the number of threads in the pool is equal to the maximum number of threads that the hardware can run in parallel. +The library presented here contains a C++ thread pool class, `BS::thread_pool`, which avoids these issues by creating a fixed pool of threads once and for all, and then continuously reusing the same threads to perform different tasks throughout the lifetime of the program. By default, the number of threads in the pool is equal to the maximum number of threads that the hardware can run in parallel. -The user submits tasks to be executed into a queue. Whenever a thread becomes available, it retrieves the next task from the queue and executes it. The pool automatically produces an `std::future` for each task, which allows the user to wait for the task to finish executing and/or obtain its eventual return value, if applicable. Threads and tasks are autonomously managed by the pool in the background, without requiring any input from the user aside from submitting the desired tasks. +The user submits tasks to be executed into a queue. Whenever a thread becomes available, it retrieves the next task from the queue and executes it. The pool optionally produces an `std::future` for each task, which allows the user to wait for the task to finish executing and/or obtain its eventual return value, if applicable. Threads and tasks are autonomously managed by the pool in the background, without requiring any input from the user aside from submitting the desired tasks. -The design of this library is guided by four important principles. First, *compactness*: the entire library consists of just one self-contained header file, with no other components or dependencies, aside from a small self-contained header file with optional utilities. Second, *portability*: the library only utilizes the C\+\+17 standard library, without relying on any compiler extensions or 3rd-party libraries, and is therefore compatible with any modern standards-conforming C\+\+17 compiler on any platform. Third, *ease of use*: the library is extensively documented, and programmers of any level should be able to use it right out of the box. +The design of this library is guided by four important principles. First, *compactness*: the entire library consists of just one self-contained header file, with no other components or dependencies. Second, *portability*: the library only utilizes the C++ standard library, without relying on any compiler extensions or 3rd-party libraries, and is therefore compatible with any modern standards-conforming C++ compiler on any platform, as long as it supports C++17 or later. Third, *ease of use*: the library is extensively documented, and programmers of any level should be able to use it right out of the box. The fourth and final guiding principle is *performance*: each and every line of code in this library was carefully designed with maximum performance in mind, and performance was tested and verified on a variety of compilers and platforms. Indeed, the library was originally designed for use in the author's own computationally-intensive scientific computing projects, running both on high-end desktop/laptop computers and high-performance computing nodes. -Other, more advanced multithreading libraries may offer more features and/or higher performance. However, they typically consist of a vast codebase with multiple components and dependencies, and involve complex APIs that require a substantial time investment to learn. This library is not intended to replace these more advanced libraries; instead, it was designed for users who don't require very advanced features, and prefer a simple and lightweight library that is easy to learn and use and can be readily incorporated into existing or new projects. +Among the available C++ thread pool libraries, `BS::thread_pool` occupies the crucial middle ground between small bare-bones thread pool classes that offer rudimentary functionality and are only suitable for simple programs, and very large libraries that offer many advanced features but consist of multiple components and dependencies and involve complex APIs that require a substantial time investment to learn. `BS::thread_pool` was designed for users who want a simple and lightweight header-only library that is easy to learn and use, and can be readily incorporated into existing or new projects, but do not want to compromise on performance or functionality. + +Obtaining the library is quick and easy; it can be downloaded manually from [the GitHub repository](https://github.com/bshoshany/thread-pool), or installed automatically using a variety of package managers and build systems. The library can be imported either as a traditional [header-only library](#installing-the-library), or as a modern [C++20 module](#importing-the-library-as-a-c20-module). `BS::thread_pool` has undergone extensive testing on multiple platforms and is actively used by thousands of C++ developers worldwide for a wide range of applications, from scientific computing to game development. ### Overview of features * **Fast:** - * Built from scratch with maximum performance in mind. + * Built from scratch with [maximum performance](#performance-tests) in mind. * 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. * 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 tasks running in parallel than is allowed by the hardware. + * All optional features can be selectively turned on to ensure minimal overhead. * **Lightweight:** - * Single header file: simply `#include "BS_thread_pool.hpp"` and you're all set! + * Single header file: simply [`#include "BS_thread_pool.hpp"`](#installing-the-library) and you're all set! * Header-only: no need to install or build the library. * Self-contained: no external requirements or dependencies. - * Portable: uses only the C++ standard library, and works with any C++17-compliant compiler. - * Only 304 lines of code, excluding comments, blank lines, and lines containing only a single brace, with all optional features disabled. - * Only 396 lines of code across both header files with all optional features enabled and including all optional utilities. + * Portable: uses only the C++ standard library, and works with any C++17-compliant compiler on any platform. + * Only 487 lines of code, including all optional features and utility classes (excluding comments, blank lines, lines containing only a single brace, C++17 polyfills, and native extensions). +* **Modern:** + * Fully supports C++17, C++20, and C++23, taking advantage of the latest language features when available for maximum performance, reliability, and usability. + * In C++20, the library can an be imported as a C++20 module using [`import BS.thread_pool`](#importing-the-library-as-a-c20-module), with many benefits, such as faster compilation times and avoiding namespace pollution. + * In C++23, the library can import the C++ Standard Library as a module using [`import std`](#importing-the-c23-standard-library-as-a-module) on supported compilers and platforms. + * Makes use of modern C++ programming practices for readability, maintainability, performance, safety, portability, and reliability. * **Easy to use:** - * Very simple operation, using only a handful of member functions, with additional member functions for more advanced use. - * Every task submitted to the queue using the `submit_task()` member function automatically generates an `std::future`, which can be used to wait for the task to finish executing and/or obtain its eventual return value. - * Loops can be automatically parallelized into any number of tasks using the `submit_loop()` member function, which returns a `BS::multi_future` that can be used to track the execution of all parallel tasks at once. - * If futures are not needed, tasks may be submitted using `detach_task()`, and loops can be parallelized using `detach_loop()` - sacrificing convenience for even greater performance. In that case, `wait()`, `wait_for()`, and `wait_until()` can be used to wait for all the tasks in the queue to complete. + * Very simple operation, using only a handful of member functions for basic use, with many additional member functions, classes, and functions for more advanced use. + * Every task submitted to the queue using [`submit_task()`](#submitting-tasks-to-the-queue) automatically generates an `std::future`, which can be used to wait for the task to finish executing, obtain its eventual return value, and/or catch any thrown exceptions. + * Loops can be automatically parallelized into any number of tasks using [`submit_loop()`](#parallelizing-loops), which returns a [`BS::multi_future`](#more-about-bsmulti_future) that can be used to track the execution of all parallel tasks at once. + * If futures are not needed, tasks may be submitted using [`detach_task()`](#detaching-and-waiting-for-tasks), and loops can be parallelized using [`detach_loop()`](#parallelizing-loops-without-futures) - sacrificing convenience for even greater performance. In that case, `wait()`, `wait_for()`, and `wait_until()` can be used to wait for all the tasks in the queue to complete. + * Extremely thorough and detailed documentation, with numerous examples, is available in the library's [`README.md` file](https://github.com/bshoshany/thread-pool/blob/master/README.md), with a total of 3,359 lines and 25,506 words! * The code is thoroughly documented using Doxygen comments - not only the interface, but also the implementation, in case the user would like to make modifications. - * The included test program `BS_thread_pool_test.cpp` can be used to perform exhaustive automated tests and benchmarks, and also serves as a comprehensive example of how to properly use the library. The included PowerShell script `BS_thread_pool_test.ps1` provides a portable way to run the tests with multiple compilers. -* **Utility classes:** - * The optional header file `BS_thread_pool_utils.hpp` contains several useful utility classes. - * Send simple signals between threads using the `BS::signaller` utility class. - * Synchronize output to a stream from multiple threads in parallel using the `BS::synced_stream` utility class. - * Easily measure execution time for benchmarking purposes using the `BS::timer` utility class. + * Optionally, the included Python script [`compile_cpp.py`](#the-compile_cpppy-script) can be used to easily compile any programs that are using the library, with full support for C++20 modules and C++23 Standard Library modules where applicable. * **Additional features:** - * Assign a priority to each task using the optional task priority feature. Tasks with higher priorities will be executed first. - * Submit a sequence of tasks enumerated by indices to the queue using `detach_sequence()` and `submit_sequence()`. - * Change the number of threads in the pool safely and on-the-fly as needed using the `reset()` member function. - * Monitor the number of queued and/or running tasks using the `get_tasks_queued()`, `get_tasks_running()`, and `get_tasks_total()` member functions. - * Get the current thread count of the pool using `get_thread_count()`. - * Freely pause and resume the pool using the `pause()`, `unpause()`, and `is_paused()` member functions; when paused, threads do not retrieve new tasks out of the queue. - * Purge all tasks currently waiting in the queue with the `purge()` member function. - * Catch exceptions thrown by tasks submitted using `submit_task()` or `submit_loop()` from the main thread through their futures. - * Run an initialization function in each thread before it starts to execute any submitted tasks. - * Get the pool index of the current thread using `BS::this_thread::get_index()` and a pointer to the pool that owns the thread using `BS::this_thread::get_pool()`. - * Get the unique thread IDs for all threads in the pool using `get_thread_ids()` or the implementation-defined thread handles using the optional `get_native_handles()` member function. - * Submit class member functions to the pool, either applied to a specific object or from within the object itself. - * Pass arguments to tasks by value, reference, or constant reference. + * Get the current thread count of the pool using [`get_thread_count()`](#getting-and-resetting-the-number-of-threads-in-the-pool). + * Change the number of threads in the pool safely and on-the-fly as needed using [`reset()`](#getting-and-resetting-the-number-of-threads-in-the-pool). + * Monitor the number of queued and/or running tasks using [`get_tasks_queued()`, `get_tasks_running()`, and `get_tasks_total()`](#monitoring-the-tasks). + * Purge all tasks currently waiting in the queue with [`purge()`](#purging-tasks). + * Run an [initialization function](#thread-initialization-functions) in each thread before it starts to execute any submitted tasks, by passing it to the `BS::thread_pool` constructor. + * Run a cleanup function in each thread right before it is destroyed, using [`set_cleanup_func()`](#thread-cleanup-functions). + * Assume lower-level control of parallelized loops using [`detach_blocks()` and `submit_blocks()`](#parallelizing-individual-indices-vs-blocks). + * Parallelize a sequence of tasks enumerated by indices to the queue using [`detach_sequence()` and `submit_sequence()`](#parallelizing-sequences). + * Get [information about the current thread](#getting-information-about-the-current-thread): the pool index using `BS::this_thread::get_index()` and a pointer to the owning pool using `BS::this_thread::get_pool()`. + * Get the unique thread IDs for all threads in the pool using [`get_thread_ids()`](#getting-and-resetting-the-number-of-threads-in-the-pool). + * Synchronize output to one or more streams from multiple threads in parallel using the [`BS::synced_stream`](#synchronizing-printing-to-a-stream-with-bssynced_stream) utility class. + * Access C++20 semaphores in C++17 using the [`BS::binary_semaphore` and `BS::counting_semaphore`](#synchronizing-tasks-with-bscounting_semaphore-and-bsbinary_semaphore) polyfill classes. +* **Optional features:** + * [Optional features](#enabling-features) can be enabled by passing a bitmask template parameter to the `BS::thread_pool` class template. + * Assign a priority to each task using the optional [task priority](#setting-task-priority) feature. The priority, in the range -128 to +127, is passed as the last argument to all `submit` and `detach` member functions. Tasks with higher priorities will be executed first. + * Freely pause and resume the pool using `pause()`, `unpause()`, and `is_paused()` with the optional [pausing](#pausing-the-pool) feature. When paused, threads do not retrieve new tasks out of the queue. + * Avoid deadlocks using the optional [wait deadlock checks](#avoiding-wait-deadlocks) feature. If a deadlock is detected while waiting for tasks, the pool will throw the exception `BS::wait_deadlock`. +* **Native extensions:** + * The library includes optional [native extensions](#native-extensions), which contain non-portable features using the operating system's native API, enabled by defining the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` at compilation time. This feature should work on most Windows, Linux, and macOS systems. + * Use [`BS::this_thread::get_os_thread_priority()` and `BS::this_thread::set_os_thread_priority()`](#setting-thread-priority) to get and set the priority of the current thread. + * Use [`BS::this_thread::get_os_thread_affinity()` and `BS::this_thread::set_os_thread_affinity()`](#setting-thread-affinity) to get and set the processor affinity of the current thread. + * Use [`BS::this_thread::get_os_thread_name()` and `BS::this_thread::set_os_thread_name()`](#setting-thread-names) to get and set the name of the current thread. + * Use [`BS::get_os_process_priority()` and `BS::set_os_process_priority()`](#setting-process-priority) to get and set the priority of the current process. + * Use [`BS::get_os_process_affinity()` and `BS::set_os_process_affinity()`](#setting-process-affinity) to get and set the processor affinity of the current process. + * Get the implementation-defined thread handles for all threads in the pool using [`get_native_handles()`](#accessing-native-thread-handles). +* **Well-tested:** + * The included test program [`BS_thread_pool_test.cpp`](#automated-tests) performs hundreds of automated tests, and also serves as a comprehensive example of how to properly use the library. + * The test program also performs [benchmarks](#performance-tests) using a highly-optimized multithreaded algorithm which generates a plot of the Mandelbrot set. + * The included Python script `test_all.py` provides a portable way to easily run the tests with multiple compilers. + * [Compatibility](#compiling-and-compatibility) is comprehensively tested on the latest versions of Windows, Ubuntu, and macOS, using Clang, GCC, and MSVC. * Under continuous and active development. Bug reports and feature requests are welcome, and should be made via [GitHub issues](https://github.com/bshoshany/thread-pool/issues). +## Getting started + +### Installing the library + +To install `BS::thread_pool`, simply download the [latest release](https://github.com/bshoshany/thread-pool/releases) from [the GitHub repository](https://github.com/bshoshany/thread-pool), place the header file `BS_thread_pool.hpp` from the `include` folder in the desired folder, and include it in your program: + +```cpp +#include "BS_thread_pool.hpp" +``` + +The thread pool will now be accessible via the `BS::thread_pool` class. For an even quicker installation, you can download the header file itself directly [at this URL](https://raw.githubusercontent.com/bshoshany/thread-pool/master/include/BS_thread_pool.hpp); no additional files are required, as the library is a single-header library. + +This library is also available on various package managers and build system, including [vcpkg](https://vcpkg.io/), [Conan](https://conan.io/), [Meson](https://mesonbuild.com/), and [CMake](https://cmake.org/). Please [see below](#installing-the-library-using-package-managers) for more details. + +If C++20 features are available, the library can also be imported as a C++20 module, in which case `#include "BS_thread_pool.hpp"` should be replaced with `import BS.thread_pool;`. This requires one additional file, and the module must be compiled before it can be used; please see detailed instructions [below](#importing-the-library-as-a-c20-module). + ### Compiling and compatibility -This library should successfully compile on any C++17 standard-compliant compiler, on all operating systems and architectures for which such a compiler is available. Compatibility was verified with a 24-core (8P+16E) / 32-thread Intel i9-13900K CPU using the following compilers and platforms: +This library officially supports C++17, C++20, and C++23. If compiled with C++20 and/or C++23 support, the library will make use of newly available features for maximum performance and usability. However, the library is fully compatible with C++17, and should successfully compile on any C++17 standard-compliant compiler, on all operating systems and architectures for which such a compiler is available. -* Windows 11 build 22631.2861: - * [Clang](https://clang.llvm.org/) v17.0.6 - * [GCC](https://gcc.gnu.org/) v13.2.0 ([MSYS2 build](https://www.msys2.org/)) - * [MSVC](https://docs.microsoft.com/en-us/cpp/) v19.38.33133 -* Ubuntu 23.10: - * [Clang](https://clang.llvm.org/) v17.0.6 - * [GCC](https://gcc.gnu.org/) v13.2.0 +Compatibility was verified using the bundled test program `BS_thread_pool_test.cpp`, compiled using the bundled Python scripts `test_all.py` and `compile_cpp.py` with native extensions enabled, importing the library [as a C++20 module](#importing-the-library-as-a-c20-module) where applicable, and importing the [C++23 Standard Library as a module](#importing-the-c23-standard-library-as-a-module) where applicable, on a 24-core (8P+16E) / 32-thread Intel i9-13900K CPU, using the following compilers, C++ standard libraries, and platforms: -In addition, this library was tested on a [Digital Research Alliance of Canada](https://alliancecan.ca/en) node equipped with two 20-core / 40-thread Intel Xeon Gold 6148 CPUs (for a total of 40 cores and 80 threads), running CentOS Linux 7.9.2009, using [GCC](https://gcc.gnu.org/) v13.2.0. +* Windows 11 23H2 build 22631.4602: + * [Clang](https://clang.llvm.org/) v19.1.4 with LLVM libc++ v19.1.4 ([MSYS2 build](https://www.msys2.org/)) + * [GCC](https://gcc.gnu.org/) v14.2.0 with GNU libstdc++ v14 (20240801) ([MSYS2 build](https://www.msys2.org/)) + * [MSVC](https://docs.microsoft.com/en-us/cpp/) v19.42.34435 with Microsoft STL v143 (202408). +* Ubuntu 24.10: + * [Clang](https://clang.llvm.org/) v19.1.6 with LLVM libc++ v19.1.6 + * [GCC](https://gcc.gnu.org/) v14.2.0 with GNU libstdc++ v14 (20240908) +* macOS 15.1 build 24B83: + * [Clang](https://clang.llvm.org/) v19.1.6 with LLVM libc++ v19.1.6 ([Homebrew build](https://formulae.brew.sh/formula/llvm)) + * Note: Apple Clang is currently not officially supported, as it does not support C++20 modules. -The test program `BS_thread_pool_test.cpp` was compiled without warnings (with the warning flags `-Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow` in GCC/Clang and `/W4` in MSVC), executed, and successfully completed all [automated tests](#testing-the-library) and benchmarks using all of the compilers and systems mentioned above. - -As this library requires C\+\+17 features, the code must be compiled with C\+\+17 support: +As this library requires C++17 features, the code must be compiled with C++17 support: * For Clang or GCC, use the `-std=c++17` flag. On Linux, you will also need to use the `-pthread` flag to enable the POSIX threads library. * For MSVC, use `/std:c++17`, and also `/permissive-` to ensure standards conformance. @@ -168,46 +234,42 @@ For maximum performance, it is recommended to compile with all available compile * For Clang or GCC, use the `-O3` flag. * For MSVC, use `/O2`. -As an example, to compile the test program `BS_thread_pool_test.cpp` with warnings and optimizations, it is recommended to use the following commands: +As an example, to compile the test program `BS_thread_pool_test.cpp` with compiler optimizations, it is recommended to use the following commands: -* On Linux with GCC: `g++ BS_thread_pool_test.cpp -std=c++17 -O3 -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow -pthread -o BS_thread_pool_test` -* On Linux with Clang: replace `g++` with `clang++`. -* On Windows with GCC or Clang: replace `-o BS_thread_pool_test` with `-o BS_thread_pool_test.exe` and remove `-pthread`. -* On Windows with MSVC: `cl BS_thread_pool_test.cpp /std:c++17 /permissive- /O2 /W4 /EHsc /Fo:BS_thread_pool_test.obj /Fe:BS_thread_pool_test.exe` +* Windows: + * GCC: `g++ BS_thread_pool_test.cpp -std=c++17 -O3 -o BS_thread_pool_test.exe` + * Clang: `clang++ BS_thread_pool_test.cpp -std=c++17 -O3 -o BS_thread_pool_test.exe` + * MSVC: `cl BS_thread_pool_test.cpp /std:c++17 /permissive- /O2 /EHsc /Fo:BS_thread_pool_test.obj /Fe:BS_thread_pool_test.exe` +* Linux/macOS: + * GCC: `g++ BS_thread_pool_test.cpp -std=c++17 -O3 -pthread -o BS_thread_pool_test` + * Clang: `clang++ BS_thread_pool_test.cpp -std=c++17 -O3 -pthread -o BS_thread_pool_test` -## Getting started +If your compiler and codebase support C++20 and/or C++23, it is recommended to enable them in order to allow the library access to the latest features: -### Installing the library +* For Clang or GCC, use the `-std=c++20` or `-std=c++23` flag. +* For MSVC, use `/std:c++20` for C++20 or `/std:c++latest` for C++23. -To install `BS::thread_pool`, simply download the [latest release](https://github.com/bshoshany/thread-pool/releases) from the GitHub repository, place the header file `BS_thread_pool.hpp` from the `include` folder in the desired folder, and include it in your program: - -```cpp -#include "BS_thread_pool.hpp" -``` - -The thread pool will now be accessible via the `BS::thread_pool` class. For an even quicker installation, you can download the header file itself directly [at this URL](https://raw.githubusercontent.com/bshoshany/thread-pool/master/include/BS_thread_pool.hpp). - -This library also comes with an independent utilities header file `BS_thread_pool_utils.hpp`, which is not required to use the thread pool, but provides some utility classes that may be helpful for multithreading. This header file also resides in the `include` folder. It can be downloaded directly [at this URL](https://raw.githubusercontent.com/bshoshany/thread-pool/master/include/BS_thread_pool_utils.hpp). - -This library is also available on various package managers and build system, including [vcpkg](https://vcpkg.io/), [Conan](https://conan.io/), [Meson](https://mesonbuild.com/), and [CMake](https://cmake.org/) with [CPM](https://github.com/cpm-cmake/CPM.cmake). Please [see below](#installing-the-library-using-package-managers) for more details. +In addition, if C++20 features are available, the library can be imported as a module; instructions for doing so are provided [below](#importing-the-library-as-a-c20-module). ### Constructors The default constructor creates a thread pool with as many threads as the hardware can handle concurrently, as reported by the implementation via `std::thread::hardware_concurrency()`. This is usually determined by the number of cores in the CPU. If a core is hyperthreaded, it will count as two threads. For example: ```cpp -// Constructs a thread pool with as many threads as available in the hardware. +// Constructs a thread pool with as many threads as are available in the hardware. BS::thread_pool pool; ``` -Optionally, a number of threads different from the hardware concurrency can be specified as an argument to the constructor. However, note that adding more threads than the hardware can handle will **not** improve performance, and in fact will most likely hinder it. This option exists in order to allow using **less** threads than the hardware concurrency, in cases where you wish to leave some threads available for other processes. For example: +Optionally, a number of threads different from the hardware concurrency can be specified as an argument to the constructor. However, note that adding more threads than the hardware can handle will **not** improve performance, and in fact will most likely hinder it. This option exists in order to allow using **fewer** threads than the hardware concurrency, in cases where you wish to leave some threads available for other processes. For example: ```cpp // Constructs a thread pool with only 12 threads. BS::thread_pool pool(12); ``` -Usually, when the thread pool is used, a program's main thread should only submit tasks to the thread pool and wait for them to finish, and should not perform any computationally intensive tasks on its own. In that case, it is recommended to use the default value for the number of threads. This ensures that all of the threads available in the hardware will be put to work while the main thread waits. +Usually, when the thread pool is used, a program's main thread should only submit tasks to the thread pool and wait for them to finish, and should not perform any computationally intensive tasks on its own. If this is the case, it is recommended to use the default value for the number of threads. This ensures that all the threads available in the hardware will be put to work while the main thread waits. + +However, if the main thread also performs computationally intensive tasks, it may be beneficial to use one fewer thread than the hardware concurrency, leaving one hardware thread available for the main thread. Furthermore, if more than one thread pool is used in the program simultaneously, the total number of thread across all pools should not exceed the hardware concurrency. ### Getting and resetting the number of threads in the pool @@ -215,43 +277,21 @@ The member function `get_thread_count()` returns the number of threads in the po It is generally unnecessary to change the number of threads in the pool after it has been created, since the whole point of a thread pool is that you only create the threads once. However, if needed, this can be done, safely and on-the-fly, using the `reset()` member function. -`reset()` will wait for all currently running tasks to be completed, but will leave the rest of the tasks in the queue. Then it will destroy the thread pool and create a new one with the desired new number of threads, as specified in the function's argument (or the hardware concurrency if no argument is given). The new thread pool will then resume executing the tasks that remained in the queue and any new submitted tasks. +`reset()` will wait for all currently running tasks to be completed, but will leave the rest of the tasks in the queue. Then it will destroy the thread pool and create a new one with the desired new number of threads, as specified in the function's argument (or the hardware concurrency if no argument is given). The new thread pool will then resume executing the tasks that remained in the queue and any newly submitted tasks. -### Finding the version of the library - -If desired, the version of this library may be read during compilation time from the following three macros: - -* `BS_THREAD_POOL_VERSION_MAJOR` - indicates the major version. -* `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: - -```none -Thread pool library version is 4.1.0. -``` - -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. +The member function `get_thread_ids()` returns a vector containing the unique identifiers for each of the pool's threads, as obtained by `std::thread::get_id()`. These values are not so useful on their own, but can be used to identify and distinguish between threads, or for allocating resources. ## Submitting tasks to the queue ### Submitting tasks with no arguments and receiving a future -In this section we will learn how to submit a task with no arguments, but potentially with a return value, to the queue. Once a task has been submitted, it will be executed as soon as a thread becomes available. Tasks are executed in the order that they were submitted (first-in, first-out), unless task priority is enabled (see below). +In this section we will learn how to submit a task with no arguments, but potentially with a return value, to the queue. Once a task has been submitted, it will be executed as soon as a thread becomes available. Tasks are executed in the order that they were submitted (first-in, first-out), unless task priority is enabled ([see below](#setting-task-priority)). For example, if the pool has 8 threads and an empty queue, and we submitted 16 tasks, then we should expect the first 8 tasks to be executed in parallel, with the remaining tasks being picked up by the threads one by one as each thread finishes executing its first task, until no tasks are left in the queue. -The member function `submit_task()` is used to submit tasks to the queue. It takes exactly one input, the task to submit. This task must be a function with no arguments, but it can have a return value. The return value is an `std::future` associated to the task. +The member function `submit_task()` is used to submit tasks to the queue. It takes exactly one input, the task to submit. This task must be a function with no arguments, but it can have a return value. -If the submitted function has a return value of type `T`, then the future will be of type `std::future`, and will be set to the return value when the function finishes its execution. If the submitted function does not have a return value, then the future will be an `std::future`, which will not return any value but may still be used to wait for the function to finish. - -Using `auto` for the return value of `submit_task()` means the compiler will automatically detect which instance of the template `std::future` to use. However, specifying the particular type `std::future`, as in the examples below, is recommended for increased readability. +`submit_task()` returns an `std::future` associated to the task. If the submitted task has a return value of type `T`, then the future will be of type `std::future`, and will be set to the task's return value when the task finishes its execution. If the submitted task does not have a return value, then the future will be an `std::future`, which will not contain any value, but may still be used to wait for the task to finish. To wait until the task finishes, use the member function `wait()` of the future. To obtain the return value, use the member function `get()`, which will also automatically wait for the task to finish if it hasn't yet. Here is a simple example: @@ -275,7 +315,7 @@ int main() In this example we submitted the function `the_answer()`, which returns an `int`. The member function `submit_task()` of the pool therefore returned an `std::future`. We then used used the `get()` member function of the future to get the return value, and printed it out. -In addition to submitted a pre-defined function, we can also use a [lambda expression](https://en.cppreference.com/w/cpp/language/lambda) to quickly define the task on-the-fly. Rewriting the previous example in terms of a lambda expression, we get: +In addition to submitting a pre-defined function, we can also use a [lambda expression](https://en.cppreference.com/w/cpp/language/lambda) to quickly define the task on-the-fly. Rewriting the previous example in terms of a lambda expression, we get: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool @@ -324,9 +364,7 @@ Here we split the lambda into multiple lines to make it more readable. The comma ### Submitting tasks with arguments and receiving a future -As stated in the previous section, tasks submitted using `submit_task()` cannot have any arguments. However, it is easy to submit tasks with argument either by wrapping the function in a lambda or using lambda captures directly. Here are two examples. - -The following is an example of submitting a pre-defined function with arguments by wrapping it with a lambda: +As stated in the previous section, tasks submitted using `submit_task()` cannot have any arguments. However, it is easy to submit tasks with argument either by wrapping the function in a lambda or using lambda captures directly. The following is an example of submitting a pre-defined function with arguments by wrapping it in a lambda: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool @@ -350,7 +388,7 @@ int main() } ``` -As you can see, to pass the arguments to `multiply` we simply called `multiply(6, 7)` explicitly inside a lambda. If the arguments are not literals, we need to use the lambda capture clause to capture the arguments from the local scope: +As you can see, to pass the arguments to `multiply()` we simply called `multiply(6, 7)` explicitly inside a lambda. If the arguments are not literals, we can use the lambda capture clause to capture the arguments from the local scope: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool @@ -376,7 +414,7 @@ int main() } ``` -We could even get rid of the `multiply` function entirely and put everything inside a lambda, if desired: +We could even get rid of the `multiply()` function entirely and just put everything inside a lambda, if desired: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool @@ -401,13 +439,13 @@ int main() Usually, it is best to submit a task to the queue using `submit_task()`. This allows you to wait for the task to finish and/or get its return value later. However, sometimes a future is not needed, for example when you just want to "set and forget" a certain task, or if the task already communicates with the main thread or with other tasks without using futures, such as via condition variables. -In such cases, you may wish to avoid the overhead involved in assigning a future to the task in order to increase performance. This is called "detaching" the task, as the task detaches from the main thread and runs independently. +In such cases, you may wish to avoid the overhead involved in assigning a future to the task, in order to increase performance. This is called "detaching" the task, as the task detaches from the main thread and runs independently. -Detaching tasks is done using the `detach_task()` member function, which allows you to detach a task to the queue without generating a future for it. The task can have any number of arguments, but it cannot have a return value, as there would be no way for the main thread to retrieve that value. +Detaching tasks is done using the `detach_task()` member function, which allows you to detach a task to the queue without generating a future for it. As with `submit_task()`, the task must have no arguments, but you can pass arguments by wrapping it in a lambda, as shown in the previous section. However, tasks executed via `detach_task()` cannot have a return value, as there would be no way for the main thread to retrieve that value. Since `detach_task()` does not return a future, there is no built-in way for the user to know when the task finishes executing. You must manually ensure that the task finishes executing before trying to use anything that depends on its output. Otherwise, bad things will happen! -`BS::thread_pool` provides the member function `wait()` to facilitate waiting for all of the tasks in the queue to complete, whether they were detached or submitted with a future. The `wait()` member function works similarly to the `wait()` member function of `std::future`. Consider, for example, the following code: +`BS::thread_pool` provides the member function `wait()` to facilitate waiting for all the tasks in the queue to complete, whether they were detached or submitted with a future. The `wait()` member function works similarly to the `wait()` member function of `std::future`. Consider, for example, the following code: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool @@ -429,7 +467,9 @@ int main() } ``` -This program first defines a local variable named `result` and initializes it to `0`. It then detaches a task in the form of a lambda expression. Note that the lambda captures `result` **by reference**, as indicated by the `&` in front of it. This means that the task can modify `result`, and any such modification will be reflected in the main thread. The task changes `result` to `42`, but it first sleeps for 100 milliseconds. When the main thread prints out the value of `result`, the task has not yet had time to modify its value, since it is still sleeping. Therefore, the program will print out the initial value `0`. +This program first defines a local variable named `result` and initializes it to `0`. It then detaches a task in the form of a lambda expression. Note that the lambda captures `result` **by reference**, as indicated by the `&` in front of it. This means that the task can modify `result`, and any such modification will be reflected in the main thread. + +The task changes `result` to `42`, but it first sleeps for 100 milliseconds. When the main thread prints out the value of `result`, the task has not yet had time to modify its value, since it is still sleeping. Therefore, the program will actually print out the initial value `0`, which is not what we want. To wait for the task to complete, we must use the `wait()` member function after detaching it: @@ -454,18 +494,18 @@ int main() } ``` -Now the program will print out the value `42`, as expected. Note, however, that `wait()` will wait for **all** the tasks in the queue, including any other tasks that were potentially submitted before or after the one we care about. If we want to wait for just one task, `submit_task()` would be a better choice. +Now the program will print out the value `42`, as expected. Note, however, that `wait()` will wait for **all** the tasks in the queue, including any other tasks that were potentially submitted before or after the one we care about. If we want to wait just for **one** task, `submit_task()` would be a better choice. ### Waiting for submitted or detached tasks with a timeout Sometimes you may wish to wait for the tasks to complete, but only for a certain amount of time, or until a specific point in time. For example, if the tasks have not yet completed after some time, you may wish to let the user know that there is a delay. -For task submitted with futures using `submit_task()`, this can be achieved using two member functions of `std::future`: +For tasks submitted with futures using `submit_task()`, this can be achieved using two member functions of `std::future`: * `wait_for()` waits for the task to be completed, but stops waiting after the specified duration, given as an argument of type `std::chrono::duration`, has passed. * `wait_until()` waits for the task to be completed, but stops waiting after the specified time point, given as an argument of type `std::chrono::time_point`, has been reached. -In both cases, the functions will return `future_status::ready` if the future is ready, meaning the task is finished and its return value, if any, has been obtained. However, it will return `std::future_status::timeout` if the future is not yet ready by the time the timeout has expired. +In both cases, the functions will return `std::future_status::ready` if the future is ready, meaning the task is finished and its return value, if any, has been obtained. However, they will return `std::future_status::timeout` if the future is not yet ready when the timeout has expired. Here is an example: @@ -505,7 +545,7 @@ Sorry, the task is not done yet. Task done! ``` -For detached tasks, since we do not have a future for them, we cannot use this method. However, `BS::thread_pool` has two member functions, also named `wait_for()` and `wait_until()`, which similarly wait for a specified duration or until a specified time point, but do so for **all** tasks (whether submitted or detached). Instead of an `std::future_status`, the thread pool's wait functions returns `true` if all tasks finished running, or `false` if the duration expired or the time point was reached but some tasks are still running. +For detached tasks, since we do not have futures for them, we cannot use this method. However, `BS::thread_pool` has two member functions, also named `wait_for()` and `wait_until()`, which similarly wait for a specified duration or until a specified time point, but do so for **all** tasks (whether submitted or detached). Instead of an `std::future_status`, the thread pool's wait functions returns `true` if all tasks finished running, or `false` if the duration expired or the time point was reached but some tasks are still running. Here is the same example as above, using `detach_task()` and `pool.wait_for()`: @@ -539,7 +579,7 @@ int main() Let us consider the following program: ```cpp -#include // std::cout, std::boolalpha +#include // std::boolalpha, std::cout class flag_class { @@ -568,11 +608,11 @@ int main() This program creates a new object `flag_object` of the class `flag_class`, sets the flag to `true` using the setter member function `set_flag()`, and then prints out the flag's value using the getter member function `get_flag()`. -What if we want to submit the member function `set_flag()` as a task to the thread pool? We simply wrap the entire statement `flag_object.set_flag(true);` from line in a lambda, and pass `flag_object` to the lambda by reference, as in this example: +What if we want to submit the member function `set_flag()` as a task to the thread pool? We can simply wrap the entire statement `flag_object.set_flag(true);` in a lambda, and pass `flag_object` to the lambda by reference, as in the following example: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool -#include // std::cout, std::boolalpha +#include // std::boolalpha, std::cout class flag_class { @@ -607,13 +647,13 @@ int main() Of course, this will also work with `detach_task()`, if we call `wait()` on the pool itself instead of on the returned future. -Note that in this example, instead of getting a future from `submit_task()` and then waiting for that future, we simply called `wait()` on that future straight away. This is a common way of waiting for a task to complete if we have nothing else to do in the meantime. Note also that we passed `flag_object` by reference to the lambda, since we want to set the flag on that same object, not a copy of it (passing by value wouldn't have worked anyway, since variables captured by value are implicitly `const`). +Note that in this example, instead of getting a future from `submit_task()` and then waiting for that future, we simply called `wait()` on that future straight away. This is a common way of waiting for a task to complete if we have nothing else to do in the meantime. Note also that we passed `flag_object` by reference to the lambda, since we want to set the flag on that same object, not a copy of it. Another thing you might want to do is call a member function from within the object itself, that is, from another member function. This follows a similar syntax, except that you must also capture `this` (i.e. a pointer to the current object) in the lambda. Here is an example: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool -#include // std::cout, std::boolalpha +#include // std::boolalpha, std::cout BS::thread_pool pool; @@ -652,17 +692,17 @@ int main() } ``` -Note that in this example we defined the thread pool as a global object, so that it is accessible outside the `main()` function. +Note that in this example we defined the thread pool as a global object, so that it is accessible outside the `main()` function. Although we could have, in theory, passed a reference to the thread pool in our call to `set_flag_to_true()`, that would be very cumbersome to do if multiple different functions need to use the same thread pool. Defining the thread pool as a global object is common practice, as it allows all functions to access the same thread pool without having to pass it around as an argument. ## Parallelizing loops ### Automatic parallelization of loops -One of the most common and effective methods of parallelization is splitting a loop into smaller loops and running them in parallel. It is most effective in "embarrassingly parallel" computations, such as vector or matrix operations, where each iteration of the loop is completely independent of every other iteration. +One of the most common and effective methods of parallelization is splitting a loop into smaller sub-loops and running them in parallel. It is most effective in "embarrassingly parallel" computations, such as vector or matrix operations, where each iteration of the loop is completely independent of every other iteration. For example, if we are summing up two vectors of 1000 elements each, and we have 10 threads, we could split the summation into 10 blocks of 100 elements each, and run all the blocks in parallel, potentially increasing performance by up to a factor of 10. -`BS::thread_pool` can automatically parallelize loops. To see how this works, consider the following generic loop: +`BS::thread_pool` can automatically parallelize loops, making it very easy to implement many parallel algorithms without having to worry about the details. To see how this works, consider the following generic loop: ```cpp for (T i = start; i < end; ++i) @@ -684,13 +724,11 @@ pool.submit_loop(start, end, loop, num_blocks); where: * `start` is the first index in the range. -* `end` is the index after the last index in the range, such that the full range is `[start, end)`. In other words, the loop will be equivalent to the one above if `start` and `end` are the same. - * `start` and `end` must both be of the same integer type `T`. See below for examples of what to do when they are not of the same type. - * Note that if `end <= start`, nothing will happen. -* `loop()` is the function that should run in every iteration of the loop, and takes one argument, the loop index. -* `num_blocks` is the number of blocks of the form `[a, b)` to split the loop into. For example, if the range is `[0, 9)` and there are 3 blocks, then the blocks will be the ranges `[0, 3)`, `[3, 6)`, and `[6, 9)`. - * The internal algorithm ensures that each of the blocks has one of two sizes, differing by 1, with the larger blocks always first, so that the tasks are as evenly distributed as possible. For example, if the range `[0, 100)` is split into 15 blocks, the result will be 10 blocks of size 7, which will be executed first, and 5 blocks of size 6. - * This argument can be omitted, in which case the number of blocks will be the number of threads in the pool. +* `end` is the index after the last index in the range, such that the full range is `[start, end)`. In other words, the loop will be equivalent to the generic loop above, but parallelized. Note that if `end <= start`, nothing will happen; the loop cannot go backwards. +* `loop()` is the function that should run in every iteration of the loop. It must take exactly one argument, the loop index. It cannot have a return value, as it will be executed multiple times by each task, so a return value would not make sense. +* `num_blocks` is the number of blocks of the form `[a, b)` to split the loop into. For example, if the range is `[0, 9)` and there are 3 blocks, then the blocks will be the ranges `[0, 3)`, `[3, 6)`, and `[6, 9)`. This argument can be omitted, in which case the number of blocks will be the number of threads in the pool. + +The thread pool's internal algorithm ensures that each of the blocks has one of two sizes, differing by 1, with the larger blocks always first, so that the tasks are as evenly distributed as possible, to optimize performance. For example, if the range `[0, 100)` is split into 15 blocks, the result will be 10 blocks of size 7, which will be submitted first, and 5 blocks of size 6. Each block will be submitted to the thread pool's queue as a separate task. Therefore, a loop that is split into 3 blocks will be split into 3 individual tasks, which may run in parallel. If there is only one block, then the entire loop will run as one task, and no parallelization will take place. @@ -701,23 +739,22 @@ BS::multi_future loop_future = pool.submit_loop(start, end, loop, num_bloc loop_future.wait(); ``` -`submit_loop()` returns an object of the helper class template [`BS::multi_future`](#more-about-bsmulti_futuret). This is essentially a specialization of `std::vector>` with additional member functions. Each of the `num_blocks` blocks will have an `std::future` assigned to it, and all these futures will be stored inside the returned `BS::multi_future`. When `loop_future.wait()` is called, the main thread will wait until all tasks generated by `submit_loop()` finish executing, and only those tasks - not any other tasks that also happen to be in the queue. This is essentially the role of the `BS::multi_future` class: to wait for a specific **group of tasks**, in this case the tasks running the loop blocks. - -What value should you use for `num_blocks`? Omitting this argument, so that the number of blocks will be equal to the number of threads in the pool, is typically a good choice. For best performance, it is recommended to do your own benchmarks to find the optimal number of blocks for each loop (you can use the [`BS::timer`](#measuring-execution-time-with-bstimer) utility class). Using fewer tasks than there are threads may be preferred if you are also running other tasks in parallel. Using more tasks than there are threads may improve performance in some cases, but parallelization with too many tasks will suffer from diminishing returns. +`submit_loop()` returns an object of the helper class [`BS::multi_future`](#more-about-bsmulti_future). This is essentially a specialization of `std::vector>` with additional member functions. Each of the `num_blocks` blocks will have an `std::future` assigned to it, and all these futures will be stored inside the returned `BS::multi_future`. When `loop_future.wait()` is called, the main thread will wait until **all** tasks generated by `submit_loop()` finish executing, and **only** those tasks - not any other tasks that also happen to be in the queue. This is essentially the role of the `BS::multi_future` class: to wait for a specific **group of tasks**, in this case the tasks running the loop blocks. As a simple example, the following code calculates and prints a table of squares of all integers from 0 to 99: ```cpp +#include // std::size_t #include // std::setw #include // std::cout int main() { - constexpr unsigned int max = 100; - unsigned int squares[max]; - for (unsigned int i = 0; i < max; ++i) + constexpr std::size_t max = 100; + std::size_t squares[max]; + for (std::size_t i = 0; i < max; ++i) squares[i] = i * i; - for (unsigned int i = 0; i < max; ++i) + for (std::size_t i = 0; i < max; ++i) std::cout << std::setw(2) << i << "^2 = " << std::setw(4) << squares[i] << ((i % 5 != 4) ? " | " : "\n"); } ``` @@ -725,55 +762,54 @@ int main() We can parallelize it as follows: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool +#include "BS_thread_pool.hpp" // BS::multi_future, BS::thread_pool +#include // std::size_t #include // std::setw #include // std::cout int main() { BS::thread_pool pool(10); - constexpr unsigned int max = 100; - unsigned int squares[max]; - const BS::multi_future loop_future = pool.submit_loop(0, max, - [&squares](const unsigned int i) + constexpr std::size_t max = 100; + std::size_t squares[max]; + const BS::multi_future loop_future = pool.submit_loop(0, max, + [&squares](const std::size_t i) { squares[i] = i * i; }); loop_future.wait(); - for (unsigned int i = 0; i < max; ++i) + for (std::size_t i = 0; i < max; ++i) std::cout << std::setw(2) << i << "^2 = " << std::setw(4) << squares[i] << ((i % 5 != 4) ? " | " : "\n"); } ``` Since there are 10 threads, and we omitted the `num_blocks` argument, the loop will be divided into 10 blocks, each calculating 10 squares. -Note that `submit_loop()` was executed with the explicit template parameter ``. The reason is that the two loop indices must be of the same type. However, here `max` is a `unsigned int`, while `0` is a (signed) `int`, so the types do not match, and the code will not compile unless we force the `0` to be of the right type. This can be done most elegantly by specifying the type of the indices explicitly using the template parameter. - -The reason this is not done automatically (e.g. using [`std::common_type`](https://en.cppreference.com/w/cpp/types/common_type) is that it may result in accidentally casting negative indices to an unsigned type, or integer indices to a too narrow integer type, which may lead to an incorrect loop range. - -We could also cast the `0` explicitly to unsigned int, but that doesn't look as nice: - -```cpp -pool.submit_loop(static_cast(0), max, /* ... */); -``` - -Or we could use a C-style cast: - -```cpp -pool.submit_loop((unsigned int)(0), max, /* ... */); -``` - -Or we could use an integer literal suffix: - -```cpp -pool.submit_loop(0U, max, ...); -``` - As a side note, notice that here we parallelized the calculation of the squares, but we did not parallelize printing the results. This is for two reasons: 1. We want to print out the squares in ascending order, and we have no guarantee that the blocks will be executed in the correct order. This is very important; you must never expect that the parallelized loop will execute at the same order as the non-parallelized loop. 2. If we did print out the squares from within the parallel tasks, we would get a huge mess, since all 10 blocks would print to the standard output at once. [Later](#synchronizing-printing-to-a-stream-with-bssynced_stream) we will see how to synchronize printing to a stream from multiple tasks at the same time. +### Optimizing the number of blocks + +The most important factor to consider when parallelizing loops is the number of blocks `num_blocks` to split the loop into. Naively, it may seem that the number of blocks should simply be equal to the number of threads in the pool, but that is usually **not** the optimal choice. Inevitably, some blocks will finish before other blocks; if there is only one block per thread, then any threads that have already finished executing their blocks will remain idle until the rest of the blocks are done, wasting many CPU cycles. + +It is therefore generally better to use a larger number of blocks than the number of threads, to ensure that all threads work at maximum capacity. On the other hand, parallelization with too many blocks will eventually suffer from diminishing returns due to increased overhead. A good rule of thumb is to use a number of blocks equal to the square of the number of threads, but this is not necessarily the optimal number in all cases. + +In the end, the optimal number of blocks will always depend on the specific algorithm being parallelized and the total number of indices in the loop, and may differ between different compilers, operating systems, and hardware configurations. For best performance, it is strongly recommended to do your own benchmarks to find the optimal number of blocks for your particular use case; see the [benchmarks code in the bundled test program](#performance-tests) for an example of how to do this. + +Finally, note that the discussion here only pertains to situations where the parallelized loop is the only thing running in the pool. If there are many other tasks running in parallel from other sources, then you probably do not need to worry about idle time, since the threads will be kept busy by the other tasks anyway. + +### Common index types + +Let us now consider a subtlety regarding the types of the start and end indices. In the example [above](#automatic-parallelization-of-loops), the start index is `0`, which is of type `int`, while the end index is `max`, which is of type `std::size_t`. These two types are not compatible, as they are both of different signedness and (on a 64-bit system) of different bit width. In such cases, `submit_loop()` uses a custom type trait `BS::common_index_type` to determine the common type of the indices. + +The common index type of two signed integers or two unsigned integers is the larger of the integers, while the common index type of a signed and an unsigned integer is a signed integer that can hold the full ranges of both integers. (This is in contrast to [`std::common_type`](https://en.cppreference.com/w/cpp/types/common_type), which would choose the unsigned integer in the latter case, causing a loop with a negative start index and an unsigned end index to fail due to integer overflow.) + +The exception to this rule is when one of the integers is a 64-bit unsigned integer, and the other is a signed integer (of any bit width), since there is no fundamental signed type that can hold the full ranges of both integers. In this case, we choose a 64-bit unsigned integer as the common index type, since the most common scenario where this might happen is when the indices go from `0` to an index of type `std::size_t` - as in our example in the previous section. + +However, it is important to note that this will fail if the first index is in fact negative. Therefore, **only** in the edge case where one index is a negative integer and the other is of an unsigned 64-bit integer type such as `std::size_t`, the user must cast both indices explicitly to the desired common type. In all other cases, this is handled automatically behind the scenes using `BS::common_index_type`. + ### Parallelizing loops without futures Just as in the case of [`detach_task()`](#detaching-and-waiting-for-tasks) vs. [`submit_task()`](#submitting-tasks-with-no-arguments-and-receiving-a-future), sometimes you may want to parallelize a loop, but you don't need it to return a `BS::multi_future`. In this case, you can save the overhead of generating the futures (which can be significant, depending on the number of blocks) by using `detach_loop()` instead of `submit_loop()`, with the same arguments. @@ -782,26 +818,27 @@ For example, we could detach the loop of squares example above as follows: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool +#include // std::size_t #include // std::setw #include // std::cout int main() { BS::thread_pool pool(10); - constexpr unsigned int max = 100; - unsigned int squares[max]; - pool.detach_loop(0, max, - [&squares](const unsigned int i) + constexpr std::size_t max = 100; + std::size_t squares[max]; + pool.detach_loop(0, max, + [&squares](const std::size_t i) { squares[i] = i * i; }); pool.wait(); - for (unsigned int i = 0; i < max; ++i) + for (std::size_t i = 0; i < max; ++i) std::cout << std::setw(2) << i << "^2 = " << std::setw(4) << squares[i] << ((i % 5 != 4) ? " | " : "\n"); } ``` -**Warning:** Since `detach_loop()` does not return a `BS::multi_future`, there is no built-in way for the user to know when the loop finishes executing. You must use either [`wait()`](#detaching-and-waiting-for-tasks) as we did here, or some other method such as condition variables, to ensure that the loop finishes executing before trying to use anything that depends on its output. Otherwise, bad things will happen! +**Warning:** Since `detach_loop()` does not return a `BS::multi_future`, there is no built-in way for the user to know when the loop finishes executing. You must use either [`wait()`](#detaching-and-waiting-for-tasks) as we did here, or some other method such as condition variables, to ensure that the loop finishes executing before trying to use anything that depends on its output. Otherwise, bad things will happen! If the loop is the only thing running in the pool, then generally `detach_loop()` followed by `wait()` is the optimal choice in terms of performance. ### Parallelizing individual indices vs. blocks @@ -814,48 +851,49 @@ for (T i = start; i < end; ++i) The `start` and `end` indices of each block are determined automatically by the pool. For example, in the previous section, the loop from 0 to 100 was split into 10 blocks of 10 indices each: `start = 0` to `end = 10`, `start = 10` to `end = 20`, and so on; the blocks are not inclusive of the last index, since the `for` loop has the condition `i < end` and not `i <= end`. -However, this also means that the `loop()` function is executed multiple times per block. This generates additional overhead due to the multiple function calls. For short loops, this should not affect performance. However, for very long loops, with millions of indices, the performance cost may be significate. +However, this also means that the `loop()` function is executed multiple times per block. This generates additional overhead due to the multiple function calls. For short loops, this should not affect performance. However, for very long loops, with millions of indices, the performance cost may be significant. -For this reason, the thread pool library provides two additional member functions for parallelizing loops: `detach_blocks()` and `submit_blocks()`. While `detach_loop()` and `submit_loop()` execute a function `loop(i)` once per index but multiple times per block, `detach_blocks()` and `submit_blocks()` execute a function `block(start, end)` once per block. +For this reason, the thread pool library provides two additional member functions for parallelizing loops: `detach_blocks()` and `submit_blocks()`. While `detach_loop()` and `submit_loop()` execute a function `loop(i)` once per index but multiple times per block, `detach_blocks()` and `submit_blocks()` execute a function `block(start, end)` only once per block. -The main advantage of this method is increased performance, but the main disadvantage is slightly more complicated code. In particular, the user must define the loop from `start` to `end` manually within each block. Here is the previous example using `detach_blocks()`: +The main advantage of this method is increased performance, but the main disadvantage is slightly more complicated code. In particular, the user must define the loop from `start` to `end` manually within each block, ensuring that all the indices in the block are handled. Here is the previous example again, this time using `detach_blocks()`: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool +#include // std::size_t #include // std::setw #include // std::cout int main() { BS::thread_pool pool(10); - constexpr unsigned int max = 100; - unsigned int squares[max]; - pool.detach_blocks(0, max, - [&squares](const unsigned int start, const unsigned int end) + constexpr std::size_t max = 100; + std::size_t squares[max]; + pool.detach_blocks(0, max, + [&squares](const std::size_t start, const std::size_t end) { - for (unsigned int i = start; i < end; ++i) + for (std::size_t i = start; i < end; ++i) squares[i] = i * i; }); pool.wait(); - for (unsigned int i = 0; i < max; ++i) + for (std::size_t i = 0; i < max; ++i) std::cout << std::setw(2) << i << "^2 = " << std::setw(4) << squares[i] << ((i % 5 != 4) ? " | " : "\n"); } ``` -Note how the block function takes two arguments, and includes the internal loop. +Note how the block function takes two arguments, and includes the internal loop. Also, since we are using `detach_blocks()`, we must wait for the loop to finish executing using `wait()`. Alternatively, we could have used `submit_blocks()` and waited on the returned `BS::multi_future` object. -Generally, compiler optimizations should be able to make `detach_loop()` and `submit_loop()` perform roughly the same as `detach_blocks()` and `submit_blocks()`. However, you should perform your own benchmarks to see which option works best for your particular use case. +Generally, compiler optimizations should be able to make `detach_loop()` and `submit_loop()` perform roughly the same as `detach_blocks()` and `submit_blocks()`. However, `detach_blocks()` and `submit_blocks()` are always going to be inherently faster, at the cost of being slightly more complicated to use. In addition, having low-level control of each block can allow for further optimizations, such as allocating resources per block instead of per index. As usual, you should perform your own benchmarks to see which option works best for your particular use case. ### Loops with return values -Unlike `submit_task()`, the member function `submit_loop()` only takes loop functions with no return values. The reason is that it wouldn't make sense to return a future for every single index of the loop. However, `submit_blocks()` does allow the block function to have a return value, as the number of blocks will generally not be too large, unlike the number of indices. +As mentioned above, unlike `submit_task()`, the member function `submit_loop()` only takes loop functions with no return value. The reason is that each block is running the loop function multiple times, so a return value would not make sense. In contrast, `submit_blocks()` allows the block function to have a return value, as each block can return a unique value. -The block function will be executed once for each block, but the blocks are managed by the thread pool, with the user only able to select the number of blocks, but not the range of each block. Therefore, there is limited usability in returning one value per block. However, for cases where this is desired, such as for summation or some sorting algorithms, `submit_blocks()` does accept functions with return values, in which case it returns a `BS::multi_future` object where `T` is the type of the return values. +The block function will be executed once for each block, but the blocks are managed by the thread pool, with the user only able to select the number of blocks, but not the range of each block. Therefore, there is limited usability in returning one value per block. However, for cases where this is desired, such as for summation or some sorting algorithms, `submit_blocks()` does accept functions with return values, in which case it returns a `BS::multi_future` object where `T` is the type of the return value. Here's an example of a function template summing all elements of type `T` in a given range: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool +#include "BS_thread_pool.hpp" // BS::multi_future, BS::thread_pool #include // std::uint64_t #include // std::future #include // std::cout @@ -865,7 +903,7 @@ BS::thread_pool pool; template T sum(T min, T max) { - BS::multi_future loop_future = pool.submit_blocks( + BS::multi_future loop_future = pool.submit_blocks( min, max + 1, [](const T start, const T end) { @@ -887,38 +925,67 @@ int main() } ``` +Note that we needed to specify the type `T` explicitly as `std::uint64_t`, that is, an unsigned 64-bit integer, as the result, 500,000,500,000, would not fit in a 32-bit integer. + Here we used the fact that `BS::multi_future` is a specialization of `std::vector>`, so we can use a range-based `for` loop to iterate over the futures, and use the `get()` member function of each future to get its value. The values of the futures will be the partial sums from each block, so when we add them up, we will get the total sum. Note that we divided the loop into 100 blocks, so there will be 100 futures in total, each with the partial sum of 10,000 numbers. The range-based `for` loop will likely start before the loop finished executing, and each time it calls a future, it will get the value of that future if it is ready, or it will wait until the future is ready and then get the value. This increases performance, since we can start summing the results without waiting for the entire loop to finish executing first - we only need to wait for individual blocks. -If we did want to wait until the entire loop finishes before summing the results, we could have used the `get()` member function of the `BS::multi_future` object itself, which returns an `std::vector` with the values obtained from each future. In that case, the sum could be obtained after calling `submit_blocks()` as follows: +If we did want to wait until the entire loop finishes before summing the results, we could have used the `get()` member function of the `BS::multi_future` object itself, which returns an `std::vector` with the values obtained from each future. In that case, the sum could be obtained after calling `submit_blocks()`, for example using `std::reduce`, as follows: ```cpp -std::vector partial_sums = loop_future.get(); -T result = std::reduce(partial_sums.begin(), partial_sums.end()); -return result; +#include "BS_thread_pool.hpp" // BS::multi_future, BS::thread_pool +#include // std::uint64_t +#include // std::cout +#include // std::reduce +#include // std::vector + +BS::thread_pool pool; + +template +T sum(T min, T max) +{ + BS::multi_future loop_future = pool.submit_blocks( + min, max + 1, + [](const T start, const T end) + { + T block_total = 0; + for (T i = start; i < end; ++i) + block_total += i; + return block_total; + }, + 100); + std::vector partial_sums = loop_future.get(); + T result = std::reduce(partial_sums.begin(), partial_sums.end()); + return result; +} + +int main() +{ + std::cout << sum(1, 1'000'000); +} ``` ### Parallelizing sequences -The member functions `detach_loop()`, `submit_loop()`, `detach_blocks()`, and `submit_blocks()` parallelize a loop by splitting it into blocks, and submitting each block as an individual task to the queue, with each such task iterating over all the indices in the corresponding block's range, which can be numerous. However, sometimes we have loops with few indices, or more generally, a sequence of tasks enumerated by some index. In such cases, we can avoid the overhead of splitting into blocks and simply submit each individual index as its own independent task to the pool's queue. +The member functions `detach_loop()`, `submit_loop()`, `detach_blocks()`, and `submit_blocks()` parallelize a loop by splitting it into blocks, and submitting each block as an individual task to the queue, with each such task iterating over all the indices in the corresponding block's range, which can be numerous. However, sometimes we have a loop with a small number of indices, or more generally, a sequence of tasks enumerated by some index. In such cases, we can avoid the overhead of splitting into blocks and simply submit each individual index as its own independent task to the pool's queue. -This can be done with `detach_sequence()` and `submit_sequence()`. The syntax of these functions is similar to `detach_loop()` and `submit_loop()`, except that they don't have the `num_blocks` argument at the end. The sequence function must take only one argument, the index. As usual, `detach_sequence()` detaches the tasks and does not return a future, while `submit_sequence()` returns a `BS::multi_future`. If the tasks in the sequence return values, then the futures will contain those values, otherwise they will be `void` futures. +This can be done with `detach_sequence()` and `submit_sequence()`. The syntax of these functions is similar to `detach_loop()` and `submit_loop()`, except that they don't have the `num_blocks` argument at the end. The sequence function must take only one argument, the index. -Here is a simple example: +As usual, `detach_sequence()` detaches the tasks and does not return a future, so you must use `wait()` if you need to wait for the entire sequence to finish executing, while `submit_sequence()` returns a `BS::multi_future`. If the tasks in the sequence return values, then the futures will contain those values, otherwise they will be `void` futures. + +Here is a simple example, where each task in the sequence calculates the factorial of its index: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool +#include "BS_thread_pool.hpp" // BS::multi_future, BS::thread_pool #include // std::uint64_t #include // std::cout #include // std::vector -using ui64 = std::uint64_t; - -ui64 factorial(const ui64 n) +std::uint64_t factorial(const std::uint64_t n) { - ui64 result = 1; - for (ui64 i = 2; i <= n; ++i) + std::uint64_t result = 1; + for (std::uint64_t i = 2; i <= n; ++i) result *= i; return result; } @@ -926,20 +993,24 @@ ui64 factorial(const ui64 n) int main() { BS::thread_pool pool; - constexpr ui64 max = 20; - BS::multi_future sequence_future = pool.submit_sequence(0, max + 1, factorial); - std::vector factorials = sequence_future.get(); - for (ui64 i = 0; i < max + 1; ++i) + constexpr std::uint64_t max = 20; + BS::multi_future sequence_future = pool.submit_sequence(0, max + 1, factorial); + std::vector factorials = sequence_future.get(); + for (std::uint64_t i = 0; i < max + 1; ++i) std::cout << i << "! = " << factorials[i] << '\n'; } ``` -### More about `BS::multi_future` +Note how the factorials of each index are stored in the `BS::multi_future`, and can be obtained as a vector using `get()`; each element of the vector is equal to the factorial of the element's index, calculated by its own individual task in the sequence. -The helper class template `BS::multi_future`, which we have been using throughout this section, provides a convenient way to collect and access groups of futures. This class is a specialization of `std::vector`, so it should be used in a similar way: +**Warning:** Since each index in the sequence will be submitted as a separate task, `detach_sequence()` and `submit_sequence()` should only be used if the number of indices is small (say, within 1-2 orders of magnitude of the number of threads), and each index performs a substantial computation on its own. If you submit a sequence of 1 million indices, each performing a 1 ms calculation, the overhead of submitting each index as a separate task would far outweigh the benefits of parallelization. -* When you create a new object, either use the default constructor to create an empty object and add futures to it later, or pass the desired number of futures to the constructor in advance. -* Use the `[]` operator to access the future at a specific index, or the `push_back()` member function to append a new future to the list. +### More about `BS::multi_future` + +The helper class `BS::multi_future`, which we have been using throughout this section, provides a convenient way to collect and access groups of futures. While a `BS::multi_future` object is created automatically by the pool when parallelizing loops, you can also use it to store futures manually, such as those obtained from `submit_task()` or by other means. `BS::multi_future` is a specialization of `std::vector>`, so it should be used in a similar way: + +* When you create a new `BS::multi_future` object, either use the default constructor to create an empty object and add futures to it later, or pass the desired number of futures to the constructor in advance. +* Use the `[]` operator to access the future at a specific index, or the `push_back()` member function to append a new future to the list. (If the number of futures is known in advance, you should use `reserve()` to allocate memory for all of them first, and only then `push_back()` the individual futures, otherwise memory will have to be reallocated multiple times, which is very inefficient.) * The `size()` member function tells you how many futures are currently stored in the object. However, `BS::multi_future` also has additional member functions that are aimed specifically at handling futures: @@ -953,17 +1024,9 @@ Aside from using `BS::multi_future` to track the execution of parallelized lo ## 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. - -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` -When printing to an output stream from multiple threads in parallel, the output may become garbled. For example, consider this code: +When printing to an output stream from multiple threads in parallel, the output may become garbled. For example, try running this code: ```cpp #include "BS_thread_pool.hpp" // BS::thread_pool @@ -973,11 +1036,12 @@ BS::thread_pool pool; int main() { - pool.detach_sequence(0, 5, - [](int i) - { - std::cout << "Task no. " << i << " executing.\n"; - }); + pool.submit_sequence(0, 5, + [](const unsigned int i) + { + std::cout << "Task no. " << i << " executing.\n"; + }) + .wait(); } ``` @@ -993,7 +1057,7 @@ Task no. 2 executing. The reason is that, although each **individual** insertion to `std::cout` is thread-safe, there is no mechanism in place to ensure subsequent insertions from the same thread are printed contiguously. -The utility class `BS::synced_stream` is designed to eliminate such synchronization issues. The constructor takes one optional argument, specifying the output stream to print to. If no argument is supplied, `std::cout` will be used: +The thread pool utility class `BS::synced_stream` is designed to eliminate such synchronization issues. The stream to print to should be passed as a constructor argument. If no argument is supplied, `std::cout` will be used: ```cpp // Construct a synced stream that will print to std::cout. @@ -1007,19 +1071,19 @@ The member function `print()` takes an arbitrary number of arguments, which are As an example, this code: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool BS::synced_stream sync_out; BS::thread_pool pool; int main() { - pool.detach_sequence(0, 5, - [](int i) - { - sync_out.println("Task no. ", i, " executing."); - }); + pool.submit_sequence(0, 5, + [](const unsigned int i) + { + sync_out.println("Task no. ", i, " executing."); + }) + .wait(); } ``` @@ -1035,16 +1099,15 @@ Task no. 4 executing. **Warning:** Always create the `BS::synced_stream` object **before** the `BS::thread_pool` object, as we did in this example. When the `BS::thread_pool` object goes out of scope, it waits for the remaining tasks to be executed. If the `BS::synced_stream` object goes out of scope before the `BS::thread_pool` object, then any tasks using the `BS::synced_stream` will crash. Since objects are destructed in the opposite order of construction, creating the `BS::synced_stream` object before the `BS::thread_pool` object ensures that the `BS::synced_stream` is always available to the tasks, even while the pool is destructing. -Most stream manipulators defined in the headers `` and ``, such as `std::setw` (set the character width of the next output), `std::setprecision` (set the precision of floating point numbers), and `std::fixed` (display floating point numbers with a fixed number of digits), can be passed to `print()` and `println()` just as you would pass them to a stream. +Most stream manipulators defined in the headers `` and ``, such as `std::setw` (set the character width of the next output), `std::setprecision` (set the precision of floating point numbers), and `std::fixed` (display floating point numbers with a fixed number of digits), can be passed as arguments to `print()` and `println()`, and will have the same effect as inserting them to the associated stream. The only exceptions are the flushing manipulators `std::endl` and `std::flush`, which will not work because the compiler will not be able to figure out which template specializations to use. Instead, use `BS::synced_stream::endl` and `BS::synced_stream::flush`. Here is an example: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::sqrt -#include // std::setprecision, std::setw -#include // std::fixed +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::sqrt +#include // std::setprecision, std::setw +#include // std::fixed BS::synced_stream sync_out; BS::thread_pool pool; @@ -1052,69 +1115,96 @@ BS::thread_pool pool; int main() { sync_out.print(std::setprecision(10), std::fixed); - pool.detach_sequence(0, 16, - [](int i) - { - sync_out.print("The square root of ", std::setw(2), i, " is ", std::sqrt(i), ".", BS::synced_stream::endl); - }); + pool.submit_sequence(0, 16, + [](const unsigned int i) + { + sync_out.print("The square root of ", std::setw(2), i, " is ", std::sqrt(i), ".", BS::synced_stream::endl); + }) + .wait(); } ``` -Note, however, that `BS::synced_stream::endl` should only be used if flushing is desired; otherwise, a newline character should be used instead. +Note, however, that `BS::synced_stream::endl` should only be used if flushing is desired; otherwise, a newline character should be used instead. As with `std::endl`, using `BS::synced_stream::endl` too often will cause a performance hit, as it will force the stream to flush the buffer every time it is called. -### Measuring execution time with `BS::timer` - -If you are using a thread pool, then your code is most likely performance-critical. Achieving maximum performance requires performing a considerable amount of benchmarking to determine the optimal settings and algorithms. Therefore, it is important to be able to measure the execution time of various computations and operations under different conditions. - -The utility class `BS::timer` provides a simple way to measure execution time. It is very straightforward to use: - -1. Create a new `BS::timer` object. -2. Immediately before you execute the computation that you want to time, call the `start()` member function. -3. Immediately after the computation ends, call the `stop()` member function. -4. Use the member function `ms()` to obtain the elapsed time for the computation in milliseconds. -5. Alternatively, use the member function `current_ms()` to obtain the elapsed time so far but keep the timer ticking. - -For example: +If desired, `BS::synced_stream` can also synchronize printing into more than one stream at a time. To facilitate this, we can pass a list of output streams to the constructor. For example, the following program will print the same output to both `std::cout` and a log file: ```cpp -BS::timer tmr; -tmr.start(); -do_something(); -tmr.stop(); -std::cout << "The elapsed time was " << tmr.ms() << " ms.\n"; +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::ofstream +#include // std::cout + +BS::thread_pool pool; + +int main() +{ + std::ofstream log_file("task.log"); + BS::synced_stream sync_out(std::cout, log_file); + pool.submit_sequence(0, 5, + [&sync_out](const unsigned int i) + { + sync_out.println("Task no. ", i, " executing."); + }) + .wait(); +} ``` -A practical application of the `BS::timer` class can be found in the benchmark portion of the test program `BS_thread_pool_test.cpp`. +Note that we must wait on the future before the `main()` function ends, as otherwise the log file may be destructed before the tasks finish executing. If we used `detach_sequence()`, which does not return a future, we would have to call `pool.wait()` in the last line. -### Sending simple signals between threads with `BS::signaller` +In this example we did not create the `BS::synced_stream` as a global object, since we wanted to pass the log file as a stream to the constructor. However, it is also possible to add streams to or remove streams from an existing `BS::synced_stream` object using the member functions `add_stream()` and `remove_stream()`. For example, in the following program, we create a `BS::synced_stream` global object with the default constructor, so that it prints to `std::cout`, but then we change out minds, remove `std::cout` from the list of streams, and add a log file instead: -`BS::signaller` is a utility class which can be used to allow simple signalling between threads. To use it, construct an object and then pass it to the different threads. Multiple threads can call the `wait()` member function of the signaller. When another thread calls the `ready()` member function, the waiting threads will stop waiting. +```cpp +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::ofstream +#include // std::cout -That's really all there is to it; `BS::signaller` is really just a convenient wrapper around [`std::promise`](https://en.cppreference.com/w/cpp/thread/promise), which contains both the promise and its future. For usage examples, please see the test program `BS_thread_pool_test.cpp`. +BS::synced_stream sync_out; +BS::thread_pool pool; + +int main() +{ + std::ofstream log_file("task.log"); + sync_out.remove_stream(std::cout); + sync_out.add_stream(log_file); + pool.submit_sequence(0, 5, + [](const unsigned int i) + { + sync_out.println("Task no. ", i, " executing."); + }) + .wait(); +} +``` + +It is common practice to create a global `BS::synced_stream` object, so that it can be accessed from anywhere in the program, without having to pass it to every function that might want to print something to the stream. However, if you also have a global `BS::thread_pool` object, you must always make sure to define the global `BS::synced_stream` object **before** the global `BS::thread_pool` object, for the reasons explained in the warning above. + +Internally, `BS::synced_stream` keeps the streams in an `std::vector`. The order in which the streams are added is also the order in which they will be printed to. For more precise control, you can use the member function `get_streams()` to get a reference to this vector, and manipulate it directly as you see fit. + +### Synchronizing tasks with `BS::counting_semaphore` and `BS::binary_semaphore` + +The thread pool library provides two utility classes, `BS::counting_semaphore` and `BS::binary_semaphore`, which offer versatile synchronization primitives that can be used to synchronize tasks in a variety of ways. These classes are equivalent to the C++20 `std::counting_semaphore` and `std::binary_semaphore`, respectively, but are offered in the library as convenience polyfills for projects based on C++17. If C++20 features are available, the polyfills are not used, and instead are just aliases for the standard library classes. + +Since `BS::counting_semaphore` and `BS::binary_semaphore` are identical in functionality to their standard library counterparts, we will not explain how to use them here. Instead, the user is referred to [cppreference.com](https://en.cppreference.com/w/cpp/thread/counting_semaphore). ## Managing tasks ### Monitoring the tasks -Sometimes you may wish to monitor what is happening with the tasks you submitted to the pool. This may be done using three member functions: +Sometimes you may wish to monitor what is happening with the tasks you submitted to the pool. This may be done using these three member functions: -* `get_tasks_queued()` gets the number of tasks currently waiting in the queue to be executed by the threads. +* `get_tasks_queued()` gets the number of tasks currently waiting in the queue to be executed. * `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_total() == get_tasks_queued() + get_tasks_running()`. +* `get_tasks_total()` gets the total number of unfinished tasks: either still in the queue, or being executed by a thread. -These functions are demonstrated in the following program: +Note that `get_tasks_total() == get_tasks_queued() + get_tasks_running()`. These functions are demonstrated in the following program: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::chrono -#include // std::this_thread +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::chrono +#include // std::this_thread BS::synced_stream sync_out; BS::thread_pool pool(4); -void sleep_half_second(const int i) +void sleep_half_second(const unsigned int i) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); sync_out.println("Task ", i, " done."); @@ -1136,6 +1226,7 @@ int main() monitor_tasks(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); monitor_tasks(); + pool.wait(); } ``` @@ -1160,38 +1251,34 @@ Task 11 done. 0 tasks total, 0 tasks running, 0 tasks queued. ``` -The reason we called `pool.wait()` in the beginning is that when the thread pool is created, an initialization task runs in each thread, so if we don't wait, the first line will say there are 16 tasks in total, including the 4 initialization tasks. See [below](#thread-pool-initialization-functions) for more details. +The reason we called `pool.wait()` in the beginning is that when the thread pool is created, an initialization task runs in each thread, so if we don't wait, the first line would say there are 16 tasks in total, including the 4 initialization tasks. See [below](#thread-initialization-functions) for more details. Of course, we also called `pool.wait()` at the end to ensure that all tasks have finished executing before the program ends. ### Purging tasks Consider a situation where the user cancels a multithreaded operation while it is still ongoing. Perhaps the operation was split into multiple tasks, and half of the tasks are currently being executed by the pool's threads, but the other half are still waiting in the queue. -The thread pool cannot terminate the tasks that are already running, as the C++17 standard does not provide that functionality (and in any case, abruptly terminating a task while it's running could have extremely bad consequences, such as memory leaks and data corruption). However, the tasks that are still waiting in the queue can be purged using the `purge()` member function. +The thread pool cannot terminate the tasks that are already running, as C++ does not provide that functionality (and in any case, abruptly terminating a task while it's running could have extremely bad consequences, such as memory leaks and data corruption). However, the tasks that are still waiting in the queue can be purged using the `purge()` member function. -Once `purge()` is called, any tasks still waiting in the queue will be discarded, and will never be executed by the threads. Please note that there is no way to restore the purged tasks; they are gone forever. +Once `purge()` is called, any tasks still waiting in the queue will be discarded, and will never be executed by the threads. Please note that there is no way to restore the purged tasks; they are gone forever! Consider for example the following program: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::chrono -#include // std::this_thread +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::chrono +#include // std::this_thread BS::synced_stream sync_out; BS::thread_pool pool(4); int main() { - for (size_t i = 0; i < 8; ++i) - { - pool.detach_task( - [i] - { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - sync_out.println("Task ", i, " done."); - }); - } + pool.detach_sequence(0, 8, + [](const unsigned int i) + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sync_out.println("Task ", i, " done."); + }); std::this_thread::sleep_for(std::chrono::milliseconds(50)); pool.purge(); pool.wait(); @@ -1207,12 +1294,45 @@ Task 2 done. Task 3 done. ``` +Please note that, as explained above, the thread pool cannot terminate running tasks on its own. If you need to do that, you must incorporate a mechanism into the task itself that will terminate the task safely. For example, you could create an atomic flag that the task checks periodically, terminating itself if the flag is set. Here is a simple example: + +```cpp +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::chrono +#include // std::this_thread + +BS::synced_stream sync_out; +BS::thread_pool pool(4); + +int main() +{ + std::atomic stop_flag = false; + pool.detach_sequence(0, 8, + [&stop_flag](const unsigned int i) + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + if (stop_flag) + return; + sync_out.println("Task ", i, " done."); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + stop_flag = true; + pool.purge(); + pool.wait(); +} +``` + +This program will not print out any output, as the tasks will terminate themselves prematurely when `stop_flag` is set to `true`. In this case, we did not have to call `purge()`, but by doing so we prevented the other 4 tasks from being executed for no reason. + ### Exception handling `submit_task()` catches any exceptions thrown by the submitted task and forwards them to the corresponding future. They can then be caught when invoking the `get()` member function of the future. For example: ```cpp -#include "BS_thread_pool.hpp" +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::exception +#include // std::future +#include // std::runtime_error BS::synced_stream sync_out; BS::thread_pool pool; @@ -1221,14 +1341,17 @@ double inverse(const double x) { if (x == 0) throw std::runtime_error("Division by zero!"); - else - return 1 / x; + return 1 / x; } int main() { constexpr double num = 0; - std::future my_future = pool.submit_task(inverse, num); + std::future my_future = pool.submit_task( + [num] + { + return inverse(num); + }); try { const double result = my_future.get(); @@ -1252,7 +1375,10 @@ However, if you change `num` to any non-zero number, no exceptions will be throw It is important to note that `wait()` does not throw any exceptions; only `get()` does. Therefore, even if your task does not return anything, i.e. your future is an `std::future`, you must still use `get()` on the future obtained from it if you want to catch exceptions thrown by it. Here is an example: ```cpp -#include "BS_thread_pool.hpp" +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::exception +#include // std::future +#include // std::runtime_error BS::synced_stream sync_out; BS::thread_pool pool; @@ -1261,14 +1387,17 @@ void print_inverse(const double x) { if (x == 0) throw std::runtime_error("Division by zero!"); - else - sync_out.println("The inverse of ", x, " is ", 1 / x, "."); + sync_out.println("The inverse of ", x, " is ", 1 / x, "."); } int main() { constexpr double num = 0; - std::future my_future = pool.submit_task(print_inverse, num); + std::future my_future = pool.submit_task( + [num] + { + print_inverse(num); + }); try { my_future.get(); @@ -1280,32 +1409,164 @@ int main() } ``` -When using `BS::multi_future` to handle multiple futures at once, exception handling works the same way: if any of the futures may throw exceptions, you may catch these exceptions when calling `get()`, even in the case of `BS::multi_future`. +When using `BS::multi_future` to handle multiple futures at once, exception handling works the same way: if any of the futures may throw exceptions, you may catch these exceptions when calling `get()`, even in the case of `BS::multi_future`. -If you do not require exception handling, or if exceptions are explicitly disabled in your codebase, you can define the macro `BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING` before including `BS_thread_pool.hpp`, which will disable exception handling in `submit_task()`. Note that if the feature-test macro `__cpp_exceptions` is undefined, `BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING` will be automatically defined. +Note that if you use `detach_task()`, or any other `detach` member function, there is no way to catch exceptions thrown by the task, as a future will not be returned. In such cases, all exceptions thrown by the task will be silently ignored, to prevent program termination. If you need to catch exceptions in a detached task, you must do so within the task itself, as in this example: -### Getting information about the threads +```cpp +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::exception +#include // std::runtime_error -`BS::thread_pool` comes with a variety of methods to obtain information about the threads in the pool: +BS::synced_stream sync_out; +BS::thread_pool pool; -1. The namespace `BS::this_thread` provides functionality similar to `std::this_thread`. If the current thread belongs to a `BS::thread_pool` object, then `BS::this_thread::get_index()` can be used to get the index of the current thread, and `BS::this_thread::get_pool()` can be used to get the pointer to the thread pool that owns the current thread. Please see [the reference below](#the-bsthis_thread-namespace) for more details. -2. The member function `get_thread_ids()` returns a vector containing the unique identifiers for each of the pool's threads, as obtained by `std::thread::get_id()`. These values are not so useful on their own, but can be used for whatever the user wants to use them for. -3. The optional member function `get_native_handles()`, if enabled, returns a vector containing the underlying implementation-defined thread handles for each of the pool's threads, as obtained by `std::thread::native_handle()`. For more information, see [the relevant section below](#accessing-native-thread-handles). +double inverse(const double x) +{ + if (x == 0) + throw std::runtime_error("Division by zero!"); + return 1 / x; +} -### Thread pool initialization functions +int main() +{ + constexpr double num = 0; + pool.detach_task( + [num] + { + try + { + const double result = inverse(num); + sync_out.println("The inverse of ", num, " is ", result, "."); + } + catch (const std::exception& e) + { + sync_out.println("Caught exception: ", e.what()); + } + }); + pool.wait(); +} +``` -Sometimes, it is necessary to initialize the threads before they run any tasks. This can be done by submitting a proper initialization function to the constructor or to `reset()`, either as the only argument or as the second argument after the desired number of threads. The thread initialization must take no arguments and have no return value. However, if needed, the function can use `BS::this_thread::get_index()` and `BS::this_thread::get_pool()` to figure out which thread and pool it belongs to. +If exceptions are explicitly disabled in your codebase, or if the feature-test macro `__cpp_exceptions` is undefined for any other reason, exception handling will be automatically disabled in the thread pool. -The thread initialization function is submitted as a set of special tasks, one per thread, which bypass the queue, but still count towards the number of running tasks, which means `get_tasks_total()` and `get_tasks_running()` will report that these tasks are running if they are checked immediately after the pool is initialized. +### Getting information about the current thread -This is done so that the user has the option to either wait for the initialization tasks to finish, by calling `wait()` on the pool, or just keep going. In either case, the initialization tasks will **always** finish executing before any tasks are picked out of the queue, so there is no reason to wait for them to finish unless they have some side-effects that affect the main thread. +The class `BS::this_thread` provides functionality analogous to `std::this_thread`, that is, it allows a thread to reference itself. It contains the following static member functions: + +* `BS::this_thread::get_index()` can be used to get the index of the current thread as an `std::optional` object. + * If this thread belongs to a `BS::thread_pool` object, the return value will be an index in the range `[0, N)` where `N == BS::thread_pool::get_thread_count()`. + * Otherwise, for example if this thread is the main thread or an independent thread not in any pools, `std::nullopt` will be returned. +* `BS::this_thread::get_pool()` can be used to get a pointer to the thread pool that owns the current thread as an `std::optional` object. + * If this thread belongs to a `BS::thread_pool` object, the return value will be a `void` pointer to that object. + * Otherwise, `std::nullopt` will be returned. + +An [`std::optional`](https://en.cppreference.com/w/cpp/utility/optional) is an object that may or may not have a value. [`std::nullopt`](https://en.cppreference.com/w/cpp/utility/optional/nullopt) is a placeholder which indicates that the object does not have a value. To access an `std::optional`, you should first use [`std::optional::has_value()`](https://en.cppreference.com/w/cpp/utility/optional/operator_bool) to check if it contains a value, and if so, use [`std::optional::value()`](https://en.cppreference.com/w/cpp/utility/optional/value) to obtain that value. A shortcut for `if (x.has_value())` is `if (x)`, and a shortcut for `x.value()` is `*x`. + +The reason that `BS::this_thread::get_pool()` returns a `void*` is that `BS::thread_pool` is a template. Once you obtain the pool pointer, you must cast it to the desired instantiation of the template if you want to use any member functions. Note that you have to cast it to the correct type; if you cast a pointer to a `BS::light_thread_pool` into a pointer to a `BS::priority_thread_pool`, for example, your program will have undefined behavior. (Please see the [optional features](#optional-features) section for more information about the template parameters and aliases.) + +Here is an example illustrating all of the above: + +```cpp +#include "BS_thread_pool.hpp" // BS::light_thread_pool, BS::synced_stream, BS::this_thread +#include // std::atomic +#include // std::size_t +#include // std::optional +#include // std::thread + +BS::synced_stream sync_out; +BS::light_thread_pool p1; +BS::light_thread_pool p2; +std::atomic ltr = 'A'; + +void check_this_thread(const char letter) +{ + const std::optional my_pool = BS::this_thread::get_pool(); + const std::optional my_index = BS::this_thread::get_index(); + + if (my_pool && my_index) + { + const std::size_t pool_number = *my_pool == &p1 ? 1 : 2; + sync_out.println("Task ", letter, " is being executed by thread #", *my_index, " of pool #", pool_number, '.'); + static_cast(*my_pool)->detach_task( + [letter] + { + sync_out.println("-> Task ", ltr++, " was submitted by task ", letter, " using detach_task()."); + }); + } + else + { + sync_out.println("Task ", letter, " is being executed by an independent thread, not in any thread pools."); + std::thread( + [letter] + { + sync_out.println("-> Task ", ltr++, " was submitted by task ", letter, " using a detached std::thread."); + }) + .detach(); + } +} + +int main() +{ + p1.submit_task( + [] + { + check_this_thread(ltr++); + }) + .wait(); + p2.submit_task( + [] + { + check_this_thread(ltr++); + }) + .wait(); + std::thread( + [] + { + check_this_thread(ltr++); + }) + .join(); +} +``` + +The output of this program will be similar to: + +```none +Task A is being executed by thread #3 of pool #1. +-> Task B was submitted by task A using detach_task(). +Task C is being executed by thread #7 of pool #2. +-> Task D was submitted by task C using detach_task(). +Task E is being executed by an independent thread, not in any thread pools. +-> Task F was submitted by task E using a detached std::thread. +``` + +In this example, we execute the task `check_this_thread()` in three different ways: + +1. By submitting it from the thread pool `p1`. +2. By submitting it from the thread pool `p2`. +3. By submitting it from an independent `std::thread`. + +The task calls `BS::this_thread::get_pool()` and `BS::this_thread::get_index()` and receives two `std::optional` objects, `my_pool` and `my_index`. If both have a value (that is, evaluate to `true`), then the task knows it is running in a thread pool. The actual values are then obtained by "dereferencing" them: the pool pointer is `*my_pool`, and the thread index is `*my_index`. + +The task deduces which pool it is running in by comparing the pointer `*my_pool` to the addresses of the pools `p1` and `p2`. It also gets the index of the thread from `*my_index`. Finally, it detaches an additional task (without waiting for it, as that might cause a deadlock!) from its own pool by first casting the `void*` pointer to the correct type, which in this case is `BS::light_thread_pool*`, and then calling the `detach_task()` member function of that specific pool. + +If `my_pool` and `my_index` do not have values (that is, evaluate to `false`), then the task knows it is running in an independent thread. In this case, it detaches the additional task using another independent thread. + +### Thread initialization functions + +Sometimes, it is necessary to initialize the threads before they run any tasks. This can be done by submitting a proper initialization function to the `BS::thread_pool` constructor or to `reset()`, either as the only argument or as the second argument after the desired number of threads. + +The thread initialization function must have no return value. It can either take one argument, the thread index of type `std::size_t`, or zero arguments. In the latter case, the function can use `BS::this_thread::get_index()` to find the thread index. In addition, the function can use `BS::this_thread::get_pool()` to find which pool its thread belongs to. + +The initialization functions are effectively submitted as a set of special tasks, one per thread, which bypass the queue, but still count towards the number of running tasks. This means `get_tasks_total()` and `get_tasks_running()` will report that these tasks are running if they are checked immediately after the pool is initialized. + +This is done so that the user has the option to either wait for the initialization functions to finish, by calling `wait()` on the pool, or just keep going. In either case, the initialization functions will always finish running before any tasks are executed by the corresponding thread, so there is no reason to wait for them to finish unless they have some side-effects that affect the main thread, or if they must finish running on **all** the threads before the pool starts executing any tasks. Here is a simple example: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::mt19937_64, std::random_device +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::mt19937_64, std::random_device BS::synced_stream sync_out; thread_local std::mt19937_64 twister; @@ -1326,16 +1587,63 @@ int main() } ``` -In this example, we create a `thread_local` Mersenne twister engine, meaning that each thread has its own independent engine. However, we did not seed the engine, so each thread will generate the exact same sequence of pseudo-random numbers. To remedy this, we pass an initialization function to the `BS::thread_pool` constructor which seeds the twister in each thread with the (hopefully) non-deterministic random number generator `std::random_device`. +In this example, we create a `thread_local` Mersenne twister engine, meaning that each thread has its own independent engine. However, if we do not seed the engine, each thread would generate the exact same sequence of pseudo-random numbers. To remedy this, we pass an initialization function to the `BS::thread_pool` constructor which seeds the twister in each thread with the (hopefully) non-deterministic random number generator `std::random_device`. + +Note that the lambda function we passed to `submit_sequence()` has the signature `[](int)`, with an unnamed `int` argument, as it does not make use of the sequence index, which will be a number in the range `[0, 4)`. This is an easy way to simply submit the same task multiple times. + +**Warning:** Exceptions thrown by thread initialization functions must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. + +### Thread cleanup functions + +Similarly to the thread initialization function, it is also possible to provide the pool with a cleanup function to run in each thread right before it is destroyed, which will happen when the pool is destructed or reset. Like the initialization function, the cleanup function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. Each pool can have its own cleanup function, which is specified using the member function `set_cleanup_func()`. Here is an example: + +```cpp +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::this_thread, BS::thread_pool +#include // std::chrono +#include // std::size_t +#include // std::ofstream +#include // std::to_string +#include // std::this_thread + +thread_local std::ofstream log_file; +thread_local BS::synced_stream sync_out(log_file); +constexpr std::size_t threads = 4; + +int main() +{ + BS::thread_pool pool(threads, + [](const std::size_t idx) + { + log_file.open("thread_" + std::to_string(idx) + ".log"); + }); + pool.set_cleanup_func( + [] + { + log_file.close(); + }); + pool.submit_sequence(0, threads * 10, + [](const std::size_t idx) + { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + sync_out.println("Task ", idx, " is running on thread ", *BS::this_thread::get_index(), '.'); + }) + .wait(); +} +``` + +In this example, we create 4 threads, each of which has a separate thread-local `BS::synced_stream` object writing to its own log file of the form `thread_N.log` where `N` is the thread index. The initialization function, passed as an argument to the constructor, opens the log file. The cleanup function, set using `set_cleanup_func()`, closes the log file. + +We submit 40 tasks to the queue using `submit_sequence()`, each of which prints a message to the log file indicating which thread it is running on. When the `main()` function exits and `pool` is destroyed, the cleanup function is called for each thread, ensuring that the log files are closed properly. + +**Warning:** As with initialization functions, exceptions thrown by thread cleanup functions must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. ### Passing task arguments by constant reference -In C++, it is often crucial to pass function arguments by reference or constant reference, instead of by value. This allows the function to access the object being passed directly, rather than creating a new copy of the object. We have already seen that submitting an argument by reference is a simple matter of capturing it with a `&` in the lambda capture list. To submit as constant reference, we can use `std::as_const` as in the following example: +In C++, it is often crucial to pass function arguments by reference or constant reference, instead of by value. This allows the function to access the object being passed directly, rather than creating a new copy of the object. We have already seen [above](#detaching-and-waiting-for-tasks) that submitting an argument by reference is a simple matter of capturing it with a `&` in the lambda capture list. To submit by **constant** reference, we can use `std::as_const()` as in the following example: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::as_const +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::as_const BS::synced_stream sync_out; @@ -1370,33 +1678,118 @@ int main() The `increment()` function takes a **reference** to an integer, and increments that integer. Passing the argument by reference guarantees that `n` itself, in the scope of `main()`, will be incremented - rather than a copy of it in the scope of `increment()`. -Similarly, the `print()` function takes a **constant reference** to an integer, and prints that integer. Passing the argument by constant reference guarantees that the variable will not be accidentally modified by the function, even though we are accessing `n` itself, rather than a copy. If we replace `print` with `increment`, the program won't compile, as `increment` cannot take constant references. +Similarly, the `print()` function takes a **constant reference** to an integer, and prints that integer. Passing the argument by constant reference guarantees that the variable will not be accidentally modified by the function, even though we are accessing `n` itself, rather than a copy. If we replace `print()` with `increment()`, the program won't compile, as `increment()` cannot take constant references. -Generally, it is not really necessary to pass arguments by constant reference, but it is more "correct" to do so, if we would like to guarantee that the variable being referenced is indeed never modified. This section is therefore included here for completeness. +Generally, it is not really necessary to pass arguments by constant reference, but it is more "correct" to do so, if we would like to guarantee that the variable being referenced is indeed never modified. ## Optional features +### Enabling features + +The thread pool has some optional features, which are disabled by default to minimize overhead. They can be enabled by passing the appropriate template parameter to the `BS::thread_pool` class when creating the pool. The template parameter is a bitmask, so you can enable several features at once by combining them with the bitwise OR operator `|`. The bitmask flags are members of the `BS::tp` enumeration: + +* `BS::tp::priority` enables [task priority](#setting-task-priority). +* `BS::tp::pause` enables [pausing the pool](#pausing-the-pool). +* `BS::tp::wait_deadlock_checks` enables [wait deadlock checks](#avoiding-wait-deadlocks). +* The default is `BS::tp::none`, which disables all optional features. + +For example, to enable both task priority and pausing the pool, the thread pool object should be created like this: + +```cpp +BS::thread_pool pool; +``` + +Convenience aliases are defined as follows: + +* `BS::light_thread_pool` disables all optional features (equivalent to `BS::thread_pool` with the default template parameter, that is, `BS::thread_pool`). +* `BS::priority_thread_pool` enables task priority (equivalent to `BS::thread_pool`). +* `BS::pause_thread_pool` enables pausing the pool (equivalent to `BS::thread_pool`). +* `BS::wdc_thread_pool` enables wait deadlock checks (equivalent to `BS::thread_pool`). + +There are no aliases with multiple features enabled; if this is desired, you must either pass the template parameter explicitly or define your own alias, and use the bitwise OR operator as shown above. + +Note that, since optional features are enabled separately for each `BS::thread_pool` object, you can have multiple pools with different features enabled in the same program. For example, you can have one `BS::light_thread_pool` for tasks that do not need to be prioritized, and a separate `BS::priority_thread_pool` for tasks that do. + +### Setting task priority + +Turning on the `BS::tp::priority` flag in the template parameter to `BS::thread_pool` enables task priority. In addition, the library defines the convenience alias `BS::priority_thread_pool`, which is equivalent to `BS::thread_pool`. When this feature is enabled, the static member `priority_enabled` will be set to `true`. + +The priority of a task or group of tasks may then be specified as an additional argument (at the end of the argument list) to `detach_task()`, `submit_task()`, `detach_blocks()`, `submit_blocks()`, `detach_loop()`, `submit_loop()`, `detach_sequence()`, and `submit_sequence()`. If the priority is not specified, the default value will be 0. + +The priority is a number of type `BS::priority_t`, which is a signed 8-bit integer, so it can have any value between -128 and +127. The tasks will be executed in priority order from highest to lowest. If priority is assigned to the block/loop/sequence parallelization functions, which submit multiple tasks, then all of these tasks will have the same priority. + +The enumeration `BS::pr` contains some pre-defined priorities for users who wish to avoid magic numbers and enjoy better future-proofing. In order of decreasing priority, the pre-defined priorities are: `BS::pr::highest`, `BS::pr::high`, `BS::pr::normal`, `BS::pr::low`, and `BS::pr::lowest`. + +Here is a simple example: + +```cpp +#include "BS_thread_pool.hpp" // BS::priority_thread_pool, BS::synced_stream + +BS::synced_stream sync_out; +BS::priority_thread_pool pool(1); + +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 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 first."); + }, + BS::pr::highest); + pool.detach_task( + [] + { + sync_out.println("This task will execute fourth."); + }, + BS::pr::low); +} +``` + +This program will print out the tasks in the correct priority order. Note that for simplicity, we used a pool with just one thread, so the tasks will run one at a time. In a pool with 5 or more threads, all 5 tasks will actually run more or less at the same time, because, for example, the task with the second-highest priority will be picked up by another thread while the task with the highest priority is still running. + +Of course, this is just a pedagogical example. In a realistic use case we may want, for example, to submit tasks that must be completed immediately with high priority so they skip over other tasks already in the queue, or background non-urgent tasks with low priority so they evaluate only after higher-priority tasks are done. + +Task priority is facilitated using [`std::priority_queue`](https://en.cppreference.com/w/cpp/container/priority_queue), which has O(log n) complexity for storing new tasks, but only O(1) complexity for retrieving the next (i.e. highest-priority) task. This is in contrast with [`std::queue`](https://en.cppreference.com/w/cpp/container/queue), used if priority is disabled, which both stores and retrieves with O(1) complexity. + +Due to this, enabling the priority queue can incur a very slight decrease in performance, depending on the specific use case, which is why this feature is disabled by default. In other words, you gain functionality, but pay for it in performance. However, the difference in performance is never substantial, and compiler optimizations can often reduce it to a negligible amount. + +Lastly, please note that when using the priority queue, tasks will not necessarily be executed in the same order they were submitted, **even if they all have the same priority**. This is due to the implementation of `std::priority_queue` as a [binary heap](https://en.wikipedia.org/wiki/Binary_heap), which means tasks are stored as a binary tree instead of sequentially. + ### Pausing the pool -Sometimes you may wish to temporarily pause the execution of tasks, or perhaps you want to submit tasks to the queue in advance and only start executing them at a later time. You can do this using the member functions `pause()`, `unpause()`, and `is_paused()`. +Turning on the `BS::tp::pause` flag in the template parameter to `BS::thread_pool` enables pausing the pool. In addition, the library defines the convenience alias `BS::pause_thread_pool`, which is equivalent to `BS::thread_pool`. When this feature is enabled, the static member `pause_enabled` will be set to `true`. -However, these functions are disabled by default, and must be explicitly enabled by defining the macro `BS_THREAD_POOL_ENABLE_PAUSE` before including `BS_thread_pool.hpp`. The reason is that pausing the pool adds additional checks to the waiting and worker functions, which have a very small but non-zero overhead. - -When you call `pause()`, the workers will temporarily stop retrieving new tasks out of the queue. However, any tasks already executed will keep running until they are done, since the thread pool has no control over the internal code of your tasks. If you need to pause a task in the middle of its execution, you must do that manually by programming your own pause mechanism into the task itself. To resume retrieving tasks, call `unpause()`. To check whether the pool is currently paused, call `is_paused()`. +This feature enables the member functions `pause()`, `unpause()`, and `is_paused()`. When you call `pause()`, the workers will temporarily stop retrieving new tasks out of the queue. However, any tasks already executed will keep running until they are done, since the thread pool has no control over the internal code of your tasks. If you need to pause a task in the middle of its execution, you must do that manually by programming your own pause mechanism into the task itself. To resume retrieving tasks, call `unpause()`. To check whether the pool is currently paused, call `is_paused()`. Here is an example: ```cpp -#define BS_THREAD_POOL_ENABLE_PAUSE -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::chrono -#include // std::this_thread +#include "BS_thread_pool.hpp" // BS::pause_thread_pool, BS::synced_stream +#include // std::chrono +#include // std::this_thread BS::synced_stream sync_out; -BS::thread_pool pool(4); +BS::pause_thread_pool pool(4); -void sleep_half_second(const int i) +void sleep_half_second(const unsigned int i) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); sync_out.println("Task ", i, " done."); @@ -1452,21 +1845,19 @@ Task 10 done. Task 11 done. ``` -Here is what happened. We initially submitted a total of 8 tasks to the queue. Since we waited for 250ms before pausing, the first 4 tasks have already started running, so they kept running until they finished. While the pool was paused, we submitted 4 more tasks to the queue, but they just waited at the end of the queue. When we unpaused, the remaining 4 initial tasks were executed, followed by the 4 new tasks. +In this example, we initially submit a total of 8 tasks to the queue. The first 4 tasks start running immediately (only 4, since the pool has 4 threads). We wait for 250ms, and then pause. The tasks that are already running (for 500ms) will keep running until they finished; pausing has no effect on currently running tasks. However, the other 4 tasks will not be executed yet. While the pool is paused, we submit 4 more tasks to the queue, but they just wait at the end of the queue. When we unpause, the remaining 4 initial tasks are executed, followed by the 4 new tasks. -While the workers are paused, `wait()` will wait for the running tasks instead of all tasks (otherwise it would wait forever). This is demonstrated by the following program: +While the workers are paused, `wait()` will wait only for the running tasks instead of all tasks (otherwise it would wait forever). This is demonstrated by the following program: ```cpp -#define BS_THREAD_POOL_ENABLE_PAUSE -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::chrono -#include // std::this_thread +#include "BS_thread_pool.hpp" // BS::pause_thread_pool, BS::synced_stream +#include // std::chrono +#include // std::this_thread BS::synced_stream sync_out; -BS::thread_pool pool(4); +BS::pause_thread_pool pool(4); -void sleep_half_second(const int i) +void sleep_half_second(const unsigned int i) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); sync_out.println("Task ", i, " done."); @@ -1544,76 +1935,343 @@ All tasks completed. The first `wait()`, which was called while the pool was not paused, waited for all 8 tasks, both running and queued. The second `wait()`, which was called after pausing the pool, only waited for the 4 running tasks, while the other 8 tasks remained queued, and were not executed since the pool was paused. Finally, the third `wait()`, which was called after unpausing the pool, waited for the remaining 8 tasks, both running and queued. +Note that pausing the pool adds additional checks to the waiting and worker functions, which have a very small but non-zero overhead. This is why this feature is disabled by default. + **Warning:** If the thread pool is destroyed while paused, any tasks still in the queue will never be executed! ### Avoiding wait deadlocks -Consider the following program: +Turning on the `BS::tp::wait_deadlock_checks` flag in the template parameter to `BS::thread_pool` enables wait deadlock checks. In addition, the library defines the convenience alias `BS::wdc_thread_pool`, which is equivalent to `BS::thread_pool`. When this feature is enabled, the static member `wait_deadlock_checks_enabled` will be set to `true`. + +To understand why this feature is useful, consider the following program: ```cpp -#include "BS_thread_pool.hpp" // BS::thread_pool -#include // std::cout +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool + +BS::synced_stream sync_out; +BS::thread_pool pool; int main() { - BS::thread_pool pool; pool.detach_task( - [&pool] + [] { pool.wait(); - std::cout << "Done waiting.\n"; + sync_out.println("Done waiting."); }); } ``` This program creates a thread pool, and then detaches a task that waits for tasks in the same thread pool to complete. If you run this program, it will never print the message "Done waiting", because the task will wait for **itself** to complete. This causes a **deadlock**, and the program will wait forever. -Usually, in simple programs, this will never happen. However, in more complicated programs, perhaps ones running multiple thread pools in parallel, wait deadlocks could potentially occur. In such cases, the macro `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK` can be defined before including `BS_thread_pool.hpp`. `wait()` will then check whether the user tried to call it from within a thread of the same pool, and if so, it will throw the exception `BS::thread_pool::wait_deadlock` instead of waiting. This check is disabled by default because wait deadlocks are not something that happens often, and the check adds a small but non-zero overhead every time `wait()` is called. +Usually, in simple programs, this will never happen. However, in more complicated programs, perhaps ones running multiple thread pools in parallel, wait deadlocks could potentially occur. In such cases, wait deadlock checks may be useful. If enabled, `wait()`, `wait_for()`, and `wait_until()` will check whether the user tried to call them from within a thread of the same pool, and if so, they will throw the exception `BS::wait_deadlock` instead of waiting. Here is an example: ```cpp -#define BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK -#include "BS_thread_pool.hpp" // BS::thread_pool -#include // std::cout +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::wdc_thread_pool + +BS::synced_stream sync_out; +BS::wdc_thread_pool pool; int main() { - BS::thread_pool pool; pool.detach_task( - [&pool] + [] { try { pool.wait(); - std::cout << "Done waiting.\n"; + sync_out.println("Done waiting."); } - catch (const BS::thread_pool::wait_deadlock&) + catch (const BS::wait_deadlock&) { - std::cout << "Error: Deadlock!\n"; + sync_out.println("Error: Deadlock!"); } }); } ``` -This time, `wait()` will detect the deadlock, and will throw an exception, causing the output to be `"Error: Deadlock!"`. +This time, `wait()` will detect the deadlock, and will throw an exception, causing the output to be "Error: Deadlock!". -Note that if the feature-test macro `__cpp_exceptions` is undefined, `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK` will be automatically undefined. +Wait deadlock checks are disabled by default because wait deadlocks are not something that happens often, and the check adds a small but non-zero overhead every time `wait()`, `wait_for()`, or `wait_until()` is called. Note that if the feature-test macro `__cpp_exceptions` is undefined, wait deadlock checks will be automatically disabled, and trying to compile a program which creates a pool with the `BS::tp::wait_deadlock_checks` flag enabled will result in a compilation error. + +## Native extensions + +### Enabling the native extensions + +While portability is one of the guiding principle for developing this library, non-portable features such as setting the thread priority using the operating system's native API can be very useful. Therefore, the library includes native extensions - which are disabled by default, as they are not portable. + +The native extensions may be enabled by defining the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` at compilation time. If including the library as a header file, the macro must be defined before `#include "BS_thread_pool.hpp"`. Note that even if the macro is defined, the native extensions are disabled automatically if a supported operating system (Windows, Linux, or macOS) is not detected. + +If importing the library [as a C++20 module](#importing-the-library-as-a-c20-module), defining the macro before importing the module will not work, as modules cannot access macros defined in the program that imported them. Instead, you must define the macro as a compiler flag: `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` for Clang and GCC or `/D BS_THREAD_POOL_NATIVE_EXTENSIONS` for MSVC. + +[The test program](#testing-the-library) only tests the native extensions if the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` is defined at compilation time. If importing the library [as a C++20 module](#importing-the-library-as-a-c20-module), please ensure that the macro is also enabled when compiling the module. + +The `constexpr` flag `BS::thread_pool_native_extensions` indicates whether the thread pool library was compiled with native extensions enabled. Note that the flag will be `false` if `BS_THREAD_POOL_NATIVE_EXTENSIONS` is defined but the operating system is unsupported. + +**Warning:** Please note that, as of v5.0.0 of the thread pool library, the native extensions have only been tested on **Windows 11 23H2, Ubuntu 24.10, and macOS 15.1**. They have not been tested on older versions of these operating systems, other Linux distributions, or any other operating systems, and are therefore not guaranteed to work on every system. If you encounter any issues, please report them on [the GitHub repository](https://github.com/bshoshany/thread-pool). + +### Setting thread priority + +The thread pool's native extensions provide the ability to set a thread's priority using the operating system's native API. Please note that this is **not** the same as [setting a task's priority](#setting-task-priority), which is a feature of the thread pool's queue, unrelated to the pool's threads themselves. Task priority controls which tasks are executed first, while thread priority (roughly) controls how much CPU time a thread gets compared to other threads. In addition, you can use the native extensions to set the priority of any thread (such as a thread created using `std::thread`), not just a pool thread. + +For performance-critical applications, you may wish to increase the thread priority, while for applications that should run in the background, you may wish to decrease it. As priority is handled very differently on different operating systems, the thread pool library provides an abstraction layer over the native APIs, in the form of the enumeration class `BS::os_thread_priority`, which has the following 7 members: + +* `BS::os_thread_priority::idle` +* `BS::os_thread_priority::lowest` +* `BS::os_thread_priority::below_normal` +* `BS::os_thread_priority::normal` +* `BS::os_thread_priority::above_normal` +* `BS::os_thread_priority::highest` +* `BS::os_thread_priority::realtime` + +On Windows, these pre-defined priorities map 1-to-1 with [the thread priority values defined by the Windows API](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadpriority) (with `realtime` mapping to time critical priority). On Linux and macOS, thread priorities are a lot more complicated, so these pre-defined priorities are mapped to the parameters available in the native API. + +On Linux (with POSIX threads), thread priority is determined by three factors: [scheduling policy](https://www.man7.org/linux/man-pages/man3/pthread_setschedparam.3.html), priority value, and ["nice" value](https://www.man7.org/linux/man-pages/man2/setpriority.2.html). The thread pool library's abstraction layer distills these factors into the above pre-defined levels, for simplicity and portability. The total number of possible combinations of parameters is much larger, but allowing more fine-grained control would not be portable, and in any case it would have limited use. For the precise mapping, please refer to the source code itself (in the header file `BS_thread_pool.hpp`). + +On macOS, the thread pool library will also use POSIX threads, but unlike Linux, the "nice" value is per-process, not per-thread (in compliance with the POSIX standard). However, macOS does allow more freedom with respect to the available range of priorities. Again, for the precise details of the mapping, please refer to the source code itself. + +Most users do not need to worry about the specifics of how thread priority is handled on different operating systems. The abstraction layer provided by the thread pool library is meant to make everything as simple and portable as possible. However, it is important to note that only Windows allows a non-privileged user to set a thread's priority to a higher value. On Linux and macOS, a non-privileged user can only set a thread's priority to a lower value, and only root can set a higher value; also, confusingly, if a user decreased the priority of their thread from normal to a lower priority, they cannot increase it back to normal without root privileges, even though normal was the thread's initial priority. + +Thread priority is managed using two static member functions of the `BS::this_thread` class: + +* `BS::this_thread::get_os_thread_priority()` gets the current thread's priority. It returns an object of type `std::optional`. If the returned object does not contain a value, then either the priority could not be determined, or it is not one of the pre-defined values listed above. +* `BS::this_thread::set_os_thread_priority()` sets the current thread's priority. It returns `true` if the priority was set successfully, or `false` otherwise. Usually, `false` means that the user does not have the necessary permissions to set the desired priority. + +Increasing or decreasing the priority of all the threads in a pool can be done most easily using an [initialization function](#thread-initialization-functions). Here is an example: + +```cpp +#define BS_THREAD_POOL_NATIVE_EXTENSIONS +#include "BS_thread_pool.hpp" // BS::os_thread_priority, BS::synced_stream, BS::this_thread, BS::thread_pool +#include // std::size_t +#include // std::map +#include // std::optional +#include // std::string + +BS::synced_stream sync_out; +BS::os_thread_priority target = BS::os_thread_priority::highest; + +const std::map os_thread_priority_map = {{BS::os_thread_priority::idle, "idle"}, {BS::os_thread_priority::lowest, "lowest"}, {BS::os_thread_priority::below_normal, "below_normal"}, {BS::os_thread_priority::normal, "normal"}, {BS::os_thread_priority::above_normal, "above_normal"}, {BS::os_thread_priority::highest, "highest"}, {BS::os_thread_priority::realtime, "realtime"}}; + +std::string os_thread_priority_name(const BS::os_thread_priority priority) +{ + const std::map::const_iterator it = os_thread_priority_map.find(priority); + return (it != os_thread_priority_map.end()) ? it->second : "unknown"; +} + +void set_priority(const std::size_t idx) +{ + const std::optional get_result = BS::this_thread::get_os_thread_priority(); + if (get_result) + sync_out.println("The OS thread priority of thread ", idx, " is currently set to '", os_thread_priority_name(*get_result), "'."); + else + sync_out.println("Error: Failed to get the OS thread priority of thread ", idx, '!'); + const bool set_result = BS::this_thread::set_os_thread_priority(target); + sync_out.println(set_result ? "Successfully" : "Error: Failed to", " set the OS priority of thread ", idx, " to '", os_thread_priority_name(target), "'."); +} + +int main() +{ + BS::thread_pool pool(4, set_priority); +} +``` + +On Linux or macOS, please ensure that you run this example as root using `sudo`, otherwise it will fail. In this example we used an initialization function `set_priority()` to first print the initial priority of each thread (which should be "normal") and then set the priority of each thread to "highest". `os_thread_priority_name()` is a helper function to convert a `BS::os_thread_priority` value to a human-readable string. + +### Setting thread affinity + +The thread pool's native extensions allow the user to set a thread's processor affinity using the operating system's native API. Processor affinity, sometimes called "pinning", controls which logical processors a thread is allowed to run on. Generally, a non-hyperthreaded core corresponds to one logical processor, and a hyperthreaded core corresponds to two logical processors. + +This can be useful for performance optimization, as it can reduce cache misses. However, it can also degrade performance, sometimes severely, since the thread will not run at all until its assigned cores are available. Therefore, it is usually better to let the operating system's scheduler manage thread affinities on its own, except in very specific cases. + +Please note that setting thread affinity works on Windows and Linux, but not on macOS, as the native API does not allow it. As affinity is handled differently on different operating systems, the thread pool library provides an abstraction layer over the native APIs. In this abstraction layer, affinity is controlled using an `std::vector` where each element corresponds to a logical processor. + +Thread affinity is managed using two static member functions of the `BS::this_thread` class: + +* `BS::this_thread::get_os_thread_affinity()` gets the current thread's affinity. It returns an object of type `std::optional>`. If the returned object does not contain a value, then the affinity could not be determined. On macOS, this function always returns `std::nullopt`. +* `BS::this_thread::set_os_thread_affinity()` sets the current thread's affinity. It returns `true` if the affinity was set successfully, or `false` otherwise. On macOS, this function always returns `false`. + +Note that the thread affinity must be a subset of the process affinity (as obtained using [`BS::get_os_process_affinity()`](#setting-process-affinity)) for the containing process of a thread. + +Setting thread affinity can significantly increase performance if multiple threads are accessing the same data, as the data can be kept in the local cache of the specific core that the threads are running on. This is illustrated in the following program: + +```cpp +#define BS_THREAD_POOL_NATIVE_EXTENSIONS +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::this_thread +#include // std::atomic +#include // std::chrono +#include // std::uint64_t +#include // std::thread +#include // std::vector + +void do_test(const bool pin_threads) +{ + BS::synced_stream sync_out; + constexpr std::uint64_t num_increments = 10'000'000; + sync_out.println(pin_threads ? "With " : "Without", " thread pinning:"); + std::atomic counter = 0; + auto worker = [&counter, pin_threads] + { + if (pin_threads) + { + std::vector affinity(std::thread::hardware_concurrency(), false); + affinity[0] = true; + BS::this_thread::set_os_thread_affinity(affinity); + } + for (std::uint64_t i = 0; i < num_increments; ++i) + ++counter; + }; + const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); + std::thread thread1(worker); + std::thread thread2(worker); + thread1.join(); + thread2.join(); + const std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + sync_out.println("Final count: ", counter, ", execution time: ", (std::chrono::duration_cast(end - start)).count(), " ms."); +} + +int main() +{ + do_test(false); + do_test(true); +} +``` + +The output should be similar to: + +```none +Without thread pinning: +Final count: 20000000, execution time: 160 ms. +With thread pinning: +Final count: 20000000, execution time: 68 ms. +``` + +In this program, we create two threads, each of which increments an atomic counter 10 million times. First, we do this without thread pinning; in this case, since the OS will most likely run the threads on two different cores, the state of the atomic variable will need to be synchronized between the two cores, which will incur a performance penalty. Then, we do this with thread pinning, using `BS::this_thread::set_os_thread_affinity()` to set the affinity of each thread to core 0 by passing a vector with `true` at index 0 and `false` at all other indices. In this case, the atomic variable will be kept in the local cache of core 0, which will increase performance. + +**Warning:** Setting the affinity of threads in a pool is almost never a good idea! When you submit a task to a thread pool, you have no control over which thread it will actually run in. The main benefit of thread affinity is to reduce cache misses, but there is no way to guarantee that tasks accessing the same data will run on the same core if they are submitted to a pool. In fact, setting the affinity of the pool threads will almost certainly decrease performance, sometimes substantially, as the operating system's scheduler will be prevented from assigning threads to cores in the most optimal way. The most common use case for `BS::this_thread::set_os_thread_affinity()` is to set the affinity of individual threads created independently of any pool, for example using `std::thread`. + +### Setting thread names + +The thread pool's native extensions permit setting a thread's name using the operating system's native API. This can be useful for debugging, as the names of the threads will be visible in the debugger (for example, in the Call Stack on Visual Studio Code). + +As with other features of the native extensions, the thread pool library provides an abstraction layer over the native APIs, consisting of the following two static member functions of the `BS::this_thread` class: + +* `BS::this_thread::get_os_thread_name()` gets the current thread's name. It returns an object of type `std::optional`. If the returned object does not contain a value, then the name could not be determined. +* `BS::this_thread::set_os_thread_name()` sets the current thread's name. It returns `true` if the name was set successfully, or `false` otherwise. Note that on Linux thread names are limited to 16 characters, including the null terminator. + +This feature is illustrated by the following program: + +```cpp +#define BS_THREAD_POOL_NATIVE_EXTENSIONS +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::this_thread, BS::thread_pool +#include // std::size_t +#include // std::optional +#include // std::string, std::to_string + +BS::synced_stream sync_out; + +void set_name(const std::size_t idx) +{ + const std::string name = "Thread " + std::to_string(idx); + const bool result = BS::this_thread::set_os_thread_name(name); + sync_out.println(result ? "Successfully" : "Error: Failed to", " set the name of thread ", idx, " to '", name, "'."); +} + +void get_name() +{ + const std::optional result = BS::this_thread::get_os_thread_name(); + if (result) + sync_out.println("This thread's name is set to '", *result, "'."); + else + sync_out.println("Error: Failed to get this thread's name!"); +} + +int main() +{ + const bool result = BS::this_thread::set_os_thread_name("Main Thread"); + sync_out.println(result ? "Successfully" : "Error: Failed to", " set the name of the main thread."); + BS::thread_pool pool(4, set_name); + pool.wait(); + // Place a breakpoint here to see the thread names in the debugger. + pool.submit_task(get_name).wait(); +} +``` + +If you place a breakpoint on the indicated line, you will be able to see the names of the threads in the debugger. The main thread will be named "Main Thread", while the 4 pool threads will be named "Thread 0" to "Thread 3". In the last line, a random thread's name will be read and printed out. + +### Setting process priority + +Although not directly related to multithreading, `BS::thread_pool`'s native extensions also provide the ability to set the entire process's priority using the operating system's native API. As with thread priority, the thread pool library provides an abstraction layer over the native APIs, in the form of the enumeration class `BS::os_process_priority`, which has the following 6 members: + +* `BS::os_process_priority::idle` +* `BS::os_process_priority::below_normal` +* `BS::os_process_priority::normal` +* `BS::os_process_priority::above_normal` +* `BS::os_process_priority::high` +* `BS::os_process_priority::realtime` + +On Windows, these pre-defined priorities map 1-to-1 with [the process priority classes defined by the Windows API](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass). On Linux and macOS, process priorities are mapped to ["nice" values](https://www.man7.org/linux/man-pages/man2/setpriority.2.html), as given by the actual values of the enumeration members (note that lower numbers correspond to higher priorities). + +Process priority is managed using two functions: + +* `BS::get_os_process_priority()` gets the process's priority. It returns an object of type `std::optional`. If the returned object does not contain a value, then either the priority could not be determined, or it is not one of the pre-defined values listed above. +* `BS::set_os_process_priority()` sets the process's priority. It returns `true` if the priority was set successfully, or `false` otherwise. Usually, `false` means that the user does not have the necessary permissions to set the desired priority. + +This is demonstrated by the following program: + +```cpp +#define BS_THREAD_POOL_NATIVE_EXTENSIONS +#include "BS_thread_pool.hpp" // BS::get_os_process_priority, BS::os_process_priority, BS::set_os_process_priority, BS::synced_stream +#include // std::map +#include // std::optional +#include // std::string + +BS::synced_stream sync_out; +BS::os_process_priority target = BS::os_process_priority::high; + +const std::map os_process_priority_map = {{BS::os_process_priority::idle, "idle"}, {BS::os_process_priority::below_normal, "below_normal"}, {BS::os_process_priority::normal, "normal"}, {BS::os_process_priority::above_normal, "above_normal"}, {BS::os_process_priority::high, "high"}, {BS::os_process_priority::realtime, "realtime"}}; + +std::string os_process_priority_name(const BS::os_process_priority priority) +{ + const std::map::const_iterator it = os_process_priority_map.find(priority); + return (it != os_process_priority_map.end()) ? it->second : "unknown"; +} + +int main() +{ + const std::optional get_result = BS::get_os_process_priority(); + if (get_result) + sync_out.println("The OS process priority is currently set to '", os_process_priority_name(*get_result), "'."); + else + sync_out.println("Error: Failed to get the OS process priority!"); + const bool set_result = BS::set_os_process_priority(target); + sync_out.println(set_result ? "Successfully" : "Error: Failed to", " set the OS process priority to '", os_process_priority_name(target), "'."); +} +``` + +On Linux or macOS, please ensure that you run this example as root using `sudo`, otherwise it will fail. (Note that here we didn't actually need to use `BS::synced_stream`, since we are not using the thread pool, and only the main thread prints to the stream; we used it only for consistency with other examples.) + +### Setting process affinity + +The thread pool's native extensions also allow the user to set the entire process's processor affinity using the operating system's native API. This works on Windows and Linux, but not on macOS, as the native API does not allow it. As with thread affinity, the thread pool library provides an abstraction layer over the native APIs, in the form of an `std::vector` where each element corresponds to a logical processor. + +Process affinity is managed using two functions: + +* `BS::this_thread::get_os_process_affinity()` gets the process's affinity. It returns an object of type `std::optional>`. If the returned object does not contain a value, then the affinity could not be determined. On macOS, this function always returns `std::nullopt`. +* `BS::this_thread::set_os_process_affinity()` sets the process's affinity. It returns `true` if the affinity was set successfully, or `false` otherwise. On macOS, this function always returns `false`. ### Accessing native thread handles -The `BS::thread_pool` member function `get_native_handles()` returns a vector containing the underlying implementation-defined thread handles for each of the pool's threads. These can then be used in an implementation-specific way to manage the threads at the OS level +If the native extensions are enabled, the `BS::thread_pool` class gains the member function `get_native_handles()`, which returns a vector containing the underlying implementation-defined thread handles for each of the pool's threads. These can then be used in an implementation-specific way to manage the threads at the OS level. -However, note that this will generally **not** be portable code. Furthermore, this feature uses [std::thread::native_handle()](https://en.cppreference.com/w/cpp/thread/thread/native_handle), which is in the C++ standard library, but is **not** guaranteed to be present on all systems. Therefore, this feature is turned off by default, and must be turned on by defining the macro `BS_THREAD_POOL_ENABLE_NATIVE_HANDLES` before including `BS_thread_pool.hpp`. - -Here is an example: +Here is a quick example: ```cpp -#define BS_THREAD_POOL_ENABLE_NATIVE_HANDLES -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream -#include // std::thread -#include // std::vector +#define BS_THREAD_POOL_NATIVE_EXTENSIONS +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool +#include // std::thread +#include // std::vector BS::synced_stream sync_out; BS::thread_pool pool(4); @@ -1621,7 +2279,7 @@ BS::thread_pool pool(4); int main() { std::vector handles = pool.get_native_handles(); - for (BS::concurrency_t i = 0; i < handles.size(); ++i) + for (std::size_t i = 0; i < handles.size(); ++i) sync_out.println("Thread ", i, " native handle: ", handles[i]); } ``` @@ -1629,164 +2287,637 @@ int main() The output will depend on your compiler and operating system. Here is an example: ```none -Thread 0 native handle: 000000F4 -Thread 1 native handle: 000000F8 -Thread 2 native handle: 000000EC -Thread 3 native handle: 000000FC +Thread 0 native handle: 00000000000000AC +Thread 1 native handle: 00000000000000B0 +Thread 2 native handle: 00000000000000B4 +Thread 3 native handle: 00000000000000B8 ``` -### Setting task priority - -Defining the macro `BS_THREAD_POOL_ENABLE_PRIORITY` before including `BS_thread_pool.hpp` enables task priority. The priority of a task or group of tasks may then be specified as an additional argument (at the end of the argument list) to `detach_task()`, `submit_task()`, `detach_blocks()`, `submit_blocks()`, `detach_loop()`, `submit_loop()`, `detach_sequence()`, and `submit_sequence()`. If the priority is not specified, the default value will be 0. - -The priority is a number of type `BS::priority_t`, which is a signed 16-bit integer, so it can have any value between -32,768 and 32,767. The tasks will be executed in priority order from highest to lowest. If priority is assigned to the block/loop/sequence parallelization functions, which submit multiple tasks, then all of these tasks will have the same priority. - -The namespace `BS::pr` contains some pre-defined priorities for users who wish to avoid magic numbers and enjoy better future-proofing. In order of decreasing priority, the pre-defined priorities are: `BS::pr::highest`, `BS::pr::high`, `BS::pr::normal`, `BS::pr::low`, and `BS::pr::lowest`. - -Here is a simple example: - -```cpp -#define BS_THREAD_POOL_ENABLE_PRIORITY -#include "BS_thread_pool.hpp" // BS::thread_pool -#include "BS_thread_pool_utils.hpp" // BS::synced_stream - -BS::synced_stream sync_out; -BS::thread_pool pool(1); - -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 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 first."); }, BS::pr::highest); - pool.detach_task([] { sync_out.println("This task will execute fourth."); }, BS::pr::low); -} -``` - -This program will print out the tasks in the correct priority order. Note that for simplicity, we used a pool with just one thread, so the tasks will run one at a time. In a pool with 5 or more threads, all 5 tasks will actually run more or less at the same time, because, for example, the task with the second-highest priority will be picked up by another thread while the task with the highest priority is still running. - -Of course, this is just a pedagogical example. In a realistic use case we may want, for example, to submit tasks that must be completed immediately with high priority so they skip over other tasks already in the queue, or background non-urgent tasks with low priority so they evaluate only after higher-priority tasks are done. - -Here are some subtleties to note when using task priority: - -* Task priority is facilitated using [`std::priority_queue`](https://en.cppreference.com/w/cpp/container/priority_queue), which has O(log n) complexity for storing new tasks, but only O(1) complexity for retrieving the next (i.e. highest-priority) task. This is in contrast with [`std::queue`](https://en.cppreference.com/w/cpp/container/queue), used if priority is disabled, which both stores and retrieves with O(1) complexity. -* Due to this, enabling the priority queue can incur a very slight decrease in performance, depending on the specific use case, which is why this feature is disabled by default. As usual, there is a trade-off here, where you get functionality in exchange for performance. However, the difference in performance is never substantial, and compiler optimizations can often reduce it to a negligible amount. -* When using the priority queue, tasks will not necessarily be executed in the same order they were submitted, **even if they all have the same priority**. This is due to the implementation of `std::priority_queue` as a [binary heap](https://en.wikipedia.org/wiki/Binary_heap), which means tasks are stored as a binary tree instead of sequentially. To execute tasks in submission order, give them monotonically decreasing priorities. -* Technically, `BS::priority_t` is defined to be ([`std::int_least16_t`](https://en.cppreference.com/w/cpp/types/integer)), since this type is guaranteed to be present on all systems, rather than `std::int16_t`, which is optional in the C++ standard. This means that on some exotic systems `BS::priority_t` may actually have more than 16 bits. However, the pre-defined priorities are 100% portable, and will always have the same values (e.g.: `BS::pr::highest = 32767`) regardless of the actual bit width. +**Warning:** Please note that any code written using the native handles directly will **not** be portable. As detailed above, the thread pool's native extensions define abstraction layers for several commonly used thread operations, which are portable on supported platforms, and are therefore strongly preferred over non-portable operations. The native handles are made available for users who need to perform operations that are not covered by these abstraction layers. ## Testing the library ### Automated tests -The file `BS_thread_pool_test.cpp` in the `tests` folder of the GitHub repository will perform automated tests of all aspects of the library. The output will be printed both to `std::cout` and to a file with the same name as the executable and the suffix `-yyyy-mm-dd_hh.mm.ss.log` based on the current date and time. In addition, the code is meant to serve as an extensive example of how to properly use the library. +The file `BS_thread_pool_test.cpp` in the `tests` folder of [the GitHub repository](https://github.com/bshoshany/thread-pool) will perform automated tests of all aspects of the library. In addition, the code is meant to serve as an extensive example of how to properly use the library. -Please make sure to: - -1. [Compile](#compiling-and-compatibility) `BS_thread_pool_test.cpp` with optimization flags enabled (e.g. `-O3` on GCC / Clang or `/O2` on MSVC). -2. Run the test without any other applications, especially multithreaded applications, running in parallel. - -The test program also takes command line arguments for automation purposes: +The test program also takes the following command line arguments: * `help`: Show a help message and exit. Any other arguments will be ignored. -* `log`: Create a log file. +* `stdout`: Print to the standard output. +* `log`: Print to a log file. It will have the same name as the executable, with a suffix `-yyyy-mm-dd_hh.mm.ss.log` based on the current date and time. * `tests`: Perform standard tests. -* `deadlock` Perform long deadlock tests. -* `benchmarks`: Perform benchmarks. +* `deadlock`: Perform long deadlock tests. +* `benchmarks`: Perform full Mandelbrot plot benchmarks. +* `plot`: Perform quick Mandelbrot plot benchmarks. +* `save`: Save the Mandelbrot plot to a file. -If no options are entered, the default is: `log tests benchmarks`. +If no options are entered, the default is `benchmarks log stdout tests`. If the file `default_args.txt` exists in the same folder, the test program reads the default arguments from it (space separated in a single line). Command line arguments can still override these defaults. This is useful when debugging. -By default, the test program enables all the optional features by defining the suitable macros, so it can test them. However, if the macro `BS_THREAD_POOL_LIGHT_TEST` is defined during compilation, the optional features will not be tested. +The following macros can be defined during compilation (using the `-D` flag in Clang and GCC or `/D` in MSVC) to enable additional features: -A PowerShell script, `BS_thread_pool_test.ps1`, is provided for your convenience in the `tests` folder to make running the test on multiple compilers and operating systems easier. Since it is written in PowerShell, it is fully portable and works on Windows, Linux, and macOS. The script will automatically detect if Clang, GCC, and/or MSVC are available, and compile the test program using each available compiler twice - with and without all the optional features. It will then run each compiled test program and report on any errors. +* `BS_THREAD_POOL_TEST_IMPORT_MODULE`: Import the thread pool library [as a C++20 module](#importing-the-library-as-a-c20-module). Note that the module must be compiled beforehand, as explained in the relevant section. +* `BS_THREAD_POOL_NATIVE_EXTENSIONS`: Test the [native extensions](#native-extensions). If importing the library as a C++20 module, ensure that the library was compiled with the same macro. -If any of the tests fail, please [submit a bug report](https://github.com/bshoshany/thread-pool/issues) including the exact specifications of your system (OS, CPU, compiler, etc.) and the generated log file. +A Python script, `test_all.py`, is provided for convenience in the `scripts` folder. This script makes use of the bundled [`compile_cpp.py` script](#the-compile_cpppy-script), and requires Python 3.12 or later. The script will automatically detect if Clang, GCC, and/or MSVC are available, and compile and run the test program using each available compiler 3 times: + +1. With C++17 support. +2. With C++20 support, using `import BS.thread_pool`. +3. With C++23 support, using `import BS.thread_pool`, and using `import std` on supported compilers. + +If any of the tests fail, please [submit a bug report](https://github.com/bshoshany/thread-pool/issues) including the exact specifications of your system (OS, CPU, compiler, etc.) and the generated log file. However, please note that only the latest versions of each compiler are supported. ### Performance tests -If all checks passed, `BS_thread_pool_test.cpp` performs simple benchmarks by filling a very large vector with values using `detach_blocks()`. The program decides what the size of the vector should be by testing how many elements are needed to reach a certain target duration when parallelizing using a number of blocks equal to the number of threads. This ensures that the test takes approximately the same amount of time on all systems, and is thus more consistent and portable. +`BS_thread_pool_test.cpp` also performs benchmarks, using a highly-optimized multithreaded algorithm which generates a plot of the [Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set), utilizing a normalized iteration count algorithm and linear interpolation to create smooth coloring. If tests are enabled, the benchmarks will only be performed if all of the tests pass. -Once the appropriate size of the vector has been determined, the program allocates the vector and fills it with values, calculated according to a fixed prescription. This operation is performed both single-threaded and multithreaded, with the multithreaded computation spread across multiple tasks submitted to the pool. +These benchmarks are heavily CPU-intensive, which results in a high speedup factor due to multithreading, ideally utilizing every core and thread to their fullest extent. This makes them useful for optimizing the library, since they are more sensitive to the thread pool's own performance than to other factors such as memory or cache. -Several different multithreaded tests are performed, with the number of tasks either equal to, smaller than, or larger than the pool's thread count. Each test is repeated multiple times, with the run times averaged over all runs of the same test. The program keeps increasing the number of blocks by a factor of 2 until diminishing returns are encountered. The run times of the tests are compared, and the maximum speedup obtained is calculated. +The full benchmarks are enabled using the command line argument `benchmarks`, which is enabled by default. The command line argument `plot` can be used to just plot the Mandelbrot set once, either instead of or in addition to doing the full benchmarks. This will plot the largest possible image that can be plotted in 5 seconds, and only measure the performance in pixels/ms for the entire plot. -As an example, here are the results of the benchmarks from a [Digital Research Alliance of Canada](https://alliancecan.ca/en) node equipped with two 20-core / 40-thread Intel Xeon Gold 6148 CPUs (for a total of 40 cores and 80 threads), running CentOS Linux 7.9.2009. The tests were compiled using GCC v13.2.0 with the `-O3` and `-march=native` flags. The output was as follows: +If you want to see the actual plot, pass the `save` command line argument. The plot is saved to a BMP file, to avoid having to depend on 3rd-party libraries. This is off by default, since that file can get quite large. + +The program determines the optimal resolution of the Mandelbrot plot by testing how many pixels are needed to reach a certain target duration when parallelizing the loop using a number of tasks equal to the number of threads. This ensures that the benchmarks take approximately the same amount of time (per thread) on all systems, and are thus more consistent and portable. + +Once the appropriate resolution has been determined, the program plots the Mandelbrot set. For more details about the algorithm used, please see the source code for `BS_thread_pool_test.cpp`. This operation is performed both single-threaded and multithreaded, with the multithreaded computation spread across multiple tasks submitted to the pool. + +Multithreaded tests are performed with increasingly higher task counts, while keeping the number of threads in the pool equal to the hardware concurrency for optimal performance. Each test is repeated multiple times, with the run times averaged over all runs of the same test. The program keeps increasing the number of tasks by a factor of 2 until diminishing returns are encountered. The run times of the tests are compared, and the maximum speedup obtained compared to the single-threaded test is calculated. + +If the [native extensions](#native-extensions) are enabled, the program will try to increase the priority of both the process itself and all the threads in the pool to the highest possible value, to prevent other processes from interfering with the benchmarks. Therefore, to obtain the most reliable benchmarks, it is recommended to run the tests as a privileged user, especially on Linux or macOS where only root can increase the priority. + +As an example, here are the results of the benchmarks running on a 24-core (8P+16E) / 32-thread Intel i9-13900K CPU. The tests were compiled using MSVC in C++23 mode, to obtain maximum performance using the latest C++23 features. Compiler optimizations were enabled using the `/O2` flag. The benchmarks were run 5 times, and the result with the median speedup was as follows: ```none -====================== -Performing benchmarks: -====================== -Using 80 threads. -Determining the number of elements to generate in order to achieve an approximate mean execution time of 50 ms with 80 tasks... -Each test will be repeated up to 30 times to collect reliable statistics. -Generating 27962000 elements: -[......] -Single-threaded, mean execution time was 2815.2 ms with standard deviation 3.5 ms. -[......] -With 2 tasks, mean execution time was 1431.3 ms with standard deviation 10.1 ms. -[.......] -With 4 tasks, mean execution time was 722.1 ms with standard deviation 11.4 ms. -[..............] -With 8 tasks, mean execution time was 364.9 ms with standard deviation 10.9 ms. -[............................] -With 16 tasks, mean execution time was 181.9 ms with standard deviation 8.0 ms. -[..............................] -With 32 tasks, mean execution time was 110.6 ms with standard deviation 1.8 ms. -[..............................] -With 64 tasks, mean execution time was 64.0 ms with standard deviation 6.3 ms. -[..............................] -With 128 tasks, mean execution time was 59.8 ms with standard deviation 0.8 ms. -[..............................] -With 256 tasks, mean execution time was 59.0 ms with standard deviation 0.0 ms. -[..............................] -With 512 tasks, mean execution time was 52.8 ms with standard deviation 0.4 ms. -[..............................] -With 1024 tasks, mean execution time was 50.7 ms with standard deviation 0.9 ms. -[..............................] -With 2048 tasks, mean execution time was 50.0 ms with standard deviation 0.5 ms. -[..............................] -With 4096 tasks, mean execution time was 49.4 ms with standard deviation 0.5 ms. -[..............................] -With 8192 tasks, mean execution time was 50.2 ms with standard deviation 0.4 ms. -Maximum speedup obtained by multithreading vs. single-threading: 56.9x, using 4096 tasks. - -+++++++++++++++++++++++++++++++++++++++ -Thread pool performance test completed! -+++++++++++++++++++++++++++++++++++++++ +Generating a 3965x3965 plot of the Mandelbrot set... +Each test will be repeated 30 times to collect reliable statistics. + 1 task: [..............................] (single-threaded) +-> Mean: 510.5 ms, standard deviation: 0.5 ms, speed: 1026.5 pixels/ms. + 8 tasks: [..............................] +-> Mean: 149.1 ms, standard deviation: 0.6 ms, speed: 3514.7 pixels/ms. + 16 tasks: [..............................] +-> Mean: 85.4 ms, standard deviation: 2.5 ms, speed: 6133.9 pixels/ms. + 32 tasks: [..............................] +-> Mean: 48.3 ms, standard deviation: 1.8 ms, speed: 10849.7 pixels/ms. + 64 tasks: [..............................] +-> Mean: 29.1 ms, standard deviation: 1.0 ms, speed: 17987.7 pixels/ms. + 128 tasks: [..............................] +-> Mean: 23.6 ms, standard deviation: 0.7 ms, speed: 22173.8 pixels/ms. + 256 tasks: [..............................] +-> Mean: 22.5 ms, standard deviation: 0.6 ms, speed: 23325.3 pixels/ms. + 512 tasks: [..............................] +-> Mean: 21.8 ms, standard deviation: 0.5 ms, speed: 24075.4 pixels/ms. +1024 tasks: [..............................] +-> Mean: 21.9 ms, standard deviation: 0.7 ms, speed: 23892.4 pixels/ms. +Maximum speedup obtained by multithreading vs. single-threading: 23.5x, using 512 tasks. ``` -These two CPUs have 40 physical cores in total, with each core providing two separate logical cores via hyperthreading, for a total of 80 threads. Without hyperthreading, we would expect a maximum theoretical speedup of 40x. With hyperthreading, one might naively expect to achieve up to an 80x speedup, but this is in fact impossible, as each pair of hyperthreaded logical cores share the same physical core's resources. However, generally we would expect at most an estimated 30% additional speedup from hyperthreading, which amounts to around 52x in this case. The speedup of 56.9x in our performance test exceeds this estimate. +This CPU has 24 cores, of which 8 are fast (5.40 GHz max) performance cores with hyperthreading (thus providing 16 threads in total), and 16 are slower (4.30 GHz max) efficiency cores without hyperthreading, for a total of 32 threads. + +Due to the hybrid architecture, it is not trivial to calculate the theoretical maximum speedup. However, we can get a rough estimate by noticing that the E-cores are about 20% slower than the P-cores, and that hyperthreading is generally known to provide around a 30% speedup. Thus, the estimated theoretical speedup (compared to a single P-core) is 8 × 1.3 + 16 × 0.8 = 23.2x. + +The actual median speedup obtained, 23.5x, is slightly above this estimate, which indicates that the thread pool provides optimal performance and allows the Mandelbrot plot algorithm to take full advantage of the CPU's capabilities. + +It should also be noted that even though the available number of hardware threads is 32, the maximum possible speedup is achieved not with 32 tasks, but with 512 tasks - half the square of the number of hardware threads. The reason for this is that splitting the job into more tasks than threads eliminates thread idle time, as explained [above](#optimizing-the-number-of-blocks). However, at 1024 tasks we encounter diminishing returns, as the overhead of submitting the tasks to the pool starts to outweigh the benefits of parallelization. + +### Finding the version of the library + +Starting with v5.0.0, the thread pool library defines the `constexpr` object `BS::thread_pool_version`, which can be used to check the version of the library at compilation time. This object is of type `BS::version`, with members `major`, `minor`, and `patch`, and all comparison operators defined as `constexpr`. It also has a `to_string()` member function and an `operator<<` overload for easy printing at runtime. + +Since `BS::thread_pool_version` is a `constexpr` object, it can be used in any context where a `constexpr` object is allowed, such as `static_assert()` and `if constexpr`. For example, the following program will fail to compile if the version is not 5.1.0 or higher: + +```cpp +#include "BS_thread_pool.hpp" + +static_assert(BS::thread_pool_version >= BS::version(5, 1, 0), "This program requires version 5.1.0 or later of the BS::thread_pool library."); + +int main() +{ + // ... +} +``` + +As another example, the following program will print the version of the library (this will implicitly use the `<<` operator of `BS::version`) and then conditionally compile one of two branches of code depending on the version of the library: + +```cpp +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool + +BS::synced_stream sync_out; +BS::thread_pool pool; + +int main() +{ + sync_out.println("Detected BS::thread_pool v", BS::thread_pool_version, '.'); + if constexpr (BS::thread_pool_version <= BS::version(5, 1, 0)) + { + // Do something supported by BS::thread_pool v5.1.0 or earlier. + } + else + { + // Do something supported by newer versions of BS::thread_pool after v5.1.0. + } +} +``` + +Currently, both the examples above are of pedagogical value only, because `BS::thread_pool_version` was only introduced in v5.0.0, and that is also the latest version at the time of writing, so there are no other versions to compare to. However, once future versions of the library are released, this object will be the preferred way to do version checking. + +For backwards compatibility, if you are not sure if you are going to get v4 or v5 of the library, you can check the version using the following preprocessor macros, which were introduced in v4.0.1: + +* `BS_THREAD_POOL_VERSION_MAJOR` - indicates the major version. +* `BS_THREAD_POOL_VERSION_MINOR` - indicates the minor version. +* `BS_THREAD_POOL_VERSION_PATCH` - indicates the patch version. + +These macros allow for conditional inclusion of code using `#if` directives. As an example, the member function [`set_cleanup_func()`](#thread-cleanup-functions) was introduced in v5.0.0. Therefore, if the major version number is 5 or higher, we can use this function; otherwise, we must find some other way to do the cleanup: + + ```cpp +#include "BS_thread_pool.hpp" // BS::synced_stream, BS::thread_pool + +BS::synced_stream sync_out; +BS::thread_pool pool; + +int main() +{ +#if BS_THREAD_POOL_VERSION_MAJOR >= 5 + pool.set_cleanup_func( + [] + { + sync_out.println("Doing cleanup..."); + }); +#else + // Do the cleanup in some other way. +#endif +} +``` + +However, please note that if the library is imported [as a C++20 module](#importing-the-library-as-a-c20-module), these macros will not be available, since macros cannot be exported from a module. In this case, you must use `BS::thread_pool_version` instead. (Indeed, this is exactly why it was introduced in the first place.) + +## Importing the library as a C++20 module + +### Compiling the module + +If C++20 features are available, the library can be imported as a C++20 module using `import BS.thread_pool`. This is the officially recommended way to use the library, as it has many benefits, such as faster compilation times, better encapsulation, no namespace pollution, no include order issues, easier maintainability, simpler dependency management, and more. The `constexpr` flag `BS::thread_pool_module` indicates whether the thread pool library was compiled as a module. For more information on C++20 modules, please see [cppreference.com](https://en.cppreference.com/w/cpp/language/modules). + +The module file itself is `BS.thread_pool.cppm`, located in the `modules` folder, and it is just a thin wrapper around the header file `BS_thread_pool.hpp`. The C++20 standard does not provide a way for one file to be used as both a module and a header file, so both files are needed in order to compile the library as a module. (However, to use the library as a header file, only `BS_thread_pool.hpp` is needed.) + +Note that the header file `BS_thread_pool.hpp` has an underscore `_` following `BS`, for backwards compatibility with older versions of the library. However, the module file `BS.thread_pool.cppm` has a dot `.` following `BS`, to conform with the C++20 module naming convention, where dots represent hierarchy; all modules written by the author of this library will use the `BS.` prefix. + +This feature has been tested with the latest versions of Clang, GCC, and MSVC. Unfortunately, at the time of writing, C++20 modules are still not fully implemented in all compilers, and each compiler implements them differently. + +The easiest way to compile the module itself, as well as any programs that import it, is using the `compile_cpp.py` Python script provided in [the GitHub repository](https://github.com/bshoshany/thread-pool), which will automatically figure out the appropriate flags for each compiler. Please see the [next section](#compiling-with-compile_cpppy-using-import-bsthread_pool) for more information. + +However, if you prefer to compile manually, the module must first be compiled into a binary file, in a format specific to each compiler, as described in the following sections. Once it is compiled once and for all, this binary file (plus an object file, in MSVC) is the only file needed to import the library; the `.cppm` and `.hpp` files are no longer needed. However, any program using the module must be compiled with a flag indicating to the compiler where to find that binary file. + +Once the module is compiled, it can be imported using `import BS.thread_pool`. In all the examples above, you can simply replace `#include "BS_thread_pool.hpp"` with `import BS.thread_pool;` in order to import the library as a module. The only exception is the [native extensions](#native-extensions), which are enabled in the examples using a macro; as explained in that section, the macro must be defined as a compiler flag, as modules cannot access macros defined in the program that imported them. + +Here is a quick example: + +```cpp +import BS.thread_pool; + +BS::synced_stream sync_out; +BS::thread_pool pool; + +int main() +{ + pool.submit_task( + [] + { + sync_out.println("Thread pool library successfully imported using C++20 modules!"); + }) + .wait(); +} +``` + +Below we will provide the commands for compiling the library as a module and then compiling [the test program](#testing-the-library) `BS_thread_pool_test.cpp` using this module, with Clang, GCC, and MSVC, as well as with CMake. In [the GitHub repository](https://github.com/bshoshany/thread-pool), the relevant files are organized as follows: + +``` +├── README.md <- this documentation file +├── include +│ └── BS_thread_pool.hpp <- the header file +├── modules +│ └── BS.thread_pool.cppm <- the module file +├── tasks +│ └── compile_cpp.py <- the compile script (optional) +└── tests + └── BS_thread_pool_test.cpp <- the test program +``` + +In the following examples, it is assumed that the commands are executed in the root directory of the repository (the one that contains `README.md`). The compiled files will be placed in a `build` subdirectory, which should be created beforehand. + +### Compiling with `compile_cpp.py` using `import BS.thread_pool` + +The bundled Python script [`compile_cpp.py`](#the-compile_cpppy-script) can be used to easily compile any programs that import the library as a module. The script will automatically figure out the appropriate flags for each compiler, so you do not have to worry about the details. For example, to compile the test program `BS_thread_pool_test.cpp` and have it import the `BS.thread_pool` module, simply run the following command in the root folder of the repository: + +```bash +python scripts/compile_cpp.py tests/BS_thread_pool_test.cpp -s=c++20 -i=include -t=release -m="BS.thread_pool=modules/BS.thread_pool.cppm,include/BS_thread_pool.hpp" -o=build/BS_thread_pool_test -d=BS_THREAD_POOL_TEST_IMPORT_MODULE -v +``` + +Please see [below](#the-compile_cpppy-script) for an explanation of the command line arguments. The `-d` argument defines the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE`, which is used to indicate to the test program that it needs to import the library as a module instead of including the header file. **Note that this macro is only used by the test program; it is not needed when you compile your own programs.** To enable the [native extensions](#native-extensions), you should also add `-d=BS_THREAD_POOL_NATIVE_EXTENSIONS` to define the required macro. To use C++23, replace `-s=c++20` with `-s=c++23`. + +Since we used `-t=release`, optimization flags will be added automatically. If you now type `build/BS_thread_pool_test`, the test program will run; you can also add the argument `-r` to run it automatically after compilation. If the module was successfully imported, the test program will print the message: + +```none +Thread pool library imported using: import BS.thread_pool (C++20 modules). +``` + +For further customization, it is recommend to create a `compile_cpp.yaml` file as explained [below](#the-compile_cpppy-script). + +### Compiling with Clang using `import BS.thread_pool` + +Note: The following instructions have only been tested using Clang v19.1.6, the latest version at the time of writing, and may not work with older versions of the compiler. + +To compile the module file `BS.thread_pool.cppm` with Clang, first create the `build` folder using `mkdir build`, and then run the following command in the root folder of the repository: + +```bash +clang++ modules/BS.thread_pool.cppm --precompile -std=c++20 -I include -o build/BS.thread_pool.pcm +``` + +Here is a breakdown of the compiler arguments: + +* `modules/BS.thread_pool.cppm`: The module file to compile. Note that it will include the file `include/BS_thread_pool.hpp` automatically. +* `--precompile`: Do not run the linker, only compile the module. +* `-std=c++20`: Use the C++20 standard. For C++23, use `-std=c++23`. +* `-I include`: Add the `include` folder to the include path, so that the module can find the header file `BS_thread_pool.hpp`. +* `-o build/BS.thread_pool.pcm`: Output the compiled module to `build/BS.thread_pool.pcm`. The extension `.pcm` is used by Clang for precompiled modules. + +Note that to enable the [native extensions](#native-extensions), you should add `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` to define the required macro. + +Once the module is compiled, you can compile the test program as follows: + +```bash +clang++ tests/BS_thread_pool_test.cpp -fmodule-file="BS.thread_pool=build/BS.thread_pool.pcm" -std=c++20 -o build/BS_thread_pool_test -D BS_THREAD_POOL_TEST_IMPORT_MODULE +``` + +Here is a breakdown of the compiler arguments: + +* `tests/BS_thread_pool_test.cpp`: The program to compile. +* `-fmodule-file="BS.thread_pool=build/BS.thread_pool.pcm"`: Specify that the module `BS.thread_pool` is located in the file `build/BS.thread_pool.pcm`. +* `-std=c++20`: Same as above. +* `-o build/BS_thread_pool_test`: Output the compiled program to `build/BS_thread_pool_test` (or `build/BS_thread_pool_test.exe` on Windows). +* `-D BS_THREAD_POOL_TEST_IMPORT_MODULE`: Define the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE`, which is used to indicate to the test program that it needs to import the library as a module instead of including the header file. **Note that this macro is only used by the test program; it is not needed when you compile your own programs.** + +Again, you should add `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to test the native extensions. You do not need to use the `-I` flag, since the header file is not needed, only the `.pcm` file. If you now type `build/BS_thread_pool_test`, the test program will run. If the module was successfully imported, the test program will print the message: + +```none +Thread pool library imported using: import BS.thread_pool (C++20 modules). +``` + +Of course, you should add warning, debugging, optimization, and other compiler flags to the commands above as needed. For more information about using C++20 modules with Clang, please see [the official documentation](https://clang.llvm.org/docs/StandardCPlusPlusModules.html). + +**Note:** On macOS, Apple Clang v16.0.0 (the latest version at the time of writing) does not support C++20 modules. Please either install the latest version of LLVM Clang using [Homebrew](https://formulae.brew.sh/formula/llvm), or include the library as a header file. + +### Compiling with GCC using `import BS.thread_pool` + +Note: The following instructions have only been tested using GCC v14.2.0, the latest version at the time of writing, and may not work with older versions of the compiler. + +To compile the module file `BS.thread_pool.cppm` with GCC, first create the `build` folder using `mkdir build`, and then run the following command in the root folder of the repository: + +```bash +g++ -x c++ modules/BS.thread_pool.cppm -c "-fmodule-mapper=|@g++-mapper-server -r build" -fmodule-only -fmodules-ts -std=c++20 -I include +``` + +Here is a breakdown of the compiler arguments: + +* `-x c++`: Treat the input file as a C++ file. This is necessary because the file has the `.cppm` extension, which is not recognized by GCC. +* `modules/BS.thread_pool.cppm`: The module file to compile. Note that it will include the file `include/BS_thread_pool.hpp` automatically. +* `-c`: Do not run the linker, only compile the module. +* `"-fmodule-mapper=|@g++-mapper-server -r build"`: Specify to the module mapper that the compiled module should be placed in the `build` folder. This will create a file `build/BS.thread_pool.gcm`. The extension `.gcm` is used by GCC for compiled modules. +* `-fmodule-only`: Do not create an object file for the module. +* `-fmodules-ts`: Enable C++20 modules. +* `-std=c++20`: Use the C++20 standard. For C++23, use `-std=c++23`. +* `-I include`: Add the `include` folder to the include path, so that the module can find the header file `BS_thread_pool.hpp`. + +Note that to enable the [native extensions](#native-extensions), you should add `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` to define the required macro. + +Once the module is compiled, you can compile the test program as follows: + +```bash +g++ tests/BS_thread_pool_test.cpp "-fmodule-mapper=|@g++-mapper-server -r build" -fmodules-ts -std=c++20 -o build/BS_thread_pool_test -D BS_THREAD_POOL_TEST_IMPORT_MODULE +``` + +Here is a breakdown of the compiler arguments: + +* `tests/BS_thread_pool_test.cpp`: The program to compile. +* `"-fmodule-mapper=|@g++-mapper-server -r build"`: Specify to the module mapper that the compiled module can be found in the `build` folder. It will look for the file `build/BS.thread_pool.gcm`. +* `-fmodules-ts`, `-std=c++20`: Same as above. +* `-o build/BS_thread_pool_test`: Output the compiled program to `build/BS_thread_pool_test` (or `build/BS_thread_pool_test.exe` on Windows). +* `-D BS_THREAD_POOL_TEST_IMPORT_MODULE`: Define the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE`, which is used to indicate to the test program that it needs to import the library as a module instead of including the header file. **Note that this macro is only used by the test program; it is not needed when you compile your own programs.** + +Again, you should add `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to test the native extensions. You do not need to use the `-I` flag, since the header file is not needed, only the `.gcm` file. If you now type `build/BS_thread_pool_test`, the test program will run. If the module was successfully imported, the test program will print the message: + +```none +Thread pool library imported using: import BS.thread_pool (C++20 modules). +``` + +Of course, you should add warning, debugging, optimization, and other compiler flags to the commands above as needed. For more information about using C++20 modules with GCC, please see [the official documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Modules.html). + +**Note:** GCC v14.2.0 (latest version at the time of writing) appears to have an internal compiler error when compiling programs containing modules (or at least, this particular module) with any optimization flags other than `-Og` enabled. Until this is fixed, if you wish to use compiler optimizations, please either include the library as a header file or use a different compiler. + +### Compiling with MSVC using `import BS.thread_pool` + +Note: The following instructions have only been tested using MSVC v19.42.34435, the latest version at the time of writing, and may not work with older versions of the compiler. + +To compile the module file `BS.thread_pool.cppm` with MSVC, first open the Visual Studio Developer PowerShell for the appropriate CPU architecture. For example, for x64, execute the following command in PowerShell: + +```pwsh +& 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' -Arch amd64 -HostArch amd64 +``` + +For ARM64, replace `amd64` with `arm64`. (Do not use the "Developer PowerShell for VS 2022" Start Menu shortcut, as it may not use the correct CPU architecture by default.) + +Navigate to the repository folder, create the `build` folder using `mkdir build`, and then run the following command in the root folder of the repository: + +```pwsh +cl modules/BS.thread_pool.cppm /c /EHsc /interface /nologo /permissive- /std:c++20 /TP /Zc:__cplusplus /I include /ifcOutput build/BS.thread_pool.ifc /Fo:build/BS.thread_pool.obj +``` + +Here is a breakdown of the compiler arguments: + +* `modules/BS.thread_pool.cppm`: The module file to compile. Note that it will include the file `include/BS_thread_pool.hpp` automatically. +* `/c`: Do not run the linker, only compile the module. +* `/EHsc`: Enable C++ exceptions. +* `/interface`: Treat the input file as a module interface unit. This is needed because MSVC expects the `.ixx` extension for the module file, but this library uses the `.cppm` extension. +* `/nologo`: Do not display the compiler's banner. +* `/permissive-`: Disable permissive behaviors, that is, enforce strict C++ standard conformance. +* `/std:c++20`: Use the C++20 standard. For C++23, use `/std:c++latest`. +* `/TP`: Treat the input file as a C++ file. This is necessary because the file has the `.cppm` extension, which is not recognized by MSVC. +* `/Zc:__cplusplus`: Make the `__cplusplus` preprocessor macro correctly reflect the C++ standard being used. +* `/I include`: Add the `include` folder to the include path, so that the module can find the header file `BS_thread_pool.hpp`. +* `/ifcOutput build/BS.thread_pool.ifc`: Output the compiled module to `build/BS.thread_pool.ifc`. The extension `.ifc` is used by MSVC for module interface files. +* `/Fo:build/BS.thread_pool.obj`: Output the compiled object file to `build/BS.thread_pool.obj`. + +Note that to enable the [native extensions](#native-extensions), you should add `/D BS_THREAD_POOL_NATIVE_EXTENSIONS` to define the required macro. + +Once the module is compiled, you can compile the test program as follows: + +```pwsh +cl tests/BS_thread_pool_test.cpp build/BS.thread_pool.obj /reference BS.thread_pool=build/BS.thread_pool.ifc /EHsc /nologo /permissive- /std:c++20 /Zc:__cplusplus /Fo:build/BS_thread_pool_test.obj /Fe:build/BS_thread_pool_test.exe /D BS_THREAD_POOL_TEST_IMPORT_MODULE +``` + +Here is a breakdown of the compiler arguments: + +* `tests/BS_thread_pool_test.cpp`: The program to compile. +* `build/BS.thread_pool.obj`: The module object file to link to the program. +* `/reference BS.thread_pool=build/BS.thread_pool.ifc`: Specify that the module `BS.thread_pool` is located in the file `build/BS.thread_pool.ifc`. +* `/EHsc`, `/nologo`, `/permissive-`, `/std:c++20`, `/Zc:__cplusplus`: Same as above. +* `/Fo:build/BS_thread_pool_test.obj`: Output the compiled object file to `build/BS_thread_pool_test.obj`. +* `/Fe:build/BS_thread_pool_test.exe`: Output the compiled program to `build/BS_thread_pool_test.exe`. +* `/D BS_THREAD_POOL_TEST_IMPORT_MODULE`: Define the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE`, which is used to indicate to the test program that it needs to import the library as a module instead of including the header file. **Note that this macro is only used by the test program; it is not needed when you compile your own programs.** + +Again, you should add `/D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to test the native extensions. You do not need to use the `/I` flag, since the header file is not needed, only the `.obj` and `.ifc` files. If you now type `build/BS_thread_pool_test`, the test program will run. If the module was successfully imported, the test program will print the message: + +```none +Thread pool library imported using: import BS.thread_pool (C++20 modules). +``` + +Of course, you should add warning, debugging, optimization, and other compiler flags to the commands above as needed. For more information about using C++20 modules with MSVC, please see [this blog post](https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/). + +### Compiling with CMake using `import BS.thread_pool` + +Note: The following instructions have only been tested using CMake v3.31.2, the latest version at the time of writing, and may not work with older versions. Also, modules are currently only supported by CMake with the [`Ninja`](https://ninja-build.org/) and `Visual Studio 17 2022` generators. + +If you are using [CMake](https://cmake.org/), you can use `target_sources()` with `CXX_MODULES` to include the module file `BS.thread_pool.cppm`. CMake will then automatically compile the module and link it to your program. Here is an example of a `CMakeLists.txt` file that can be used to build the test program and import the thread pool library as a module: + +```cmake +cmake_minimum_required(VERSION 3.31) +project(BS_thread_pool_test LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +if(MSVC) + add_compile_options(/permissive- /Zc:__cplusplus) +endif() + +add_library(BS_thread_pool) +target_sources(BS_thread_pool PRIVATE FILE_SET CXX_MODULES FILES modules/BS.thread_pool.cppm) +target_include_directories(BS_thread_pool PRIVATE include) + +add_executable(${PROJECT_NAME} tests/BS_thread_pool_test.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE BS_thread_pool) +target_compile_definitions(${PROJECT_NAME} PRIVATE BS_THREAD_POOL_TEST_IMPORT_MODULE) +``` + +Note that for MSVC we have to add the `/permissive-` flag to enforce strict C++ standard conformance, otherwise the test program will not compile, and `/Zc:__cplusplus`, otherwise the test program cannot detect the correct C++ version. This is handled automatically by the `if(MSVC)` block. + +To enable the [native extensions](#native-extensions), add the line `add_compile_definitions(BS_THREAD_POOL_NATIVE_EXTENSIONS)`. Replace `CMAKE_CXX_STANDARD 20` with `23` if you wish to use C++23 features. + +Place this file in the root folder of the repository, and then run the following commands: + +```bash +cmake -B build +cmake --build build +build/BS_thread_pool_test +``` + +For MSVC, replace the last command with `build/Debug/BS_thread_pool_test`. If the module was successfully imported, the test program will print the message: + +```none +Thread pool library imported using: import BS.thread_pool (C++20 modules). +``` + +Of course, you should add warning, debugging, optimization, and other compiler flags to the configuration above as needed. For more information about using C++20 modules with CMake, please see [the official documentation](https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html). + +You can also instruct CMake to download the library automatically from the GitHub repository, as explained below, either using [CPM](#installing-using-cmake-with-cpm) or [`FetchContent`](#installing-using-cmake-with-fetchcontent). + +## Importing the C++23 Standard Library as a module + +### Enabling `import std` + +If C++23 features are available, the thread pool library can import the C++ Standard Library as a module using `import std`. This has the same benefits described [above](#importing-the-library-as-a-c20-module) for importing the library as a module, such as faster compilation times. To enable this feature, define the macro `BS_THREAD_POOL_IMPORT_STD` at compilation time. + +At the time of writing, importing the C++ Standard Library as a module is only officially supported by the following combinations of compilers and standard libraries: + +* Recent versions of MSVC with Microsoft STL. +* Recent versions of LLVM Clang (**not** Apple Clang) with LLVM libc++. + +It is not supported by GCC with any standard library, Clang with any standard library other than libc++, any compiler with GNU libstdc++, or any other compiler or standard library. + +If `BS_THREAD_POOL_IMPORT_STD` is defined, then you must also import the thread pool library itself as a module. If the library is included as a header file, this will force the program that included the header file to also import `std`, which is not desirable and can lead to compilation errors if the program `#include`s any Standard Library header files. + +Defining the macro before importing the module will not work, as modules cannot access macros defined in the program that imported them. Instead, you must define the macro as a compiler flag: `-D BS_THREAD_POOL_IMPORT_STD` for Clang and GCC or `/D BS_THREAD_POOL_IMPORT_STD` for MSVC. + +[The test program](#testing-the-library) will also import the `std` module if the macro `BS_THREAD_POOL_IMPORT_STD` is defined at compilation time. In that case, you should also enable the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE` to import the thread pool library as a module. + +The `constexpr` flag `BS::thread_pool_import_std` indicates whether the thread pool library was compiled with `import std`. Note that the flag will be `false` if `BS_THREAD_POOL_IMPORT_STD` is defined but the compiler or standard library does not support importing the C++ Standard Library as a module. + +At the time of writing, importing the `std` module requires compiling it first. As explained in the [previous section](#importing-the-library-as-a-c20-module), using the bundled `compile_cpp.py` script is the easiest way to do this, as we show in the [next section](#compiling-with-compile_cpppy-using-import-std). However, for those who wish to compile manually, in the following sections we will explain how to do it with both Clang and MSVC, as well as with CMake. It is assumed that the reader has already read the section about importing the `BS.thread_pool` library as a module, so we omit some details here. + +### Compiling with `compile_cpp.py` using `import std` + +The bundled Python script [`compile_cpp.py`](#the-compile_cpppy-script) can be used to easily compile any programs that import the C++ Standard Library as a module. The script will automatically figure out the appropriate flags for each compiler, so you do not have to worry about the details. For example, to compile the test program `BS_thread_pool_test.cpp` and have it import both the `BS.thread_pool` module and the `std` module, simply run the following command in the root folder of the repository: + +```bash +python scripts/compile_cpp.py tests/BS_thread_pool_test.cpp -s=c++23 -i=include -t=release -m="BS.thread_pool=modules/BS.thread_pool.cppm,include/BS_thread_pool.hpp" -o=build/BS_thread_pool_test -d=BS_THREAD_POOL_TEST_IMPORT_MODULE -d=BS_THREAD_POOL_IMPORT_STD -u=auto -v +``` + +Please see [below](#the-compile_cpppy-script) for an explanation of the command line arguments. The differences between this command and the one we used for [importing the thread pool library as a module](#compiling-with-compile_cpppy-using-import-bsthread_pool) are: + +* Changed `-s=c++20` to `-s=c++23` so we can use the C++23 standard. +* Added `-d=BS_THREAD_POOL_IMPORT_STD` to define the required macro. +* Added `-u=auto` to automatically detect the location of the `std` module. If this doesn't work, you will need to specify the path manually. + +To enable the [native extensions](#native-extensions), you should also add `-d=BS_THREAD_POOL_NATIVE_EXTENSIONS` to define the required macro. If you now type `build/BS_thread_pool_test`, the test program will run. If the `std` module was successfully imported, the test program will print the message: + +```none +C++ Standard Library imported using: +* Thread pool library: import std (C++23 std module). +* Test program: import std (C++23 std module). +``` + +For further customization, it is recommend to create a `compile_cpp.yaml` file as explained [below](#the-compile_cpppy-script). + +### Compiling with Clang and LLVM libc++ using `import std` + +Note: The following instructions have only been tested using Clang v19.1.6 and LLVM libc++ v19.1.6, the latest versions at the time of writing, and may not work with older versions. + +Before compiling the `std` module, you must find the file `std.cppm`: + +* On Windows, libc++ is most likely installed via [MSYS2](https://www.msys2.org/), so the `std` module should be at `C:\msys64\clang64\share\libc++\v1\std.cppm`. If you did not install MSYS2 in `C:\msys64`, replace that with the correct path. If you installed libc++ without MSYS2, locate `std.cppm` manually in the installation folder. +* On Linux, the `std` module should be at `/usr/lib/llvm-/share/libc++/v1/std.cppm`. Replace `` with the major version number of libc++, e.g. `19`. If you installed libc++ in a different folder, locate `std.cppm` manually in that folder. +* On macOS, the `std` module should be at `/usr/local/Cellar/llvm//share/libc++/v1/std.cppm`. Replace `` with the full version number of libc++, e.g. `19.1.6`. If you installed libc++ in a different folder, locate `std.cppm` manually in that folder. + +To compile the module file `std.cppm` with Clang, first create the `build` folder using `mkdir build`, and then run the following command in the root folder of the repository: + +```bash +clang++ "path to std.cppm" --precompile -std=c++23 -o build/std.pcm -Wno-reserved-module-identifier +``` + +Of course, you should replace `"path to std.cppm"` with the actual path. The compiler arguments are explained [above](#compiling-with-clang-using-import-bsthread_pool). The additional argument `-Wno-reserved-module-identifier` is needed to silence a false-positive warning. + +Next, compile the `BS.thread_pool` module as [above](#compiling-with-clang-using-import-bsthread_pool), but with the following additional flags: + +* `-fmodule-file="std=build/std.pcm"`: Specify that the module `std` is located in the file `build/std.pcm`. +* `-D BS_THREAD_POOL_IMPORT_STD`: Instruct the library to import the `std` module. + +```bash +clang++ modules/BS.thread_pool.cppm --precompile -fmodule-file="std=build/std.pcm" -std=c++23 -I include -o build/BS.thread_pool.pcm -D BS_THREAD_POOL_IMPORT_STD +``` + +Add `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to enable the [native extensions](#native-extensions). Once the module is compiled, you can compile the test program as follows: + +```bash +clang++ tests/BS_thread_pool_test.cpp -fmodule-file="std=build/std.pcm" -fmodule-file="BS.thread_pool=build/BS.thread_pool.pcm" -std=c++23 -o build/BS_thread_pool_test -D BS_THREAD_POOL_TEST_IMPORT_MODULE -D BS_THREAD_POOL_IMPORT_STD +``` + +Again, you should add `-D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to test the native extensions. If you now type `build/BS_thread_pool_test`, the test program will run. If the `std` module was successfully imported, the test program will print the message: + +```none +C++ Standard Library imported using: +* Thread pool library: import std (C++23 std module). +* Test program: import std (C++23 std module). +``` + +### Compiling with MSVC and Microsoft STL using `import std` + +Note: The following instructions have only been tested using MSVC v19.42.34435 and Microsoft STL v143 (202408), the latest versions at the time of writing, and may not work with older versions. + +Before compiling the `std` module, you must find the file `std.ixx`. It should be located in the folder `C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\\modules`. Replace `` with the full version number of the MSVC runtime library, e.g. `14.42.34433`. If you installed Visual Studio in a different folder, locate `std.ixx` manually in that folder. + +To compile the module file `std.ixx` with MSVC, first open the Visual Studio Developer PowerShell for the appropriate CPU architecture as explained [above](#compiling-with-msvc-using-import-bsthread_pool). Navigate to the repository folder, create the `build` folder using `mkdir build`, and then run the following command in the root folder of the repository: + +```pwsh +cl "path to std.ixx" /c /EHsc /nologo /permissive- /std:c++latest /Zc:__cplusplus /ifcOutput build/std.ifc /Fo:build/std.obj +``` + +Of course, you should replace `"path to std.ixx"` with the actual path. The compiler arguments are explained [above](#compiling-with-msvc-using-import-bsthread_pool). + +Next, compile the `BS.thread_pool` module as [above](#compiling-with-msvc-using-import-bsthread_pool), but with the following additional flags: + +* `/reference std=build/std.ifc`: Specify that the module `std` is located in the file `build/std.ifc`. +* `/D BS_THREAD_POOL_IMPORT_STD`: Instruct the library to import the `std` module. + +```pwsh +cl modules/BS.thread_pool.cppm /reference std=build/std.ifc /c /EHsc /interface /nologo /permissive- /std:c++latest /TP /Zc:__cplusplus /I include /ifcOutput build/BS.thread_pool.ifc /Fo:build/BS.thread_pool.obj /D BS_THREAD_POOL_IMPORT_STD +``` + +Add `/D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to enable the [native extensions](#native-extensions). Once the module is compiled, you can compile the test program as follows (note that we added `build/std.obj` to link with the `std` module): + +```pwsh +cl tests/BS_thread_pool_test.cpp build/std.obj build/BS.thread_pool.obj /reference std=build/std.ifc /reference BS.thread_pool=build/BS.thread_pool.ifc /EHsc /nologo /permissive- /std:c++latest /Zc:__cplusplus /Fo:build/BS_thread_pool_test.obj /Fe:build/BS_thread_pool_test.exe /D BS_THREAD_POOL_TEST_IMPORT_MODULE /D BS_THREAD_POOL_IMPORT_STD +``` + +Again, you should add `/D BS_THREAD_POOL_NATIVE_EXTENSIONS` if you wish to test the native extensions. If you now type `build/BS_thread_pool_test`, the test program will run. If the `std` module was successfully imported, the test program will print the message: + +```none +C++ Standard Library imported using: +* Thread pool library: import std (C++23 std module). +* Test program: import std (C++23 std module). +``` + +### Compiling with CMake using `import std` + +Note: The following instructions have only been tested using CMake v3.31.2, the latest version at the time of writing, and may not work with older versions. Also, modules are currently only supported by CMake with the [`Ninja`](https://ninja-build.org/) and `Visual Studio 17 2022` generators. + +If you are using [CMake](https://cmake.org/), you can enable `CMAKE_EXPERIMENTAL_CXX_IMPORT_STD` to automatically compile the `std` module, provided the compiler and standard library support it. Here is an example of a `CMakeLists.txt` file that can be used to build the test program, import the thread pool library as a module, and import the C++ Standard Library as a module: + +```cmake +cmake_minimum_required(VERSION 3.31) +project(BS_thread_pool_test LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD ON) + +add_compile_definitions(BS_THREAD_POOL_IMPORT_STD) + +if(MSVC) + add_compile_options(/permissive- /Zc:__cplusplus) +endif() + +add_library(BS_thread_pool) +target_sources(BS_thread_pool PRIVATE FILE_SET CXX_MODULES FILES modules/BS.thread_pool.cppm) +target_include_directories(BS_thread_pool PRIVATE include) + +add_executable(${PROJECT_NAME} tests/BS_thread_pool_test.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE BS_thread_pool) +target_compile_definitions(${PROJECT_NAME} PRIVATE BS_THREAD_POOL_TEST_IMPORT_MODULE) +``` + +The `if(MSVC)` block is explained [above](#compiling-with-msvc-using-import-bsthread_pool). To enable the [native extensions](#native-extensions), add the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` to `add_compile_definitions()`. + +Place this file in the root folder of the repository, and then run the following commands: + +```bash +cmake -B build +cmake --build build +build/BS_thread_pool_test +``` + +For MSVC, replace the last command with `build/Debug/BS_thread_pool_test`. If the `std` module was successfully imported, the test program will print the message: + +```none +C++ Standard Library imported using: +* Thread pool library: import std (C++23 std module). +* Test program: import std (C++23 std module). +``` + +You can also instruct CMake to download the library automatically from the GitHub repository, as explained below, either using [CPM](#installing-using-cmake-with-cpm) or [`FetchContent`](#installing-using-cmake-with-fetchcontent). ## Installing the library using package managers ### Installing using vcpkg -If you are using the [vcpkg](https://vcpkg.io/) C/C++ package manager, you can easily install `BS::thread_pool` with the following commands: +If you are using the [vcpkg](https://vcpkg.io/) C/C++ package manager, you can easily install `BS::thread_pool` with the following command: -On Linux/macOS: - -```none -./vcpkg install bshoshany-thread-pool -``` - -On Windows: - -```none -.\vcpkg install bshoshany-thread-pool:x86-windows bshoshany-thread-pool:x64-windows +```bash +vcpkg install bshoshany-thread-pool ``` To update the package to the latest version, run: -```none +```bash vcpkg upgrade ``` +Please refer to [this package's page on vcpkg.io](https://vcpkg.io/en/package/bshoshany-thread-pool) for more information. + ### Installing using Conan -If you are using the [Conan](https://conan.io/) C/C++ package manager, you can easily integrate `BS::thread_pool` into your project by adding the following lines to your `conanfile.txt`: +If you are using the [Conan](https://conan.io/) C/C++ package manager, you can easily integrate `BS::thread_pool` into your project by adding the following lines to your `conanfile.txt`: ```ini [requires] -bshoshany-thread-pool/4.1.0 +bshoshany-thread-pool/5.0.0 ``` 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. @@ -1795,216 +2926,402 @@ To update the package to the latest version, simply change the version number. P If you are using the [Meson](https://mesonbuild.com/) build system, you can install `BS::thread_pool` from [WrapDB](https://mesonbuild.com/Wrapdb-projects.html). To do so, create a `subprojects` folder in your project (if it does not already exist) and run the following command: -```none +```bash meson wrap install bshoshany-thread-pool ``` Then, use `dependency('bshoshany-thread-pool')` in your `meson.build` file to include the package. To update the package to the latest version, run: -```none +```bash meson wrap update bshoshany-thread-pool ``` ### Installing using CMake with CPM -If you are using [CMake](https://cmake.org/), you can install `BS::thread_pool` with [CPM](https://github.com/cpm-cmake/CPM.cmake). If CPM is already installed, simply add the following to your project's `CMakeLists.txt`: +Note: The following instructions have only been tested using CMake v3.31.2 and CPM v0.40.2, the latest versions at the time of writing, and may not work with older versions. + +If you are using [CMake](https://cmake.org/), you can install `BS::thread_pool` most easily with [CPM](https://github.com/cpm-cmake/CPM.cmake). If CPM is already installed, simply add the following to your project's `CMakeLists.txt`: ```cmake CPMAddPackage( NAME BS_thread_pool GITHUB_REPOSITORY bshoshany/thread-pool - VERSION 4.1.0) + VERSION 5.0.0 + EXCLUDE_FROM_ALL + SYSTEM +) add_library(BS_thread_pool INTERFACE) target_include_directories(BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR}/include) ``` -This will automatically download the indicated version of the package from the GitHub repository and include it in your project. +This will automatically download the indicated version of the package from [the GitHub repository](https://github.com/bshoshany/thread-pool) and include it in your project. -It is also possible to use CPM without installing it first, by adding the following lines to `CMakeLists.txt` before `CPMAddPackage`: +A convenient shorthand for GitHub packages also exists, in which case `CPMAddPackage()` can be called with a single argument of the form `"gh:user/name@version"`. After that, `CPM_LAST_PACKAGE_NAME` will be set to the name of the package, so we need to use this variable to define the include folder. This results in a more compact configuration: ```cmake -set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake") +CPMAddPackage("gh:bshoshany/thread-pool@5.0.0") +add_library(BS_thread_pool INTERFACE) +target_include_directories(BS_thread_pool INTERFACE ${${CPM_LAST_PACKAGE_NAME}_SOURCE_DIR}/include) +``` + +It is also possible to use CPM without installing it first, by adding the following lines to `CMakeLists.txt` before `CPMAddPackage()`: + +```cmake +set(CPM_DOWNLOAD_LOCATION ${CMAKE_BINARY_DIR}/CPM.cmake) if(NOT(EXISTS ${CPM_DOWNLOAD_LOCATION})) - message(STATUS "Downloading CPM.cmake") file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake ${CPM_DOWNLOAD_LOCATION}) endif() include(${CPM_DOWNLOAD_LOCATION}) ``` -Here is an example of a complete `CMakeLists.txt` for a project named `my_project` consisting of a single source file `main.cpp` which uses `BS_thread_pool.hpp`: +Here is an example of a complete `CMakeLists.txt` which automatically downloads and compiles the test program [`BS_thread_pool_test.cpp`](#automated-tests): ```cmake -cmake_minimum_required(VERSION 3.19) -project(my_project LANGUAGES CXX) +cmake_minimum_required(VERSION 3.31) +project(BS_thread_pool_test LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake") + +if(MSVC) + add_compile_options(/permissive- /Zc:__cplusplus) +endif() + +set(CPM_DOWNLOAD_LOCATION ${CMAKE_BINARY_DIR}/CPM.cmake) if(NOT(EXISTS ${CPM_DOWNLOAD_LOCATION})) - message(STATUS "Downloading CPM.cmake") file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake ${CPM_DOWNLOAD_LOCATION}) endif() include(${CPM_DOWNLOAD_LOCATION}) -CPMAddPackage( - NAME BS_thread_pool - GITHUB_REPOSITORY bshoshany/thread-pool - VERSION 4.1.0) + +CPMAddPackage("gh:bshoshany/thread-pool@5.0.0") add_library(BS_thread_pool INTERFACE) -target_include_directories(BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR}/include) -add_executable(my_project main.cpp) -target_link_libraries(my_project BS_thread_pool) +target_include_directories(BS_thread_pool INTERFACE ${${CPM_LAST_PACKAGE_NAME}_SOURCE_DIR}/include) + +add_executable(${PROJECT_NAME} ${${CPM_LAST_PACKAGE_NAME}_SOURCE_DIR}/tests/BS_thread_pool_test.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE BS_thread_pool) ``` -With both `CMakeLists.txt` and `main.cpp` in the same folder, type the following commands to build the project: +Note that for MSVC we have to add the `/permissive-` flag to enforce strict C++ standard conformance, otherwise the test program will not compile, and `/Zc:__cplusplus`, otherwise the test program cannot detect the correct C++ version. This is handled automatically by the `if(MSVC)` block. -```none -cmake -S . -B build +To enable the [native extensions](#native-extensions), add the line `add_compile_definitions(BS_THREAD_POOL_NATIVE_EXTENSIONS)`. Replace `CMAKE_CXX_STANDARD 17` with `20` or `23` if you wish to use C++20 or C++23 features, respectively. Of course, you should add warning, debugging, optimization, and other compiler flags to the configuration above as needed. + +With this `CMakeLists.txt` in an empty folder, type the following commands to build and run the project: + +```bash +cmake -B build cmake --build build +build/BS_thread_pool_test +``` + +For MSVC, replace the last command with `build/Debug/BS_thread_pool_test`. Please see [here](#compiling-with-cmake-using-import-bsthread_pool) for instructions on how to import the library as a C++20 module with CMake, and [here](#compiling-with-cmake-using-import-std) for instructions on how to import the C++ Standard Library as a module with CMake. + +### Installing using CMake with `FetchContent` + +Note: The following instructions have only been tested using CMake v3.31.2, the latest version at the time of writing, and may not work with older versions. + +If you are using [CMake](https://cmake.org/) but do not wish to use 3rd-party tools, you can also install `BS::thread_pool` using the built-in [`FetchContent`](https://cmake.org/cmake/help/latest/module/FetchContent.html) module. Here is an example of a complete `CMakeLists.txt` which automatically downloads and compiles the test program, as in the previous section, but this time using `FetchContent` directly: + +```cmake +cmake_minimum_required(VERSION 3.31) +project(BS_thread_pool_test LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +if(MSVC) + add_compile_options(/permissive- /Zc:__cplusplus) +endif() + +include(FetchContent) +set(FETCHCONTENT_UPDATES_DISCONNECTED ON) +FetchContent_Declare( + bshoshany_thread_pool + GIT_REPOSITORY https://github.com/bshoshany/thread-pool.git + GIT_TAG v5.0.0 + DOWNLOAD_EXTRACT_TIMESTAMP TRUE + EXCLUDE_FROM_ALL + SYSTEM +) +FetchContent_MakeAvailable(bshoshany_thread_pool) +add_library(BS_thread_pool INTERFACE) +target_include_directories(BS_thread_pool INTERFACE ${bshoshany_thread_pool_SOURCE_DIR}/include) + +add_executable(${PROJECT_NAME} ${bshoshany_thread_pool_SOURCE_DIR}/tests/BS_thread_pool_test.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE BS_thread_pool) ``` ## Complete library reference -This section provides a complete reference to classes, member functions, objects, and macros available in this library, along with other important information. Member functions are given here with simplified prototypes (e.g. removing `const`) for ease of reading. +This section provides a complete reference for all classes and functions available in this library, along with other important information. Functions are given with simplified prototypes (e.g. removing `const`) for ease of reading. Explanations are kept brief, as the purpose of this section is only to provide a quick reference; for more detailed information and usage examples, please refer to the full documentation above. -More information can be found in the provided [Doxygen](https://www.doxygen.nl/) comments. Any modern IDE, such as [Visual Studio Code](https://code.visualstudio.com/), can use the Doxygen comments to provide automatic documentation for any class and member function in this library when hovering over code with the mouse or using auto-complete. +Descriptions of each item can also be found in the [Doxygen](https://www.doxygen.nl/) comments embedded in the source code. Any modern IDE, such as [Visual Studio Code](https://code.visualstudio.com/), can use these Doxygen comments to provide automatic documentation for any class and member function in this library when hovering over code with the mouse or using auto-complete. -### Main thread pool header file (`BS_thread_pool.hpp`) +### The `BS::thread_pool` class template -#### The `BS::thread_pool` class - -The class `BS::thread_pool` is the main thread pool class. It can be used to create a pool of threads and submit tasks to a queue. When a thread becomes available, it takes a task from the queue and executes it. The member functions that are available by default, when no macros are defined, are: +`BS::thread_pool` is the main thread pool class. It is used to create a pool of threads that continuously execute tasks submitted to a queue. It can take template parameters, which enable optional features as described [below](#optional-features-and-the-template-parameter). The member functions that are available by default, when no template parameters are used, are: * Constructors: - * `thread_pool()`: Construct a new thread pool. The number of threads will be the total number of hardware threads available, as reported by the implementation. This is usually determined by the number of cores in the CPU. If a core is hyperthreaded, it will count as two threads. - * `thread_pool(BS::concurrency_t num_threads)`: Construct a new thread pool with the specified number of threads. - * `thread_pool(std::function& init_task)`: Construct a new thread pool with the specified initialization function. - * `thread_pool(BS::concurrency_t num_threads, std::function& init_task)`: Construct a new thread pool with the specified number of threads and initialization function. + * `thread_pool()`: Construct a new thread pool with a number of threads equal to `std::thread::hardware_concurrency()`. + * `thread_pool(std::size_t num_threads)`: Construct a new thread pool with the specified number of threads. + * `thread_pool(F&& init)`: Construct a new thread pool with a number of threads equal to `std::thread::hardware_concurrency()` and the specified initialization function. `F` is a template parameter. + * `thread_pool(std::size_t num_threads, F&& init)`: Construct a new thread pool with the specified number of threads and the specified initialization function. * Resetters: - * `void reset()`: Reset the pool with the total number of hardware threads available, as reported by the implementation. 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. - * `void reset(BS::concurrency_t num_threads)`: Reset the pool with a new number of threads. - * `void reset(std::function& init_task)` Reset the pool with the total number of hardware threads available, as reported by the implementation, and a new initialization function. - * `void reset(BS::concurrency_t num_threads, std::function& init_task)`: Reset the pool with a new number of threads and a new initialization function. + * `void reset()`: Reset the pool with a number of threads equal to `std::thread::hardware_concurrency()`, waiting for running tasks first, and preserving submitted tasks after the reset. + * `void reset(std::size_t num_threads)`: Reset the pool with a new number of threads. + * `void reset(F&& init)` Reset the pool with a number of threads equal to `std::thread::hardware_concurrency()` and a new initialization function. `F` is a template parameter. + * `void reset(std::size_t num_threads, F&& init)`: Reset the pool with a new number of threads and a new initialization function. +* Setters: + * `void set_cleanup_func(F&& cleanup)`: Set the thread pool's cleanup function. `F` is a template parameter. * Getters: - * `size_t get_tasks_queued()`: Get the number of tasks currently waiting in the queue to be executed by the threads. - * `size_t get_tasks_running()`: Get the number of tasks currently being executed by the threads. - * `size_t get_tasks_total()`: Get the total number of unfinished tasks: either still waiting in the queue, or running in a thread. Note that `get_tasks_total() == get_tasks_queued() + get_tasks_running()`. - * `BS::concurrency_t get_thread_count()`: Get the number of threads in the pool. - * `std::vector get_thread_ids()`: Get a vector containing the unique identifiers for each of the pool's threads, as obtained by `std::thread::get_id()`. -* Task submission without futures (`T` and `F` are template parameters): - * `void detach_task(F&& task)`: Submit a function with no arguments and no return value into the task queue. To push a function with arguments, enclose it in a lambda expression. Does not return a future, so the user must use `wait()` or some other method to ensure that the task finishes executing, otherwise bad things will happen. - * `void detach_blocks(T first_index, T index_after_last, F&& block, size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The `block` function takes two arguments, the start and end of the block, so that it is only called only once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Does not return a `BS::multi_future`, so the user must use `wait()` or some other method to ensure that the loop finishes executing, otherwise bad things will happen. - * `void detach_loop(T first_index, T index_after_last, F&& loop, size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The `loop` function takes one argument, the loop index, so that it is called many times per block. Does not return a `BS::multi_future`, so the user must use `wait()` or some other method to ensure that the loop finishes executing, otherwise bad things will happen. - * `void detach_sequence(T first_index, T index_after_last, F&& sequence)`: Submit a sequence of tasks enumerated by indices to the queue. Does not return a `BS::multi_future`, so the user must use `wait()` or some other method to ensure that the sequence finishes executing, otherwise bad things will happen. -* Task submission with futures (`T`, `F`, and `R` are template parameters): - * `std::future submit_task(F&& task)`: Submit a function with no arguments into the task queue. To submit a function with arguments, enclose it in a lambda expression. If the function has a return value, get a future for the eventual returned value. If the function has no return value, get an `std::future` which can be used to wait until the task finishes. - * `BS::multi_future submit_blocks(T first_index, T index_after_last, F&& block, size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The `block` function takes two arguments, the start and end of the block, so that it is only called only once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Returns a `BS::multi_future` that contains the futures for all of the blocks. - * `BS::multi_future submit_loop(T first_index, T index_after_last, F&& loop, size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The `loop` function takes one argument, the loop index, so that it is called many times per block. It must have no return value. Returns a `BS::multi_future` that contains the futures for all of the blocks. - * `BS::multi_future submit_sequence(T first_index, T index_after_last, F&& sequence)`: Submit a sequence of tasks enumerated by indices to the queue. Returns a `BS::multi_future` that contains the futures for all of the tasks. + * `std::size_t get_tasks_queued()`: Get the number of tasks currently waiting in the queue to be executed by the threads. + * `std::size_t get_tasks_running()`: Get the number of tasks currently being executed by the threads. + * `std::size_t get_tasks_total()`: Get the total number of unfinished tasks: either still waiting in the queue, or running in a thread. Note that `get_tasks_total() == get_tasks_queued() + get_tasks_running()`. + * `std::size_t get_thread_count()`: Get the number of threads in the pool. + * `std::vector get_thread_ids()`: Get a vector containing the unique identifiers for each of the pool's threads, as obtained by `std::thread::get_id()` (or `std::jthread::get_id()` in C++20 and later). +* Task submission without futures (`T1`, `T2`, and `F` are template parameters): + * `void detach_task(F&& task)`: Submit a function with no arguments and no return value into the task queue. To submit a function with arguments, enclose it in a lambda expression. + * `void detach_blocks(T1 first_index, T2 index_after_last, F&& block, std::size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks. The block function takes two arguments, the start and end of the block, so that it is only called once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. + * `void detach_loop(T1 first_index, T2 index_after_last, F&& loop, std::size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks. The loop function takes one argument, the loop index, so that it is called many times per block. + * `void detach_sequence(1T first_index, T2 index_after_last, F&& sequence)`: Submit a sequence of tasks enumerated by indices to the queue. The sequence function takes one argument, the task index, and will be called once per index. +* Task submission with futures (`T1`, `T2`, `F`, and `R` are template parameters): + * `std::future submit_task(F&& task)`: Submit a function with no arguments into the task queue. To submit a function with arguments, enclose it in a lambda expression. + * `BS::multi_future submit_blocks(T1 first_index, T2 index_after_last, F&& block, std::size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks. The block function takes two arguments, the start and end of the block, so that it is only called once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Returns a `BS::multi_future` that contains the futures for all of the blocks. + * `BS::multi_future submit_loop(T1 first_index, T2 index_after_last, F&& loop, std::size_t num_blocks = 0)`: Parallelize a loop by automatically splitting it into blocks. The loop function takes one argument, the loop index, so that it is called many times per block. It must have no return value. Returns a `BS::multi_future` that contains the futures for all of the blocks. + * `BS::multi_future submit_sequence(T1 first_index, T2 index_after_last, F&& sequence)`: Submit a sequence of tasks enumerated by indices to the queue. The sequence function takes one argument, the task index, and will be called once per index. Returns a `BS::multi_future` that contains the futures for all of the tasks. * Task management: - * `void purge()`: Purge all the tasks waiting in the queue. Tasks that are currently running will not be affected, but any tasks still waiting in the queue will be discarded, and will never be executed by the threads. Please note that there is no way to restore the purged tasks. -* Waiting for tasks (`R` and `P`, `C`, and `D` are template parameters): - * `void wait()`: 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 pool is paused, this function only waits for the currently running tasks (otherwise it would wait forever). Note: To wait for just one specific task, use `submit_task()` instead, and call the `wait()` member function of the generated future. + * `void purge()`: Purge all the tasks waiting in the queue. Please note that there is no way to restore the purged tasks. +* Waiting for tasks (`R`, `P`, `C`, and `D` are template parameters): + * `void wait()`: Wait for all tasks to be completed, both those that are currently running in the threads and those that are still waiting in the queue. * `bool wait_for(std::chrono::duration& duration)`: Wait for tasks to be completed, but stop waiting after the specified duration has passed. Returns `true` if all tasks finished running, `false` if the duration expired but some tasks are still running. * `bool wait_until(std::chrono::time_point& timeout_time)`: Wait for tasks to be completed, but stop waiting after the specified time point has been reached. Returns `true` if all tasks finished running, `false` if the time point was reached but some tasks are still running. +* Destructor: + * `~thread_pool()`: Wait for all tasks to complete, then destroy all threads. If a cleanup function was set, it will run in each thread right before it is destroyed. -When a `BS::thread_pool` object goes out of scope, the destructor first waits for all tasks to complete, then destroys all threads. Note that if the pool is paused, then any tasks still in the queue will never be executed. +### Optional features and the template parameter -#### Optional features for the `BS::thread_pool` class +The thread pool has several optional features that must be explicitly enabled by passing a template parameter. The template parameter is a bitmask, so you can enable several features at once by combining them with the bitwise OR operator `|`. The bitmask flags are members of the `BS::tp` enumeration. -The thread pool has several optional features that must be explicitly enabled using macros. - -* **Task priority:** Enabled by defining the macro `BS_THREAD_POOL_ENABLE_PRIORITY`. - * When enabled, the priority of a task or group of tasks may be specified as an additional argument (at the end of the argument list) to `detach_task()`, `submit_task()`, `detach_blocks()`, `submit_blocks()`, `detach_loop()`, `submit_loop()`, `detach_sequence()`, and `submit_sequence()`. If the priority is not specified, the default value will be 0. - * The priority is a number of type `BS::priority_t`, which is a signed 16-bit integer, so it can have any value between -32,768 and 32,767. The tasks will be executed in priority order from highest to lowest. - * The namespace `BS::pr` contains some pre-defined priorities: `BS::pr::highest`, `BS::pr::high`, `BS::pr::normal`, `BS::pr::low`, and `BS::pr::lowest`. -* **Pausing:** Enabled by defining the macro `BS_THREAD_POOL_ENABLE_PAUSE`. Adds the following member functions: +* **Task priority:** Enabled by turning on the `BS::tp::priority` flag in the template parameter. When enabled, the static member `priority_enabled` will be set to `true`. + * When enabled, the priority of a task or group of tasks may be specified as an additional argument (at the end of the argument list) to all detach and submit functions. If the priority is not specified, the default value will be 0. + * The priority is of type `BS::priority_t`, a signed 8-bit integer, with values between -128 and +127. The tasks will be executed in priority order from highest to lowest. Groups of parallelized tasks will all have the same priority. + * The enumeration `BS::pr` contains some pre-defined priorities: `BS::pr::highest`, `BS::pr::high`, `BS::pr::normal`, `BS::pr::low`, and `BS::pr::lowest`. +* **Pausing:** Enabled by turning on the `BS::tp::pause` flag in the template parameter. When enabled, the static member `pause_enabled` will be set to `true`. Adds the following member functions: * `void pause()`: Pause the pool. The workers will temporarily stop retrieving new tasks out of the queue, although any tasks already executed will keep running until they are finished. * `void unpause()`: Unpause the pool. The workers will resume retrieving new tasks out of the queue. * `bool is_paused()`: Check whether the pool is currently paused. -* **Getting the native handles of the threads:** Enabled by defining the macro `BS_THREAD_POOL_ENABLE_NATIVE_HANDLES`. Adds the following member function: - * `std::vector get_native_handles()`: Get a vector containing the underlying implementation-defined thread handles for each of the pool's threads. -* **Wait deadlock checks:** Enabled by defining the macro `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK`. - * When enabled, `wait()`, `wait_for()`, and `wait_until()` will check whether the user tried to call them from within a thread of the same pool, which would result in a deadlock. If so, they will throw the exception `BS::thread_pool::wait_deadlock` instead of waiting. -* **Disabling exception handling**: Achieved by defining the macro `BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING`. - * This can be used to disable exception handling in `submit_task()` if it is not needed, or if exceptions are explicitly disabled in the codebase. - * Note that this macro can be defined independently of `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK`. Disabling exception handling removes the `try`-`catch` block from `submit_task()`, while enabling wait deadlock checks adds a `throw` expression to `wait()`, `wait_for()`, and `wait_until()`. - * If the feature-test macro `__cpp_exceptions` is undefined, `BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING` is automatically defined, and `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK` is automatically undefined. +* **Wait deadlock checks:** Enabled by turning on the `BS::tp::wait_deadlock_checks` flag in the template parameter. When enabled, the static member `wait_deadlock_checks_enabled` will be set to `true`. + * When enabled, `wait()`, `wait_for()`, and `wait_until()` will check whether the user tried to call them from within a thread of the same pool, which would result in a deadlock. If so, they will throw the exception `BS::wait_deadlock` instead of waiting. + * If the feature-test macro `__cpp_exceptions` is undefined, wait deadlock checks will be automatically disabled, and trying to enable this feature will result in a compilation error. -#### The `BS::this_thread` namespace +Convenience aliases are defined as follows: -The namespace `BS::this_thread` provides functionality similar to `std::this_thread`. It contains the following function objects: +* `BS::light_thread_pool` disables all optional features (equivalent to `BS::thread_pool` with the default template parameter, that is, `BS::thread_pool`). +* `BS::priority_thread_pool` enables task priority (equivalent to `BS::thread_pool`). +* `BS::pause_thread_pool` enables pausing the pool (equivalent to `BS::thread_pool`). +* `BS::wdc_thread_pool` enables wait deadlock checks (equivalent to `BS::thread_pool`). -* `BS::this_thread::get_index()` can be used to get the index of the current thread. If this thread belongs to a `BS::thread_pool` object, it will have an index from 0 to `BS::thread_pool::get_thread_count() - 1`. Otherwise, for example if this thread is the main thread or an independent [`std::thread`](https://en.cppreference.com/w/cpp/thread/thread), [`std::nullopt`](https://en.cppreference.com/w/cpp/utility/optional/nullopt) will be returned. -* `BS::this_thread::get_pool()` can be used to get the pointer to the thread pool that owns the current thread. If this thread belongs to a `BS::thread_pool` object, a pointer to that object will be returned. Otherwise, `std::nullopt` will be returned. -* In both cases, an [`std::optional`](https://en.cppreference.com/w/cpp/utility/optional) object will be returned, of type `BS::this_thread::optional_index` or `BS::this_thread::optional_pool` respectively. Unless you are 100% sure this thread is in a pool, first use [`std::optional::has_value()`](https://en.cppreference.com/w/cpp/utility/optional/operator_bool) to check if it contains a value, and if so, use [`std::optional::value()`](https://en.cppreference.com/w/cpp/utility/optional/value) to obtain that value. +### The `BS::this_thread` class -#### The `BS::multi_future` class +The class `BS::this_thread` provides functionality analogous to `std::this_thread`. It contains the following static member functions: -`BS::multi_future` is a helper class used to facilitate waiting for and/or getting the results of multiple futures at once. It is defined as a specialization of `std::vector>`. This means that all of the member functions that can be used on an [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) can also be used on a `BS::multi_future`. For example, you may use a range-based for loop with a `BS::multi_future`, since it has iterators. +* `static std::optional get_index()`: Get the index of the current thread. The optional object will not have a value if the thread is not in a pool. +* `static std::optional get_pool()`: Get a pointer to the thread pool that owns the current thread. The optional object will not have a value if the thread is not in a pool. -In addition to inherited member functions, `BS::multi_future` has the following specialized member functions (`R` and `P`, `C`, and `D` are template parameters): +If the [native extensions](#the-native-extensions) are enabled, the class will contain additional static member functions. Please see the relevant section for more information. + +### The native extensions + +The native extensions may be enabled by defining the macro `BS_THREAD_POOL_NATIVE_EXTENSIONS` at compilation time. If including the library as a header file, the macro must be defined before `#include "BS_thread_pool.hpp"`. If importing the library as a C++20 module, the macro must be defined as a compiler flag. The native extensions use the operating system's native API, and are thus not portable; however, they should work on Windows, Linux, and macOS. + +The native extensions add the following functions to the `BS` namespace: + +* `bool set_os_process_affinity(std::vector& affinity)`: Set the processor affinity of the current process. The argument is an `std::vector` where each element corresponds to a logical processor. Returns `true` if the affinity was set successfully, `false` otherwise. Does not work on macOS. +* `std::optional> BS::get_os_process_affinity()`: Get the processor affinity of the current process. The optional object will not have a value if the affinity could not be determined. Does not work on macOS. +* `bool BS::set_os_process_priority(BS::os_process_priority priority)`: Set the priority of the current process. The argument must be a member of the `BS::os_process_priority` enumeration, which contains the options `idle`, `below_normal`, `normal`, `above_normal`, `high`, and `realtime`. Returns `true` if the priority was set successfully, or `false` otherwise. +* `std::optional BS::get_os_process_priority()`: Get the priority of the current process. The optional object will not have a value if the priority could not be determined, or it is not one of the pre-defined values in the `BS::os_process_priority` enumeration. + +The native extensions also add the following static member functions to `BS::this_thread`: + +* `bool set_os_thread_affinity(std::vector& affinity)`: Set the processor affinity of the current thread. The argument is an `std::vector` where each element corresponds to a logical processor. Note that the thread affinity must be a subset of the process affinity for the containing process of a thread. Does not work on macOS. +* `std::optional> get_os_thread_affinity()`: Get the processor affinity of the current thread. The optional object will not have a value if the affinity could not be determined. Does not work on macOS. +* `bool set_os_thread_name(std::string& name)`: Set the name of the current thread. Note that on Linux thread names are limited to 16 characters, including the null terminator. Returns `true` if the name was set successfully, `false` otherwise. +* `std::optional get_os_thread_name()`: Get the name of the current thread. The optional object will not have a value if the name could not be determined. +* `bool set_os_thread_priority(BS::os_thread_priority priority)`: Set the priority of the current thread. The argument must be a member of the `BS::os_thread_priority` enumeration, which contains the options `idle`, `lowest`, `below_normal`, `normal`, `above_normal`, `highest`, and `realtime`. Returns `true` if the priority was set successfully, or `false` otherwise. +* `std::optional get_os_thread_priority()`: Get the priority of the current thread. The optional object will not have a value if the priority could not be determined, or it is not one of the pre-defined values in the `BS::os_thread_priority` enumeration. + +Finally, the native extensions add the following member function to `BS::thread_pool`: + +* `std::vector get_native_handles()`: Get a vector containing the underlying implementation-defined thread handles for each of the pool's threads. + +### The `BS::multi_future` class + +`BS::multi_future` is a helper class used to facilitate waiting for and/or getting the results of multiple futures at once. It is defined as a specialization of `std::vector>`. This means that all of the member functions that can be used on an [`std::vector>`](https://en.cppreference.com/w/cpp/container/vector) can also be used on a `BS::multi_future`. For example, you may use a range-based for loop with a `BS::multi_future`, since it has iterators. + +In addition to inherited member functions, `BS::multi_future` has the following specialized member functions (`R` and `P`, `C`, and `D` are template parameters): * `[void or std::vector] get()`: Get the results from all the futures stored in this `BS::multi_future`, rethrowing any stored exceptions. If the futures return `void`, this function returns `void` as well. If the futures return a type `T`, this function returns a vector containing the results. -* `size_t ready_count()`: Check how many of the futures stored in this `BS::multi_future` are ready. +* `std::size_t ready_count()`: Check how many of the futures stored in this `BS::multi_future` are ready. * `bool valid()`: Check if all the futures stored in this `BS::multi_future` are valid. * `void wait()`: Wait for all the futures stored in this `BS::multi_future`. * `bool wait_for(std::chrono::duration& duration)`: Wait for all the futures stored in this `BS::multi_future`, but stop waiting after the specified duration has passed. Returns `true` if all futures have been waited for before the duration expired, `false` otherwise. -* `bool wait_until(std::chrono::time_point& timeout_time)`: Wait for all the futures stored in this `multi_future` object, but stop waiting after the specified time point has been reached. Returns `true` if all futures have been waited for before the time point was reached, `false` otherwise. +* `bool wait_until(std::chrono::time_point& timeout_time)`: Wait for all the futures stored in this `BS::multi_future` object, but stop waiting after the specified time point has been reached. Returns `true` if all futures have been waited for before the time point was reached, `false` otherwise. -### Utility header file (`BS_thread_pool_utils.hpp`) +### The `BS::synced_stream` class -#### The `BS::signaller` class +`BS::synced_stream` is a utility class which can be used to synchronize printing to one or more output streams by different threads. It has the following member functions (`T` is a template parameter pack): -`BS::signaller` is a utility class which can be used to allow simple signalling between threads. This class is really just a convenient wrapper around [`std::promise`](https://en.cppreference.com/w/cpp/thread/promise), which contains both the promise and its future. It has the following member functions: - -* `signaller()`: Construct a new signaller. -* `void wait()`: Wait until the signaller is ready. -* `void ready()`: Inform any waiting threads that the signaller is ready. - -#### The `BS::synced_stream` class - -`BS::synced_stream` is a utility class which can be used to synchronize printing to an output stream by different threads. It has the following member functions (`T` is a template parameter pack): - -* `synced_stream(std::ostream& stream = std::cout)`: Construct a new synced stream which prints to the given output stream. -* `void print(T&&... items)`: Print any number of items into the output stream. Ensures that no other threads print to this stream simultaneously, as long as they all exclusively use the same `synced_stream` object to print. -* `void println(T&&... items)`: Print any number of items into the output stream, followed by a newline character. +* `synced_stream()`: Construct a new synced stream which prints to `std::cout`. +* `synced_stream(T&... streams)`: Construct a new synced stream which prints to the given output streams. +* `void add_stream(std::ostream& stream)`: Add a stream to the list of output streams. +* `std::vector& get_streams()`: Get a reference to a vector containing pointers to the output streams to print to. +* `void print(T&... items)`: Print any number of items into the output streams. Ensures that no other threads print to the streams simultaneously, as long as they all exclusively use the same `BS::synced_stream` object to print. +* `void println(T&&... items)`: Print any number of items into the output streams, followed by a newline character. +* `void remove_stream(std::ostream& stream)`: Remove a stream from the list of output streams. In addition, the class comes with two stream manipulators, which are meant to help the compiler figure out which template specializations to use with the class: * `BS::synced_stream::endl`: An explicit cast of `std::endl`. Prints a newline character to the stream, and then flushes it. Should only be used if flushing is desired, otherwise a newline character should be used instead. * `BS::synced_stream::flush`: An explicit cast of `std::flush`. Used to flush the stream. -#### The `BS::timer` class +### The `BS::version` class -`BS::timer` is a utility class which can be used to measure execution time for benchmarking purposes. It has the following member functions: +`BS::version` is a utility class used to represent a version number. It has public members `major`, `minor`, and `patch`, as well as the following member functions: -* `timer()`: Construct a new timer and immediately start measuring time. -* `void start()`: Start (or restart) measuring time. Note that the timer starts ticking as soon as the object is created, so this is only necessary if we want to restart the clock later. -* `void stop()`: Stop measuring time and store the elapsed time since the object was constructed or since `start()` was last called. -* `std::chrono::milliseconds::rep current_ms()`: Get the number of milliseconds that have elapsed since the object was constructed or since `start()` was last called, but keep the timer ticking. -* `std::chrono::milliseconds::rep ms()`: Get the number of milliseconds stored when `stop()` was last called. +* `constexpr version(std::uint64_t major, std::uint64_t minor, std::uint64_t patch)`: Construct a new version object with the specified major, minor, and patch numbers. +* `std::strong_ordering operator<=>(version&)`: 3-way comparison operator for two version numbers, in C++20 and later. In C++17, the operators `==`, `!=`, `<`, `<=`, `>`, `>=` are instead defined explicitly. +* `std::string to_string()`: Convert the version number to a string in the format `"major.minor.patch"`. +* `std::ostream& operator<<(std::ostream& stream, version& ver)`: Output the version string to a stream. + +In addition, the library defines a `constexpr` object `BS::thread_pool_version` of type `BS::version`, which can be used to check the version of the library at compilation time. + +Note that this feature is only available starting with v5.0.0 of the library; previous versions used the macros `BS_THREAD_POOL_VERSION_MAJOR`, `BS_THREAD_POOL_VERSION_MINOR`, and `BS_THREAD_POOL_VERSION_PATCH`, which are still defined for compatibility purposes, but are not accessible if the library is imported as a C++20 module. + +### Diagnostic variables + +The library defines the following `constexpr` variables: + +* `bool thread_pool_import_std`: Indicates whether the library imported the C++23 Standard Library module using `import std`. +* `bool thread_pool_module`: Indicates whether the library was compiled as a C++20 module. +* `bool thread_pool_native_extensions`: Indicates whether the native extensions are enabled. + +### All names exported by the C++20 module + +When the library is imported as a C++20 module using `import BS.thread_pool`, it exports the following names, in alphabetical order: + +* `BS::binary_semaphore` +* `BS::common_index_type_t` +* `BS::counting_semaphore` +* `BS::light_thread_pool` +* `BS::multi_future` +* `BS::pause_thread_pool` +* `BS::pr` +* `BS::priority_t` +* `BS::priority_thread_pool` +* `BS::synced_stream` +* `BS::this_thread` +* `BS::thread_pool` +* `BS::thread_pool_import_std` +* `BS::thread_pool_module` +* `BS::thread_pool_native_extensions` +* `BS::thread_pool_version` +* `BS::tp` +* `BS::version` +* `BS::wait_deadlock` +* `BS::wdc_thread_pool` + +If the native extensions are enabled, the following names are also exported: + +* `BS::get_os_process_affinity` +* `BS::get_os_process_priority` +* `BS::os_process_priority` +* `BS::os_thread_priority` +* `BS::set_os_process_affinity` +* `BS::set_os_process_priority` + +## Development tools + +### The `compile_cpp.py` script + +The Python script `compile_cpp.py`, in the `scripts` folder of [the GitHub repository](https://github.com/bshoshany/thread-pool), can be used to compile any C++ source file with different compilers on different platforms. It requires Python 3.12 or later. + +The script was written by the author of the library to make it easier to test the library with different combinations of compilers, standards, and platforms using the built-in Visual Studio Code tasks. However, note that this script is not meant to replace CMake or any full-fledged build system, it's just a convenient script for developing single-header libraries like this one or other small projects. + +The `compile_cpp.py` script also transparently handles C++20 modules and importing the C++ Standard Library as a module in C++23. Therefore, users of this library who wish to import it as a C++20 module may find this script particularly useful. + +The compilation parameters can be configured using the command line arguments and/or via an optional YAML configuration file `compile_cpp.yaml`. The command line arguments are as follows: + +* Positional argument(s): the source file(s) to compile. +* `-h` or `--help`: Show the help message and exit. +* `-a` or `--arch`: The target architecture (MSVC only). Must be one of `[amd64, arm64]`, default is `amd64`. +* `-c` or `--compiler`: Which compiler to use. Must be one of `[cl, clang++, g++]`. The default is to determine it automatically based on the platform. +* `-d` or `--define`: Macros to define. Use this argument multiple times to define more than one macro. Additional macros can be defined in `compile_cpp.yaml`. +* `-f` or `--flag`: Extra compiler flags to add. Use this argument multiple times to add more than one flag. Additional flags can be specified in `compile_cpp.yaml`. +* `-i` or `--include`: The include folder to use. Use this argument multiple times to use more than one include folder. Additional include folders can be specified in `compile_cpp.yaml`. +* `-l` or `--as-module`: Enable this flag to compile the file as a C++20 module. +* `-m` or `--module`: C++20 module files to use if desired, in the format `module_name=module_file,dependent_files,...`. Use this argument multiple times to use more than one module. Additional modules can be specified in `compile_cpp.yaml`. The dependent files are only used to determine whether the module needs to be recompiled. +* `-o` or `--output`: The output folder and/or executable name. End with `/` to create the folder if it doesn't already exist. If not specified, the folder defined in `compile_cpp.yaml` will be used. If the executable name is not specified, it will be determined automatically in the format `source_[module_]type-compiler-standard` where: + * `source` is the name of the first source file (without the extension). + * `module_`, if present, indicates that the file is a C++20 module. + * `type` is one of `[debug, release]`. + * `compiler` is one of `[clang, gcc, msvc]`. + * `standard` is one of `[c++17, c++20, c++23]`. +* `-p` or `--pass`: Pass command line arguments to the compiled program when running it, if `-r` is specified. Use this argument multiple times to pass more than one argument to the program. Additional arguments can be specified in `compile_cpp.yaml`. +* `-r` or `--run`: Enable this flag to run the program after compiling it. +* `-s` or `--std`: Which C++ standard to use. Must be one of `[c++17, c++20, c++23]`. The default is `c++23`. +* `-t` or `--type`: Which mode to compile in. Must be one of `[debug, release]`. The default is `debug`. +* `-u` or `--std-module`: Specify the path to the standard library module (C++23 only). Taken from `compile_cpp.yaml` if not specified. Use `auto` to auto-detect or `disable` to explicitly disable. +* `-v` or `--verbose`: Enable this flag to print the script's diagnostic messages. + +The `compile_cpp.yaml` file includes the following fields: + +* `defines`: A list of macros to define when compiling the source files. +* `flags`: A map of flags to pass to each compiler. The compiler should be one of `[cl, clang++, g++]`. The flags should be a list of strings. +* `includes`: A list of include folders. +* `modules`: A map of C++20 modules in the format `module_name: [module_path, dependent files, ...]`. Will only be used in C++20 or C++23 mode. The dependent files are only used to determine whether the module needs to be recompiled. +* `output`: The output folder for the compiled files. +* `pass_args`: A list of arguments to pass to the program if running it after compilation. +* `std_module`: A map of paths to the standard library modules for each OS and compiler combination (C++23 only). The OS should be one of `[Windows, Linux, Darwin]`. Use `Automatic` to determine the path automatically if possible. + +Please see the `compile_cpp.yaml` file in the GitHub repository for an example of how to use it. + +### Other included tools + +The `scripts` folder of [the GitHub repository](https://github.com/bshoshany/thread-pool) contains two other Python scripts that are used in the development of the library: + +* `test_all.py` performs the [automated tests](#automated-tests) in C++17, C++20, and C++23 modes, using all compilers available in the system (Clang, GCC, and/or MSVC). Since there are so many tests, the test script does not perform the benchmarks, as that would take too long. Pass the optional argument `--compile-only` to only check that the program compiles successfully with all compilers, without running it. +* `clear_folder.py` is used to clean up output and temporary folders. It will create the folder if it does not already exist, so the outcome is always an empty folder. + +In addition, for Visual Studio Code users, the GitHub repository includes three `.vscode` folders: + +* `.vscode-windows`, to be used in Windows with Clang, GCC, and MSVC. +* `.vscode-linux`, to be used in Linux with Clang and GCC. +* `.vscode-macos`, to be used in macOS with LLVM Clang (not Apple Clang). + +Each folder contains appropriate `c_cpp_properties.json`, `launch.json`, and `tasks.json` files that utilize the included Python scripts. Users are welcome to use these files in their own projects, but they may require some modifications to work on specific systems. ## About the project -### Issue and pull request policy +### Bug reports and feature requests This library is under continuous and active development. If you encounter any bugs, or if you would like to request any additional features, please feel free to [open a new issue on GitHub](https://github.com/bshoshany/thread-pool/issues) and I will look into it as soon as I can. -Contributions are always welcome. However, I release my projects in cumulative updates after editing and testing them locally on my system, so my policy is not to accept any pull requests. If you open a pull request, and I decide to incorporate your suggestion into the project, I will first modify your code to comply with the project's coding conventions (formatting, syntax, naming, comments, programming practices, etc.), and perform some tests to ensure that the change doesn't break anything. I will then merge it into the next release of the project, possibly together with some other changes. The new release will also include a note in `CHANGELOG.md` with a link to your pull request, and modifications to the documentation in `README.md` as needed. +### Contribution and pull request policy -### Acknowledgements - -Many GitHub users have helped improve this project, directly or indirectly, via issues, pull requests, comments, and/or personal correspondence. Please see `CHANGELOG.md` for links to specific issues and pull requests that have been the most helpful. Thank you all for your contribution! :) +Contributions are always welcome. However, I release my projects in cumulative updates after editing and testing them locally on my system, so **my policy is to never accept any pull requests**. If you open a pull request, and I decide to incorporate your suggestion into the project, I will first modify your code to comply with the project's coding conventions (formatting, syntax, naming, comments, programming practices, etc.), and perform some tests to ensure that the change doesn't break anything. I will then merge it into the next release of the project, possibly together with some other changes. The new release will also include a note in `CHANGELOG.md` with a link to your pull request, and modifications to the documentation in `README.md` as needed. ### Starring the repository If you found this project useful, please consider [starring it on GitHub](https://github.com/bshoshany/thread-pool/stargazers)! This allows me to see how many people are using my code, and motivates me to keep working to improve it. +### Acknowledgements + +Many GitHub users have helped improve this project, directly or indirectly, via issues, pull requests, comments, and/or personal correspondence. Please see `CHANGELOG.md` for links to specific issues and pull requests that have been the most helpful. Thank you all for your contribution! 😊 + ### Copyright and citing -Copyright (c) 2024 [Barak Shoshany](https://baraksh.com). Licensed under the [MIT license](LICENSE.txt). +Copyright (c) 2024 [Barak Shoshany](https://baraksh.com/). Licensed under the [MIT license](https://github.com/bshoshany/thread-pool/blob/master/LICENSE.txt). -If you use this C++ thread pool 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 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: @@ -2029,6 +3346,14 @@ You can use the following BibTeX entry: Please note that the papers on [SoftwareX](https://www.sciencedirect.com/science/article/pii/S235271102400058X) and [arXiv](https://arxiv.org/abs/2105.00613) are not up to date with the latest version of the library. These publications are only intended to facilitate discovery of this library by scientists, and to enable citing it in scientific research. Documentation for the latest version is provided only by the `README.md` file in [the GitHub repository](https://github.com/bshoshany/thread-pool). -### Learning more about C++ +### About the author -Beginner C++ programmers may be interested in [my lecture notes](https://baraksh.com/CSE701/notes.php) for a course taught at McMaster University, which teach modern C and C++ from scratch, including some of the advanced techniques and programming practices used in developing this library. +My name is Barak Shoshany and I am a theoretical, mathematical, and computational physicist. I work as an Assistant Professor of Physics at Brock University in Ontario, Canada, and I am also a Sessional Lecturer at McMaster University. My research focuses on the nature of time and causality in general relativity and quantum mechanics, as well as symbolic and high-performance scientific computing. For more about me, please see [my personal website](https://baraksh.com/). + +### Learning more about C++ + +Beginner C++ programmers may be interested in [my lecture notes](https://baraksh.com/CSE701/notes/) for a graduate-level course taught at McMaster University, which teach modern C and C++ from scratch, including some of the advanced techniques and programming practices used in developing this library. I have been teaching this course every year since 2020, and the notes are continuously updated and improved based on student feedback. + +### Other projects to check out + +If you are a physicist or astronomer, you may be interested in my project [OGRe](https://github.com/bshoshany/OGRe): An Object-Oriented General Relativity Package for Mathematica, or its Python port [OGRePy](https://github.com/bshoshany/OGRePy): An Object-Oriented General Relativity Package for Python. diff --git a/compile_cpp.yaml b/compile_cpp.yaml new file mode 100644 index 0000000..e7970c7 --- /dev/null +++ b/compile_cpp.yaml @@ -0,0 +1,25 @@ +# A list of macros to define when compiling the source files. +defines: [BS_THREAD_POOL_TEST_IMPORT_MODULE, BS_THREAD_POOL_IMPORT_STD, BS_THREAD_POOL_NATIVE_EXTENSIONS] +# A map of flags to pass to each compiler. The compiler should be one of [cl, clang++, g++]. The flags should be a list of strings. +flags: + cl: [/W4] + clang++: [-Wall, -Wextra, -Wconversion, -Wsign-conversion, -Wpedantic, -Wshadow, -Weffc++, -march=native, -fcolor-diagnostics, -fansi-escape-codes, -stdlib=libc++] + g++: [-Wall, -Wextra, -Wconversion, -Wpedantic, -Wshadow, -Wuseless-cast, -march=native, -fdiagnostics-color=always] +# A list of include folders. +includes: [include] +# A map of C++20 modules in the format "module_name: [module_path, dependent files, ...]". Will only be used in C++20 or C++23 mode. The dependent files are any files that the module depends on, and are only used to determine whether the module needs to be recompiled. +modules: + BS.thread_pool: [modules/BS.thread_pool.cppm, include/BS_thread_pool.hpp] +# The output folder for the compiled files. +output: build/ +# A list of arguments to pass to the program if running it after compilation. +pass_args: [] +# A map of paths to the standard library modules for each OS and compiler combination (C++23 only). The OS should be one of [Darwin, Linux, Windows]. This is currently only officially supported by MSVC with Microsoft STL and LLVM Clang (NOT Apple Clang) with LLVM libc++. It is not supported by GCC with any standard library, or any compiler with GNU libstdc++. Use "auto" to determine the path automatically if possible. +std_module: + Darwin: + clang++: auto + Linux: + clang++: auto + Windows: + cl: auto + clang++: auto diff --git a/include/BS_thread_pool.hpp b/include/BS_thread_pool.hpp index 984fc36..e9bb7ce 100644 --- a/include/BS_thread_pool.hpp +++ b/include/BS_thread_pool.hpp @@ -1,163 +1,333 @@ -#ifndef BS_THREAD_POOL_HPP -#define BS_THREAD_POOL_HPP /** + * ██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + * ██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + * ██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ + * * @file BS_thread_pool.hpp - * @author Barak Shoshany (baraksh@gmail.com) (https://baraksh.com) - * @version 4.1.0 - * @date 2024-03-22 + * @author Barak Shoshany (baraksh@gmail.com) (https://baraksh.com/) + * @version 5.0.0 + * @date 2024-12-19 * @copyright Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 * - * @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, modern, and easy-to-use C++17/C++20/C++23 thread pool library. This header file contains the entire library, and is the only file needed to use the library. */ -#ifndef __cpp_exceptions - #define BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING - #undef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK +#ifndef BS_THREAD_POOL_HPP +#define BS_THREAD_POOL_HPP + +// We need to include since if we're using `import std` it will not define any feature-test macros, including `__cpp_lib_modules`, which we need to check if `import std` is supported in the first place. +#ifdef __has_include + #if __has_include() + #include // NOLINT(misc-include-cleaner) + #endif #endif -#include // std::chrono -#include // std::condition_variable -#include // std::size_t -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - #include // std::int_least16_t +// If the macro `BS_THREAD_POOL_IMPORT_STD` is defined, import the C++ Standard Library as a module. Otherwise, include the relevant Standard Library header files. This is currently only officially supported by MSVC with Microsoft STL and LLVM Clang (NOT Apple Clang) with LLVM libc++. It is not supported by GCC with any standard library, or any compiler with GNU libstdc++. We also check that the feature is enabled by checking `__cpp_lib_modules`. However, MSVC defines this macro even in C++20 mode, which is not standards-compliant, so we check that we are in C++23 mode; MSVC currently reports `__cplusplus` as `202004L` for C++23 mode, so we use that value. +#if defined(BS_THREAD_POOL_IMPORT_STD) && defined(__cpp_lib_modules) && (__cplusplus >= 202004L) && (defined(_MSC_VER) || (defined(__clang__) && defined(_LIBCPP_VERSION) && !defined(__apple_build_version__))) + // Only allow importing the `std` module if the library itself is imported as a module. If the library is included as a header file, this will force the program that included the header file to also import `std`, which is not desirable and can lead to compilation errors if the program `#include`s any Standard Library header files. + #ifdef BS_THREAD_POOL_MODULE +import std; + #else + #error "The thread pool library cannot import the C++ Standard Library as a module using `import std` if the library itself is not imported as a module. Either use `import BS.thread_pool` to import the libary, or remove the `BS_THREAD_POOL_IMPORT_STD` macro. Aborting compilation." + #endif +#else + #undef BS_THREAD_POOL_IMPORT_STD + + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + + #ifdef __cpp_concepts + #include + #endif + #ifdef __cpp_exceptions + #include + #include + #endif + #ifdef __cpp_impl_three_way_comparison + #include + #endif + #ifdef __cpp_lib_int_pow2 + #include + #endif + #ifdef __cpp_lib_semaphore + #include + #endif + #ifdef __cpp_lib_jthread + #include + #endif #endif -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING - #include // std::current_exception + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS + #if defined(_WIN32) + #include + #undef min + #undef max + #elif defined(__linux__) || defined(__APPLE__) + #include + #include + #include + #include + #if defined(__linux__) + #include + #include + #endif + #else + #undef BS_THREAD_POOL_NATIVE_EXTENSIONS + #endif #endif -#include // std::function -#include // std::future, std::future_status, std::promise -#include // std::make_shared, std::make_unique, std::shared_ptr, std::unique_ptr -#include // std::mutex, std::scoped_lock, std::unique_lock -#include // std::nullopt, std::optional -#include // std::priority_queue (if priority enabled), std::queue -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - #include // std::runtime_error + +#if defined(__linux__) + // On Linux, defines macros called `major` and `minor`. We undefine them here so the `version` struct can work. + #ifdef major + #undef major + #endif + #ifdef minor + #undef minor + #endif #endif -#include // std::thread -#include // std::conditional_t, std::decay_t, std::invoke_result_t, std::is_void_v, std::remove_const_t (if priority enabled) -#include // std::forward, std::move -#include // std::vector /** * @brief A namespace used by Barak Shoshany's projects. */ namespace BS { // Macros indicating the version of the thread pool library. -#define BS_THREAD_POOL_VERSION_MAJOR 4 -#define BS_THREAD_POOL_VERSION_MINOR 1 +#define BS_THREAD_POOL_VERSION_MAJOR 5 +#define BS_THREAD_POOL_VERSION_MINOR 0 #define BS_THREAD_POOL_VERSION_PATCH 0 -class thread_pool; - /** - * @brief A type to represent the size of things. + * @brief A struct used to store a version number, which can be checked and compared at compilation time. */ -using size_t = std::size_t; +struct version +{ + constexpr version(const std::uint64_t major_, const std::uint64_t minor_, const std::uint64_t patch_) noexcept : major(major_), minor(minor_), patch(patch_) {} -/** - * @brief A convenient shorthand for the type of `std::thread::hardware_concurrency()`. Should evaluate to unsigned int. - */ -using concurrency_t = std::invoke_result_t; - -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY -/** - * @brief A type used to indicate the priority of a task. Defined to be an integer with a width of (at least) 16 bits. - */ -using priority_t = std::int_least16_t; - -/** - * @brief A namespace containing some pre-defined priorities for convenience. - */ -namespace pr { - constexpr priority_t highest = 32767; - constexpr priority_t high = 16383; - constexpr priority_t normal = 0; - constexpr priority_t low = -16384; - constexpr priority_t lowest = -32768; -} // namespace pr - - // Macros used internally to enable or disable the priority arguments in the relevant functions. - #define BS_THREAD_POOL_PRIORITY_INPUT , const priority_t priority = 0 - #define BS_THREAD_POOL_PRIORITY_OUTPUT , priority +// In C++20 and later we can use the spaceship operator `<=>` to automatically generate comparison operators. In C++17 we have to define them manually. +#ifdef __cpp_impl_three_way_comparison + std::strong_ordering operator<=>(const version&) const = default; #else - #define BS_THREAD_POOL_PRIORITY_INPUT - #define BS_THREAD_POOL_PRIORITY_OUTPUT + [[nodiscard]] constexpr friend bool operator==(const version& lhs, const version& rhs) noexcept + { + return std::tuple(lhs.major, lhs.minor, lhs.patch) == std::tuple(rhs.major, rhs.minor, rhs.patch); + } + + [[nodiscard]] constexpr friend bool operator!=(const version& lhs, const version& rhs) noexcept + { + return !(lhs == rhs); + } + + [[nodiscard]] constexpr friend bool operator<(const version& lhs, const version& rhs) noexcept + { + return std::tuple(lhs.major, lhs.minor, lhs.patch) < std::tuple(rhs.major, rhs.minor, rhs.patch); + } + + [[nodiscard]] constexpr friend bool operator>=(const version& lhs, const version& rhs) noexcept + { + return !(lhs < rhs); + } + + [[nodiscard]] constexpr friend bool operator>(const version& lhs, const version& rhs) noexcept + { + return std::tuple(lhs.major, lhs.minor, lhs.patch) > std::tuple(rhs.major, rhs.minor, rhs.patch); + } + + [[nodiscard]] constexpr friend bool operator<=(const version& lhs, const version& rhs) noexcept + { + return !(lhs > rhs); + } +#endif + + [[nodiscard]] std::string to_string() const + { + return std::to_string(major) + '.' + std::to_string(minor) + '.' + std::to_string(patch); + } + + friend std::ostream& operator<<(std::ostream& stream, const version& ver) + { + stream << ver.to_string(); + return stream; + } + + std::uint64_t major; + std::uint64_t minor; + std::uint64_t patch; +}; // struct version + +/** + * @brief The version of the thread pool library. + */ +inline constexpr version thread_pool_version(BS_THREAD_POOL_VERSION_MAJOR, BS_THREAD_POOL_VERSION_MINOR, BS_THREAD_POOL_VERSION_PATCH); + +#ifdef BS_THREAD_POOL_MODULE +// If the library is being compiled as a module, ensure that the version of the module file matches the version of the header file. +static_assert(thread_pool_version == version(BS_THREAD_POOL_MODULE), "The versions of BS.thread_pool.cppm and BS_thread_pool.hpp do not match. Aborting compilation."); +/** + * @brief A flag indicating whether the thread pool library was compiled as a C++20 module. + */ +inline constexpr bool thread_pool_module = true; +#else +/** + * @brief A flag indicating whether the thread pool library was compiled as a C++20 module. + */ +inline constexpr bool thread_pool_module = false; +#endif + +#ifdef BS_THREAD_POOL_IMPORT_STD +/** + * @brief A flag indicating whether the thread pool library imported the C++23 Standard Library module using `import std`. + */ +inline constexpr bool thread_pool_import_std = true; +#else +/** + * @brief A flag indicating whether the thread pool library imported the C++23 Standard Library module using `import std`. + */ +inline constexpr bool thread_pool_import_std = false; +#endif + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS +/** + * @brief A flag indicating whether the thread pool library's native extensions are enabled. + */ +inline constexpr bool thread_pool_native_extensions = true; +#else +/** + * @brief A flag indicating whether the thread pool library's native extensions are enabled. + */ +inline constexpr bool thread_pool_native_extensions = false; #endif /** - * @brief A namespace used to obtain information about the current thread. + * @brief The type used for the bitmask template parameter of the thread pool. */ -namespace this_thread { +using opt_t = std::uint8_t; + +template +class thread_pool; + +#ifdef __cpp_lib_move_only_function +/** + * @brief The template to use to store functions in the task queue and other places. In C++23 and later we use `std::move_only_function`. + */ +template +using function_t = std::move_only_function; +#else +/** + * @brief The template to use to store functions in the task queue and other places. In C++17 we use `std::function`. + */ +template +using function_t = std::function; +#endif + +/** + * @brief The type of tasks in the task queue. + */ +using task_t = function_t; + +#ifdef __cpp_lib_jthread +/** + * @brief The type of threads to use. In C++20 and later we use `std::jthread`. + */ +using thread_t = std::jthread; + // The following macros are used to determine how to stop the workers. In C++20 and later we can use `std::stop_token`. + #define BS_THREAD_POOL_WORKER_TOKEN const std::stop_token &stop_token, + #define BS_THREAD_POOL_WAIT_TOKEN , stop_token + #define BS_THREAD_POOL_STOP_CONDITION stop_token.stop_requested() + #define BS_THREAD_POOL_OR_STOP_CONDITION +#else +/** + * @brief The type of threads to use. In C++17 we use`std::thread`. + */ +using thread_t = std::thread; + // The following macros are used to determine how to stop the workers. In C++17 we use a manual flag `workers_running`. + #define BS_THREAD_POOL_WORKER_TOKEN + #define BS_THREAD_POOL_WAIT_TOKEN + #define BS_THREAD_POOL_STOP_CONDITION !workers_running + #define BS_THREAD_POOL_OR_STOP_CONDITION || !workers_running +#endif + +/** + * @brief A type used to indicate the priority of a task. Defined to be a signed integer with a width of exactly 8 bits (-128 to +127). + */ +using priority_t = std::int8_t; + +/** + * @brief An enum containing some pre-defined priorities for convenience. + */ +enum pr : priority_t +{ + lowest = -128, + low = -64, + normal = 0, + high = +64, + highest = +127 +}; + +/** + * @brief A helper struct to store a task with an assigned priority. + */ +struct [[nodiscard]] pr_task +{ /** - * @brief A type returned by `BS::this_thread::get_index()` which can optionally contain the index of a thread, if that thread belongs to a `BS::thread_pool`. Otherwise, it will contain no value. + * @brief Construct a new task with an assigned priority. + * + * @param task_ The task. + * @param priority_ The desired priority. */ - using optional_index = std::optional; + explicit pr_task(task_t&& task_, const priority_t priority_ = 0) noexcept(std::is_nothrow_move_constructible_v) : task(std::move(task_)), priority(priority_) {} /** - * @brief A type returned by `BS::this_thread::get_pool()` which can optionally contain the pointer to the pool that owns a thread, if that thread belongs to a `BS::thread_pool`. Otherwise, it will contain no value. + * @brief Compare the priority of two tasks. + * + * @param lhs The first task. + * @param rhs The second task. + * @return `true` if the first task has a lower priority than the second task, `false` otherwise. */ - using optional_pool = std::optional; - - /** - * @brief A helper class to store information about the index of the current thread. - */ - class [[nodiscard]] thread_info_index + [[nodiscard]] friend bool operator<(const pr_task& lhs, const pr_task& rhs) noexcept { - friend class BS::thread_pool; - - public: - /** - * @brief Get the index of the current thread. If this thread belongs to a `BS::thread_pool` object, it will have an index from 0 to `BS::thread_pool::get_thread_count() - 1`. Otherwise, for example if this thread is the main thread or an independent `std::thread`, `std::nullopt` will be returned. - * - * @return An `std::optional` object, optionally containing a thread index. Unless you are 100% sure this thread is in a pool, first use `std::optional::has_value()` to check if it contains a value, and if so, use `std::optional::value()` to obtain that value. - */ - [[nodiscard]] optional_index operator()() const - { - return index; - } - - private: - /** - * @brief The index of the current thread. - */ - optional_index index = std::nullopt; - }; // class thread_info_index + return lhs.priority < rhs.priority; + } /** - * @brief A helper class to store information about the thread pool that owns the current thread. + * @brief The task. */ - class [[nodiscard]] thread_info_pool - { - friend class BS::thread_pool; - - public: - /** - * @brief Get the pointer to the thread pool that owns the current thread. If this thread belongs to a `BS::thread_pool` object, a pointer to that object will be returned. Otherwise, for example if this thread is the main thread or an independent `std::thread`, `std::nullopt` will be returned. - * - * @return An `std::optional` object, optionally containing a pointer to a thread pool. Unless you are 100% sure this thread is in a pool, first use `std::optional::has_value()` to check if it contains a value, and if so, use `std::optional::value()` to obtain that value. - */ - [[nodiscard]] optional_pool operator()() const - { - return pool; - } - - private: - /** - * @brief A pointer to the thread pool that owns the current thread. - */ - optional_pool pool = std::nullopt; - }; // class thread_info_pool + task_t task; /** - * @brief A `thread_local` object used to obtain information about the index of the current thread. + * @brief The priority of the task. */ - inline thread_local thread_info_index get_index; + priority_t priority = 0; +}; // struct pr_task - /** - * @brief A `thread_local` object used to obtain information about the thread pool that owns the current thread. - */ - inline thread_local thread_info_pool get_pool; -} // namespace this_thread +// In C++20 and later we can use concepts. In C++17 we instead use SFINAE ("Substitution Failure Is Not An Error") with `std::enable_if_t`. +#ifdef __cpp_concepts + #define BS_THREAD_POOL_IF_PAUSE_ENABLED template requires(P) +template +concept init_func_c = std::invocable || std::invocable; + #define BS_THREAD_POOL_INIT_FUNC_CONCEPT(F) init_func_c F +#else + #define BS_THREAD_POOL_IF_PAUSE_ENABLED template > + #define BS_THREAD_POOL_INIT_FUNC_CONCEPT(F) typename F, typename = std::enable_if_t || std::is_invocable_v> // NOLINT(bugprone-macro-parentheses) +#endif /** * @brief A helper class to facilitate waiting for and/or getting the results of multiple futures at once. @@ -171,16 +341,8 @@ public: // Inherit all constructors from the base class `std::vector`. using std::vector>::vector; - // The copy constructor and copy assignment operator are deleted. The elements stored in a `multi_future` are futures, which cannot be copied. - multi_future(const multi_future&) = delete; - multi_future& operator=(const multi_future&) = delete; - - // The move constructor and move assignment operator are defaulted. - multi_future(multi_future&&) = default; - multi_future& operator=(multi_future&&) = default; - /** - * @brief Get the results from all the futures stored in this `multi_future`, rethrowing any stored exceptions. + * @brief Get the results from all the futures stored in this `BS::multi_future`, rethrowing any stored exceptions. * * @return If the futures return `void`, this function returns `void` as well. Otherwise, it returns a vector containing the results. */ @@ -203,13 +365,13 @@ public: } /** - * @brief Check how many of the futures stored in this `multi_future` are ready. + * @brief Check how many of the futures stored in this `BS::multi_future` are ready. * * @return The number of ready futures. */ - [[nodiscard]] size_t ready_count() const + [[nodiscard]] std::size_t ready_count() const { - size_t count = 0; + std::size_t count = 0; for (const std::future& future : *this) { if (future.wait_for(std::chrono::duration::zero()) == std::future_status::ready) @@ -219,11 +381,11 @@ public: } /** - * @brief Check if all the futures stored in this `multi_future` are valid. + * @brief Check if all the futures stored in this `BS::multi_future` are valid. * * @return `true` if all futures are valid, `false` if at least one of the futures is not valid. */ - [[nodiscard]] bool valid() const + [[nodiscard]] bool valid() const noexcept { bool is_valid = true; for (const std::future& future : *this) @@ -232,7 +394,7 @@ public: } /** - * @brief Wait for all the futures stored in this `multi_future`. + * @brief Wait for all the futures stored in this `BS::multi_future`. */ void wait() const { @@ -241,7 +403,7 @@ public: } /** - * @brief Wait for all the futures stored in this `multi_future`, but stop waiting after the specified duration has passed. This function first waits for the first future for the desired duration. If that future is ready before the duration expires, this function waits for the second future for whatever remains of the duration. It continues similarly until the duration expires. + * @brief Wait for all the futures stored in this `BS::multi_future`, but stop waiting after the specified duration has passed. This function first waits for the first future for the desired duration. If that future is ready before the duration expires, this function waits for the second future for whatever remains of the duration. It continues similarly until the duration expires. * * @tparam R An arithmetic type representing the number of ticks to wait. * @tparam P An `std::ratio` representing the length of each tick in seconds. @@ -262,7 +424,7 @@ public: } /** - * @brief Wait for all the futures stored in this `multi_future`, but stop waiting after the specified time point has been reached. This function first waits for the first future until the desired time point. If that future is ready before the time point is reached, this function waits for the second future until the desired time point. It continues similarly until the time point is reached. + * @brief Wait for all the futures stored in this `BS::multi_future`, but stop waiting after the specified time point has been reached. This function first waits for the first future until the desired time point. If that future is ready before the time point is reached, this function waits for the second future until the desired time point. It continues similarly until the time point is reached. * * @tparam C The type of the clock used to measure time. * @tparam D An `std::chrono::duration` type used to indicate the time point. @@ -283,11 +445,806 @@ public: }; // class multi_future /** - * @brief A fast, lightweight, and easy-to-use C++17 thread pool class. + * @brief A helper class to divide a range into blocks. Used by `detach_blocks()`, `submit_blocks()`, `detach_loop()`, and `submit_loop()`. + * + * @tparam T The type of the indices. Should be a signed or unsigned integer. */ +template +class [[nodiscard]] blocks +{ +public: + /** + * @brief Construct a `blocks` object with the given specifications. + * + * @param first_index_ The first index in the range. + * @param index_after_last_ The index after the last index in the range. + * @param num_blocks_ The desired number of blocks to divide the range into. + */ + blocks(const T first_index_, const T index_after_last_, const std::size_t num_blocks_) noexcept : first_index(first_index_), index_after_last(index_after_last_), num_blocks(num_blocks_) + { + if (index_after_last > first_index) + { + const std::size_t total_size = static_cast(index_after_last - first_index); + num_blocks = std::min(num_blocks, total_size); + block_size = total_size / num_blocks; + remainder = total_size % num_blocks; + if (block_size == 0) + { + block_size = 1; + num_blocks = (total_size > 1) ? total_size : 1; + } + } + else + { + num_blocks = 0; + } + } + + /** + * @brief Get the index after the last index of a block. + * + * @param block The block number. + * @return The index after the last index. + */ + [[nodiscard]] T end(const std::size_t block) const noexcept + { + return (block == num_blocks - 1) ? index_after_last : start(block + 1); + } + + /** + * @brief Get the number of blocks. Note that this may be different than the desired number of blocks that was passed to the constructor. + * + * @return The number of blocks. + */ + [[nodiscard]] std::size_t get_num_blocks() const noexcept + { + return num_blocks; + } + + /** + * @brief Get the first index of a block. + * + * @param block The block number. + * @return The first index. + */ + [[nodiscard]] T start(const std::size_t block) const noexcept + { + return first_index + static_cast(block * block_size) + static_cast(block < remainder ? block : remainder); + } + +private: + /** + * @brief The size of each block (except possibly the last block). + */ + std::size_t block_size = 0; + + /** + * @brief The first index in the range. + */ + T first_index = 0; + + /** + * @brief The index after the last index in the range. + */ + T index_after_last = 0; + + /** + * @brief The number of blocks. + */ + std::size_t num_blocks = 0; + + /** + * @brief The remainder obtained after dividing the total size by the number of blocks. + */ + std::size_t remainder = 0; +}; // class blocks + +#ifdef __cpp_exceptions +/** + * @brief An exception that will be thrown by `wait()`, `wait_for()`, and `wait_until()` if the user tries to call them from within a thread of the same pool, which would result in a deadlock. Only used if the flag `BS:tp::wait_deadlock_checks` is enabled in the template parameter of `BS::thread_pool`. + */ +struct wait_deadlock : public std::runtime_error +{ + wait_deadlock() : std::runtime_error("BS::wait_deadlock") {}; +}; +#endif + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS + #if defined(_WIN32) +/** + * @brief An enum containing pre-defined OS-specific process priority values for portability. + */ +enum class os_process_priority +{ + idle = IDLE_PRIORITY_CLASS, + below_normal = BELOW_NORMAL_PRIORITY_CLASS, + normal = NORMAL_PRIORITY_CLASS, + above_normal = ABOVE_NORMAL_PRIORITY_CLASS, + high = HIGH_PRIORITY_CLASS, + realtime = REALTIME_PRIORITY_CLASS +}; + +/** + * @brief An enum containing pre-defined OS-specific thread priority values for portability. + */ +enum class os_thread_priority +{ + idle = THREAD_PRIORITY_IDLE, + lowest = THREAD_PRIORITY_LOWEST, + below_normal = THREAD_PRIORITY_BELOW_NORMAL, + normal = THREAD_PRIORITY_NORMAL, + above_normal = THREAD_PRIORITY_ABOVE_NORMAL, + highest = THREAD_PRIORITY_HIGHEST, + realtime = THREAD_PRIORITY_TIME_CRITICAL +}; + #elif defined(__linux__) || defined(__APPLE__) +/** + * @brief An enum containing pre-defined OS-specific process priority values for portability. + */ +enum class os_process_priority +{ + idle = PRIO_MAX - 2, + below_normal = PRIO_MAX / 2, + normal = 0, + above_normal = PRIO_MIN / 3, + high = PRIO_MIN * 2 / 3, + realtime = PRIO_MIN +}; + +/** + * @brief An enum containing pre-defined OS-specific thread priority values for portability. + */ +enum class os_thread_priority +{ + idle, + lowest, + below_normal, + normal, + above_normal, + highest, + realtime +}; + #endif + +/** + * @brief Get the processor affinity of the current process using the current platform's native API. This should work on Windows and Linux, but is not possible on macOS as the native API does not allow it. + * + * @return An `std::optional` object, optionally containing the processor affinity of the current process as an `std::vector` where each element corresponds to a logical processor. If the returned object does not contain a value, then the affinity could not be determined. On macOS, this function always returns `std::nullopt`. + */ +[[nodiscard]] inline std::optional> get_os_process_affinity() +{ + #if defined(_WIN32) + DWORD_PTR process_mask = 0; + DWORD_PTR system_mask = 0; + if (GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask) == 0) + return std::nullopt; + #ifdef __cpp_lib_int_pow2 + const std::size_t num_cpus = static_cast(std::bit_width(system_mask)); + #else + std::size_t num_cpus = 0; + if (system_mask != 0) + { + num_cpus = 1; + while ((system_mask >>= 1U) != 0U) + ++num_cpus; + } + #endif + std::vector affinity(num_cpus); + for (std::size_t i = 0; i < num_cpus; ++i) + affinity[i] = ((process_mask & (1ULL << i)) != 0ULL); + return affinity; + #elif defined(__linux__) + cpu_set_t cpu_set; + CPU_ZERO(&cpu_set); + if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &cpu_set) != 0) + return std::nullopt; + const int num_cpus = get_nprocs(); + if (num_cpus < 1) + return std::nullopt; + std::vector affinity(static_cast(num_cpus)); + for (std::size_t i = 0; i < affinity.size(); ++i) + affinity[i] = CPU_ISSET(i, &cpu_set); + return affinity; + #elif defined(__APPLE__) + return std::nullopt; + #endif +} + +/** + * @brief Set the processor affinity of the current process using the current platform's native API. This should work on Windows and Linux, but is not possible on macOS as the native API does not allow it. + * + * @param affinity The processor affinity to set, as an `std::vector` where each element corresponds to a logical processor. + * @return `true` if the affinity was set successfully, `false` otherwise. On macOS, this function always returns `false`. + */ +inline bool set_os_process_affinity(const std::vector& affinity) +{ + #if defined(_WIN32) + DWORD_PTR process_mask = 0; + for (std::size_t i = 0; i < std::min(affinity.size(), sizeof(DWORD_PTR) * 8); ++i) + process_mask |= (affinity[i] ? (1ULL << i) : 0ULL); + return SetProcessAffinityMask(GetCurrentProcess(), process_mask) != 0; + #elif defined(__linux__) + cpu_set_t cpu_set; + CPU_ZERO(&cpu_set); + for (std::size_t i = 0; i < std::min(affinity.size(), CPU_SETSIZE); ++i) + { + if (affinity[i]) + CPU_SET(i, &cpu_set); + } + return sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpu_set) == 0; + #elif defined(__APPLE__) + return affinity[0] && false; // NOLINT(readability-simplify-boolean-expr) // Using `affinity` to suppress unused parameter warning. + #endif +} + +/** + * @brief Get the priority of the current process using the current platform's native API. This should work on Windows, Linux, and macOS. + * + * @return An `std::optional` object, optionally containing the priority of the current process, as a member of the enum `BS::os_process_priority`. If the returned object does not contain a value, then either the priority could not be determined, or it is not one of the pre-defined values and therefore cannot be represented in a portable way. + */ +[[nodiscard]] inline std::optional get_os_process_priority() +{ + #if defined(_WIN32) + // On Windows, this is straightforward. + const DWORD priority = GetPriorityClass(GetCurrentProcess()); + if (priority == 0) + return std::nullopt; + return static_cast(priority); + #elif defined(__linux__) || defined(__APPLE__) + // On Linux/macOS there is no direct analogue of `GetPriorityClass()` on Windows, so instead we get the "nice" value. The usual range is -20 to 19 or 20, with higher values corresponding to lower priorities. However, we are only using 6 pre-defined values for portability, so if the value was set via any means other than `BS::set_os_process_priority()`, it may not match one of our pre-defined values. Note that `getpriority()` returns -1 on error, but since this does not correspond to any of our pre-defined values, this function will return `std::nullopt` anyway. + const int nice_val = getpriority(PRIO_PROCESS, static_cast(getpid())); + switch (nice_val) + { + case static_cast(os_process_priority::idle): + return os_process_priority::idle; + case static_cast(os_process_priority::below_normal): + return os_process_priority::below_normal; + case static_cast(os_process_priority::normal): + return os_process_priority::normal; + case static_cast(os_process_priority::above_normal): + return os_process_priority::above_normal; + case static_cast(os_process_priority::high): + return os_process_priority::high; + case static_cast(os_process_priority::realtime): + return os_process_priority::realtime; + default: + return std::nullopt; + } + #endif +} + +/** + * @brief Set the priority of the current process using the current platform's native API. This should work on Windows, Linux, and macOS. However, note that higher priorities might require elevated permissions. + * + * @param priority The priority to set. Must be a value from the enum `BS::os_process_priority`. + * @return `true` if the priority was set successfully, `false` otherwise. Usually, `false` means that the user does not have the necessary permissions to set the desired priority. + */ +inline bool set_os_process_priority(const os_process_priority priority) +{ + #if defined(_WIN32) + // On Windows, this is straightforward. + return SetPriorityClass(GetCurrentProcess(), static_cast(priority)) != 0; + #elif defined(__linux__) || defined(__APPLE__) + // On Linux/macOS there is no direct analogue of `SetPriorityClass()` on Windows, so instead we set the "nice" value. The usual range is -20 to 19 or 20, with higher values corresponding to lower priorities. However, we are only using 6 pre-defined values for portability. Note that the "nice" values are only relevant for the `SCHED_OTHER` policy, but we do not set that policy here, as it is per-thread rather than per-process. + // Also, it's important to note that a non-root user cannot decrease the nice value (i.e. increase the process priority), only increase it. This can cause confusing behavior. For example, if the current priority is `BS::os_process_priority::normal` and the user sets it to `BS::os_process_priority::idle`, they cannot change it back `BS::os_process_priority::normal`. + return setpriority(PRIO_PROCESS, static_cast(getpid()), static_cast(priority)) == 0; + #endif +} +#endif + +/** + * @brief A class used to obtain information about the current thread and, if native extensions are enabled, set its priority and affinity. + */ +class [[nodiscard]] this_thread +{ + template + friend class thread_pool; + +public: + /** + * @brief Get the index of the current thread. If this thread belongs to a `BS::thread_pool` object, the return value will be an index in the range `[0, N)` where `N == BS::thread_pool::get_thread_count()`. Otherwise, for example if this thread is the main thread or an independent thread not in any pools, `std::nullopt` will be returned. + * + * @return An `std::optional` object, optionally containing a thread index. + */ + [[nodiscard]] static std::optional get_index() noexcept + { + return my_index; + } + + /** + * @brief Get a pointer to the thread pool that owns the current thread. If this thread belongs to a `BS::thread_pool` object, the return value will be a `void` pointer to that object. Otherwise, for example if this thread is the main thread or an independent thread not in any pools, `std::nullopt` will be returned. + * + * @return An `std::optional` object, optionally containing a pointer to a thread pool. Note that this will be a `void` pointer, so it must be cast to the desired instantiation of the `BS::thread_pool` template in order to use any member functions. + */ + [[nodiscard]] static std::optional get_pool() noexcept + { + return my_pool; + } + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS + /** + * @brief Get the processor affinity of the current thread using the current platform's native API. This should work on Windows and Linux, but is not possible on macOS as the native API does not allow it. + * + * @return An `std::optional` object, optionally containing the processor affinity of the current thread as an `std::vector` where each element corresponds to a logical processor. If the returned object does not contain a value, then the affinity could not be determined. On macOS, this function always returns `std::nullopt`. + */ + [[nodiscard]] static std::optional> get_os_thread_affinity() + { + #if defined(_WIN32) + // Windows does not have a `GetThreadAffinityMask()` function, but `SetThreadAffinityMask()` returns the previous affinity mask, so we can use that to get the current affinity and then restore it. It's a bit of a hack, but it works. Since the thread affinity must be a subset of the process affinity, we use the process affinity as the temporary value. + DWORD_PTR process_mask = 0; + DWORD_PTR system_mask = 0; + if (GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask) == 0) + return std::nullopt; + const DWORD_PTR previous_mask = SetThreadAffinityMask(GetCurrentThread(), process_mask); + if (previous_mask == 0) + return std::nullopt; + SetThreadAffinityMask(GetCurrentThread(), previous_mask); + #ifdef __cpp_lib_int_pow2 + const std::size_t num_cpus = static_cast(std::bit_width(system_mask)); + #else + std::size_t num_cpus = 0; + if (system_mask != 0) + { + num_cpus = 1; + while ((system_mask >>= 1U) != 0U) + ++num_cpus; + } + #endif + std::vector affinity(num_cpus); + for (std::size_t i = 0; i < num_cpus; ++i) + affinity[i] = ((previous_mask & (1ULL << i)) != 0ULL); + return affinity; + #elif defined(__linux__) + cpu_set_t cpu_set; + CPU_ZERO(&cpu_set); + if (pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set) != 0) + return std::nullopt; + const int num_cpus = get_nprocs(); + if (num_cpus < 1) + return std::nullopt; + std::vector affinity(static_cast(num_cpus)); + for (std::size_t i = 0; i < affinity.size(); ++i) + affinity[i] = CPU_ISSET(i, &cpu_set); + return affinity; + #elif defined(__APPLE__) + return std::nullopt; + #endif + } + + /** + * @brief Set the processor affinity of the current thread using the current platform's native API. This should work on Windows and Linux, but is not possible on macOS as the native API does not allow it. Note that the thread affinity must be a subset of the process affinity (as obtained using `BS::get_os_process_affinity()`) for the containing process of a thread. + * + * @param affinity The processor affinity to set, as an `std::vector` where each element corresponds to a logical processor. + * @return `true` if the affinity was set successfully, `false` otherwise. On macOS, this function always returns `false`. + */ + static bool set_os_thread_affinity(const std::vector& affinity) + { + #if defined(_WIN32) + DWORD_PTR thread_mask = 0; + for (std::size_t i = 0; i < std::min(affinity.size(), sizeof(DWORD_PTR) * 8); ++i) + thread_mask |= (affinity[i] ? (1ULL << i) : 0ULL); + return SetThreadAffinityMask(GetCurrentThread(), thread_mask) != 0; + #elif defined(__linux__) + cpu_set_t cpu_set; + CPU_ZERO(&cpu_set); + for (std::size_t i = 0; i < std::min(affinity.size(), CPU_SETSIZE); ++i) + { + if (affinity[i]) + CPU_SET(i, &cpu_set); + } + return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set) == 0; + #elif defined(__APPLE__) + return affinity[0] && false; // NOLINT(readability-simplify-boolean-expr) // Using `affinity` to suppress unused parameter warning. + #endif + } + + /** + * @brief Get the name of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS. + * + * @return An `std::optional` object, optionally containing the name of the current thread. If the returned object does not contain a value, then the name could not be determined. + */ + [[nodiscard]] static std::optional get_os_thread_name() + { + #if defined(_WIN32) + // On Windows thread names are wide strings, so we need to convert them to normal strings. + PWSTR data = nullptr; + const HRESULT hr = GetThreadDescription(GetCurrentThread(), &data); + if (FAILED(hr)) + return std::nullopt; + if (data == nullptr) + return std::nullopt; + const int size = WideCharToMultiByte(CP_UTF8, 0, data, -1, nullptr, 0, nullptr, nullptr); + if (size == 0) + { + LocalFree(data); + return std::nullopt; + } + std::string name(static_cast(size) - 1, 0); + const int result = WideCharToMultiByte(CP_UTF8, 0, data, -1, name.data(), size, nullptr, nullptr); + LocalFree(data); + if (result == 0) + return std::nullopt; + return name; + #elif defined(__linux__) || defined(__APPLE__) + #ifdef __linux__ + // On Linux thread names are limited to 16 characters, including the null terminator. + constexpr std::size_t buffer_size = 16; + #else + // On macOS thread names are limited to 64 characters, including the null terminator. + constexpr std::size_t buffer_size = 64; + #endif + char name[buffer_size] = {}; + if (pthread_getname_np(pthread_self(), name, buffer_size) != 0) + return std::nullopt; + return std::string(name); + #endif + } + + /** + * @brief Set the name of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS. Note that on Linux thread names are limited to 16 characters, including the null terminator. + * + * @param name The name to set. + * @return `true` if the name was set successfully, `false` otherwise. + */ + static bool set_os_thread_name(const std::string& name) + { + #if defined(_WIN32) + // On Windows thread names are wide strings, so we need to convert them from normal strings. + const int size = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0); + if (size == 0) + return false; + std::wstring wide(static_cast(size), 0); + if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, wide.data(), size) == 0) + return false; + const HRESULT hr = SetThreadDescription(GetCurrentThread(), wide.data()); + return SUCCEEDED(hr); + #elif defined(__linux__) + // On Linux this is straightforward. + return pthread_setname_np(pthread_self(), name.data()) == 0; + #elif defined(__APPLE__) + // On macOS, unlike Linux, a thread can only set a name for itself, so the signature is different. + return pthread_setname_np(name.data()) == 0; + #endif + } + + /** + * @brief Get the priority of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS. + * + * @return An `std::optional` object, optionally containing the priority of the current thread, as a member of the enum `BS::os_thread_priority`. If the returned object does not contain a value, then either the priority could not be determined, or it is not one of the pre-defined values. + */ + [[nodiscard]] static std::optional get_os_thread_priority() + { + #if defined(_WIN32) + // On Windows, this is straightforward. + const int priority = GetThreadPriority(GetCurrentThread()); + if (priority == THREAD_PRIORITY_ERROR_RETURN) + return std::nullopt; + return static_cast(priority); + #elif defined(__linux__) + // On Linux, we distill the choices of scheduling policy, priority, and "nice" value into 7 pre-defined levels, for simplicity and portability. The total number of possible combinations of policies and priorities is much larger, so if the value was set via any means other than `BS::this_thread::set_os_thread_priority()`, it may not match one of our pre-defined values. + int policy = 0; + struct sched_param param = {}; + if (pthread_getschedparam(pthread_self(), &policy, ¶m) != 0) + return std::nullopt; + if (policy == SCHED_FIFO && param.sched_priority == sched_get_priority_max(SCHED_FIFO)) + { + // The only pre-defined priority that uses SCHED_FIFO and the maximum available priority value is the "realtime" priority. + return os_thread_priority::realtime; + } + if (policy == SCHED_RR && param.sched_priority == sched_get_priority_min(SCHED_RR) + (sched_get_priority_max(SCHED_RR) - sched_get_priority_min(SCHED_RR)) / 2) + { + // The only pre-defined priority that uses SCHED_RR and a priority in the middle of the available range is the "highest" priority. + return os_thread_priority::highest; + } + #ifdef __linux__ + if (policy == SCHED_IDLE) + { + // The only pre-defined priority that uses SCHED_IDLE is the "idle" priority. Note that this scheduling policy is not available on macOS. + return os_thread_priority::idle; + } + #endif + if (policy == SCHED_OTHER) + { + // For SCHED_OTHER, the result depends on the "nice" value. The usual range is -20 to 19 or 20, with higher values corresponding to lower priorities. Note that `getpriority()` returns -1 on error, but since this does not correspond to any of our pre-defined values, this function will return `std::nullopt` anyway. + const int nice_val = getpriority(PRIO_PROCESS, static_cast(syscall(SYS_gettid))); + switch (nice_val) + { + case PRIO_MIN + 2: + return os_thread_priority::above_normal; + case 0: + return os_thread_priority::normal; + case (PRIO_MAX / 2) + (PRIO_MAX % 2): + return os_thread_priority::below_normal; + case PRIO_MAX - 3: + return os_thread_priority::lowest; + #ifdef __APPLE__ + // `SCHED_IDLE` doesn't exist on macOS, so we use the policy `SCHED_OTHER` with a "nice" value of `PRIO_MAX - 2`. + case PRIO_MAX - 2: + return os_thread_priority::idle; + #endif + default: + return std::nullopt; + } + } + return std::nullopt; + #elif defined(__APPLE__) + // On macOS, we distill the choices of scheduling policy and priority into 7 pre-defined levels, for simplicity and portability. The total number of possible combinations of policies and priorities is much larger, so if the value was set via any means other than `BS::this_thread::set_os_thread_priority()`, it may not match one of our pre-defined values. + int policy = 0; + struct sched_param param = {}; + if (pthread_getschedparam(pthread_self(), &policy, ¶m) != 0) + return std::nullopt; + if (policy == SCHED_FIFO && param.sched_priority == sched_get_priority_max(SCHED_FIFO)) + { + // The only pre-defined priority that uses SCHED_FIFO and the maximum available priority value is the "realtime" priority. + return os_thread_priority::realtime; + } + if (policy == SCHED_RR && param.sched_priority == sched_get_priority_min(SCHED_RR) + (sched_get_priority_max(SCHED_RR) - sched_get_priority_min(SCHED_RR)) / 2) + { + // The only pre-defined priority that uses SCHED_RR and a priority in the middle of the available range is the "highest" priority. + return os_thread_priority::highest; + } + if (policy == SCHED_OTHER) + { + // For SCHED_OTHER, the result depends on the specific value of the priority. + if (param.sched_priority == sched_get_priority_max(SCHED_OTHER)) + return os_thread_priority::above_normal; + if (param.sched_priority == sched_get_priority_min(SCHED_OTHER) + (sched_get_priority_max(SCHED_OTHER) - sched_get_priority_min(SCHED_OTHER)) / 2) + return os_thread_priority::normal; + if (param.sched_priority == sched_get_priority_min(SCHED_OTHER) + (sched_get_priority_max(SCHED_OTHER) - sched_get_priority_min(SCHED_OTHER)) * 2 / 3) + return os_thread_priority::below_normal; + if (param.sched_priority == sched_get_priority_min(SCHED_OTHER) + (sched_get_priority_max(SCHED_OTHER) - sched_get_priority_min(SCHED_OTHER)) / 3) + return os_thread_priority::lowest; + if (param.sched_priority == sched_get_priority_min(SCHED_OTHER)) + return os_thread_priority::idle; + return std::nullopt; + } + return std::nullopt; + #endif + } + + /** + * @brief Set the priority of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS. However, note that higher priorities might require elevated permissions. + * + * @param priority The priority to set. Must be a value from the enum `BS::os_thread_priority`. + * @return `true` if the priority was set successfully, `false` otherwise. Usually, `false` means that the user does not have the necessary permissions to set the desired priority. + */ + static bool set_os_thread_priority(const os_thread_priority priority) + { + #if defined(_WIN32) + // On Windows, this is straightforward. + return SetThreadPriority(GetCurrentThread(), static_cast(priority)) != 0; + #elif defined(__linux__) + // On Linux, we distill the choices of scheduling policy, priority, and "nice" value into 7 pre-defined levels, for simplicity and portability. The total number of possible combinations of policies and priorities is much larger, but allowing more fine-grained control would not be portable. + int policy = 0; + struct sched_param param = {}; + std::optional nice_val = std::nullopt; + switch (priority) + { + case os_thread_priority::realtime: + // "Realtime" pre-defined priority: We use the policy `SCHED_FIFO` with the highest possible priority. + policy = SCHED_FIFO; + param.sched_priority = sched_get_priority_max(SCHED_FIFO); + break; + case os_thread_priority::highest: + // "Highest" pre-defined priority: We use the policy `SCHED_RR` ("round-robin") with a priority in the middle of the available range. + policy = SCHED_RR; + param.sched_priority = sched_get_priority_min(SCHED_RR) + (sched_get_priority_max(SCHED_RR) - sched_get_priority_min(SCHED_RR)) / 2; + break; + case os_thread_priority::above_normal: + // "Above normal" pre-defined priority: We use the policy `SCHED_OTHER` (the default). This policy does not accept a priority value, so priority must be 0. However, we set the "nice" value to the minimum value as given by `PRIO_MIN`, plus 2 (which should evaluate to -18). The usual range is -20 to 19 or 20, with higher values corresponding to lower priorities. + policy = SCHED_OTHER; + param.sched_priority = 0; + nice_val = PRIO_MIN + 2; + break; + case os_thread_priority::normal: + // "Normal" pre-defined priority: We use the policy `SCHED_OTHER`, priority must be 0, and we set the "nice" value to 0 (the default). + policy = SCHED_OTHER; + param.sched_priority = 0; + nice_val = 0; + break; + case os_thread_priority::below_normal: + // "Below normal" pre-defined priority: We use the policy `SCHED_OTHER`, priority must be 0, and we set the "nice" value to half the maximum value as given by `PRIO_MAX`, rounded up (which should evaluate to 10). + policy = SCHED_OTHER; + param.sched_priority = 0; + nice_val = (PRIO_MAX / 2) + (PRIO_MAX % 2); + break; + case os_thread_priority::lowest: + // "Lowest" pre-defined priority: We use the policy `SCHED_OTHER`, priority must be 0, and we set the "nice" value to the maximum value as given by `PRIO_MAX`, minus 3 (which should evaluate to 17). + policy = SCHED_OTHER; + param.sched_priority = 0; + nice_val = PRIO_MAX - 3; + break; + case os_thread_priority::idle: + // "Idle" pre-defined priority on Linux: We use the policy `SCHED_IDLE`, priority must be 0, and we don't touch the "nice" value. + policy = SCHED_IDLE; + param.sched_priority = 0; + break; + default: + return false; + } + bool success = (pthread_setschedparam(pthread_self(), policy, ¶m) == 0); + if (nice_val.has_value()) + success = success && (setpriority(PRIO_PROCESS, static_cast(syscall(SYS_gettid)), nice_val.value()) == 0); + return success; + #elif defined(__APPLE__) + // On macOS, unlike Linux, the "nice" value is per-process, not per-thread (in compliance with the POSIX standard). However, unlike Linux, `SCHED_OTHER` on macOS does have a range of priorities. So for `realtime` and `highest` priorities we use `SCHED_FIFO` and `SCHED_RR` respectively as for Linux, but for the other priorities we use `SCHED_OTHER` with a priority in the range given by `sched_get_priority_min(SCHED_OTHER)` to `sched_get_priority_max(SCHED_OTHER)`. + int policy = 0; + struct sched_param param = {}; + switch (priority) + { + case os_thread_priority::realtime: + // "Realtime" pre-defined priority: We use the policy `SCHED_FIFO` with the highest possible priority. + policy = SCHED_FIFO; + param.sched_priority = sched_get_priority_max(SCHED_FIFO); + break; + case os_thread_priority::highest: + // "Highest" pre-defined priority: We use the policy `SCHED_RR` ("round-robin") with a priority in the middle of the available range. + policy = SCHED_RR; + param.sched_priority = sched_get_priority_min(SCHED_RR) + (sched_get_priority_max(SCHED_RR) - sched_get_priority_min(SCHED_RR)) / 2; + break; + case os_thread_priority::above_normal: + // "Above normal" pre-defined priority: We use the policy `SCHED_OTHER` (the default) with the highest possible priority. + policy = SCHED_OTHER; + param.sched_priority = sched_get_priority_max(SCHED_OTHER); + break; + case os_thread_priority::normal: + // "Normal" pre-defined priority: We use the policy `SCHED_OTHER` (the default) with a priority in the middle of the available range (which appears to be the default?). + policy = SCHED_OTHER; + param.sched_priority = sched_get_priority_min(SCHED_OTHER) + (sched_get_priority_max(SCHED_OTHER) - sched_get_priority_min(SCHED_OTHER)) / 2; + break; + case os_thread_priority::below_normal: + // "Below normal" pre-defined priority: We use the policy `SCHED_OTHER` (the default) with a priority equal to 2/3rds of the normal value. + policy = SCHED_OTHER; + param.sched_priority = sched_get_priority_min(SCHED_OTHER) + (sched_get_priority_max(SCHED_OTHER) - sched_get_priority_min(SCHED_OTHER)) * 2 / 3; + break; + case os_thread_priority::lowest: + // "Lowest" pre-defined priority: We use the policy `SCHED_OTHER` (the default) with a priority equal to 1/3rd of the normal value. + policy = SCHED_OTHER; + param.sched_priority = sched_get_priority_min(SCHED_OTHER) + (sched_get_priority_max(SCHED_OTHER) - sched_get_priority_min(SCHED_OTHER)) / 3; + break; + case os_thread_priority::idle: + // "Idle" pre-defined priority on macOS: We use the policy `SCHED_OTHER` (the default) with the lowest possible priority. + policy = SCHED_OTHER; + param.sched_priority = sched_get_priority_min(SCHED_OTHER); + break; + default: + return false; + } + return pthread_setschedparam(pthread_self(), policy, ¶m) == 0; + #endif + } +#endif + +private: + inline static thread_local std::optional my_index = std::nullopt; + inline static thread_local std::optional my_pool = std::nullopt; +}; // class this_thread + +/** + * @brief A meta-programming template to determine the common type of two integer types. Unlike `std::common_type`, this template maintains correct signedness. + * + * @tparam T1 The first type. + * @tparam T2 The second type. + * @tparam Enable A dummy parameter to enable SFINAE in specializations. + */ +template +struct common_index_type +{ + // Fallback to `std::common_type_t` if no specialization matches. + using type = std::common_type_t; +}; + +// The common type of two signed integers is the larger of the integers, with the same signedness. +template +struct common_index_type && std::is_signed_v>> +{ + using type = std::conditional_t<(sizeof(T1) >= sizeof(T2)), T1, T2>; +}; + +// The common type of two unsigned integers is the larger of the integers, with the same signedness. +template +struct common_index_type && std::is_unsigned_v>> +{ + using type = std::conditional_t<(sizeof(T1) >= sizeof(T2)), T1, T2>; +}; + +// The common type of a signed and an unsigned integer is a signed integer that can hold the full ranges of both integers. +template +struct common_index_type && std::is_unsigned_v) || (std::is_unsigned_v && std::is_signed_v)>> +{ + using S = std::conditional_t, T1, T2>; + using U = std::conditional_t, T1, T2>; + static constexpr std::size_t larger_size = (sizeof(S) > sizeof(U)) ? sizeof(S) : sizeof(U); + using type = std::conditional_t>, + // If the unsigned integer is 64 bits, the common type should also be an unsigned 64-bit integer, that is, `std::uint64_t`. The reason is that the most common scenario where this might happen is where the indices go from 0 to `x` where `x` has been previously defined as `std::size_t`, e.g. the size of a vector. Note that this will fail if the first index is negative; in that case, the user must cast the indices explicitly to the desired common type. If the unsigned integer is not 64 bits, then the signed integer must be 64 bits, hence the common type is `std::int64_t`. + std::conditional_t>; +}; + +/** + * @brief A helper type alias to obtain the common type from the template `BS::common_index_type`. + * + * @tparam T1 The first type. + * @tparam T2 The second type. + */ +template +using common_index_type_t = typename common_index_type::type; + +/** + * @brief An enumeration of flags to be used in the bitmask template parameter of `BS::thread_pool` to enable optional features. + */ +enum tp : opt_t +{ + /** + * @brief No optional features enabled. + */ + none = 0, + + /** + * @brief Enable task priority. + */ + priority = 1 << 0, + + /** + * @brief Enable pausing. + */ + pause = 1 << 2, + + /** + * @brief Enable wait deadlock checks. + */ + wait_deadlock_checks = 1 << 3 +}; + +/** + * @brief A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with all optional features disabled. + */ +using light_thread_pool = thread_pool; + +/** + * @brief A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with task priority enabled. + */ +using priority_thread_pool = thread_pool; + +/** + * @brief A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with pausing enabled. + */ +using pause_thread_pool = thread_pool; + +/** + * @brief A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. This alias defines a thread pool with wait deadlock checks enabled. + */ +using wdc_thread_pool = thread_pool; + +/** + * @brief A fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool class. + * + * @tparam OptFlags A bitmask of flags which can be used to enable optional features. The flags are members of the `BS::tp` enumeration: `BS::tp::priority`, `BS::tp::pause`, and `BS::tp::wait_deadlock_checks`. The default is `BS::tp::none`, which disables all optional features. To enable multiple features, use the bitwise OR operator `|`, e.g. `BS::tp::priority | BS::tp::pause`. + */ +template class [[nodiscard]] thread_pool { public: + /** + * @brief A flag indicating whether task priority is enabled. + */ + static constexpr bool priority_enabled = (OptFlags & tp::priority) != 0; + + /** + * @brief A flag indicating whether pausing is enabled. + */ + static constexpr bool pause_enabled = (OptFlags & tp::pause) != 0; + + /** + * @brief A flag indicating whether wait deadlock checks are enabled. + */ + static constexpr bool wait_deadlock_checks_enabled = (OptFlags & tp::wait_deadlock_checks) != 0; + +#ifndef __cpp_exceptions + static_assert(!wait_deadlock_checks_enabled, "Wait deadlock checks cannot be enabled if exception handling is disabled."); +#endif + // ============================ // Constructors and destructors // ============================ @@ -302,58 +1259,184 @@ public: * * @param num_threads The number of threads to use. */ - explicit thread_pool(const concurrency_t num_threads) : thread_pool(num_threads, [] {}) {} + explicit thread_pool(const std::size_t num_threads) : thread_pool(num_threads, [] {}) {} /** * @brief Construct a new thread pool with the specified initialization function. * - * @param init_task An initialization function to run in each thread before it starts to execute any submitted tasks. The function must take no arguments and have no return value. It will only be executed exactly once, when the thread is first constructed. + * @param init An initialization function to run in each thread before it starts executing any submitted tasks. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. It will be executed exactly once per thread, when the thread is first constructed. The initialization function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. */ - explicit thread_pool(const std::function& init_task) : thread_pool(0, init_task) {} + template + explicit thread_pool(F&& init) : thread_pool(0, std::forward(init)) + { + } /** * @brief Construct a new thread pool with the specified number of threads and initialization function. * * @param num_threads The number of threads to use. - * @param init_task An initialization function to run in each thread before it starts to execute any submitted tasks. The function must take no arguments and have no return value. It will only be executed exactly once, when the thread is first constructed. + * @param init An initialization function to run in each thread before it starts executing any submitted tasks. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. It will be executed exactly once per thread, when the thread is first constructed. The initialization function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. */ - thread_pool(const concurrency_t num_threads, const std::function& init_task) : thread_count(determine_thread_count(num_threads)), threads(std::make_unique(determine_thread_count(num_threads))) + template + thread_pool(const std::size_t num_threads, F&& init) { - create_threads(init_task); + create_threads(num_threads, std::forward(init)); } - // The copy and move constructors and assignment operators are deleted. The thread pool uses a mutex, which cannot be copied or moved. + // The copy and move constructors and assignment operators are deleted. The thread pool cannot be copied or moved. thread_pool(const thread_pool&) = delete; thread_pool(thread_pool&&) = delete; thread_pool& operator=(const thread_pool&) = delete; thread_pool& operator=(thread_pool&&) = delete; /** - * @brief Destruct the thread pool. Waits for all tasks to complete, then destroys all threads. Note that if the pool is paused, then any tasks still in the queue will never be executed. + * @brief Destruct the thread pool. Waits for all tasks to complete, then destroys all threads. If a cleanup function was set, it will run in each thread right before it is destroyed. Note that if the pool is paused, then any tasks still in the queue will never be executed. */ - ~thread_pool() + ~thread_pool() noexcept { - wait(); - destroy_threads(); +#ifdef __cpp_exceptions + try + { +#endif + wait(); +#ifndef __cpp_lib_jthread + destroy_threads(); +#endif +#ifdef __cpp_exceptions + } + catch (...) + { + } +#endif } // ======================= // Public member functions // ======================= -#ifdef BS_THREAD_POOL_ENABLE_NATIVE_HANDLES /** - * @brief Get a vector containing the underlying implementation-defined thread handles for each of the pool's threads, as obtained by `std::thread::native_handle()`. Only enabled if `BS_THREAD_POOL_ENABLE_NATIVE_HANDLES` is defined. + * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The block function takes two arguments, the start and end of the block, so that it is only called once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Does not return a `BS::multi_future`, so the user must use `wait()` or some other method to ensure that the loop finishes executing, otherwise bad things will happen. + * + * @tparam T1 The type of the first index. Should be a signed or unsigned integer. + * @tparam T2 The type of the index after the last index. Should be a signed or unsigned integer. + * @tparam F The type of the function to loop through. + * @param first_index The first index in the loop. + * @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 `index_after_last <= first_index`, no blocks will be submitted. + * @param block A function that 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. `block(start, end)` should typically involve a loop of the form `for (T i = start; i < end; ++i)`. + * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. + * @param priority The priority of the tasks. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + */ + template , typename F> + void detach_blocks(const T1 first_index, const T2 index_after_last, F&& block, const std::size_t num_blocks = 0, const priority_t priority = 0) + { + if (static_cast(index_after_last) > static_cast(first_index)) + { + const std::shared_ptr> block_ptr = std::make_shared>(std::forward(block)); + const blocks blks(static_cast(first_index), static_cast(index_after_last), num_blocks ? num_blocks : thread_count); + for (std::size_t blk = 0; blk < blks.get_num_blocks(); ++blk) + { + detach_task( + [block_ptr, start = blks.start(blk), end = blks.end(blk)] + { + (*block_ptr)(start, end); + }, + priority); + } + } + } + + /** + * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The loop function takes one argument, the loop index, so that it is called many times per block. Does not return a `BS::multi_future`, so the user must use `wait()` or some other method to ensure that the loop finishes executing, otherwise bad things will happen. + * + * @tparam T1 The type of the first index. Should be a signed or unsigned integer. + * @tparam T2 The type of the index after the last index. Should be a signed or unsigned integer. + * @tparam F The type of the function to loop through. + * @param first_index The first index in the loop. + * @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 `index_after_last <= first_index`, no blocks will be submitted. + * @param loop The function to loop through. Will be called once per index, many times per block. Should take exactly one argument: the loop index. + * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. + * @param priority The priority of the tasks. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + */ + template , typename F> + void detach_loop(const T1 first_index, const T2 index_after_last, F&& loop, const std::size_t num_blocks = 0, const priority_t priority = 0) + { + if (static_cast(index_after_last) > static_cast(first_index)) + { + const std::shared_ptr> loop_ptr = std::make_shared>(std::forward(loop)); + const blocks blks(static_cast(first_index), static_cast(index_after_last), num_blocks ? num_blocks : thread_count); + for (std::size_t blk = 0; blk < blks.get_num_blocks(); ++blk) + { + detach_task( + [loop_ptr, start = blks.start(blk), end = blks.end(blk)] + { + for (T i = start; i < end; ++i) + (*loop_ptr)(i); + }, + priority); + } + } + } + + /** + * @brief Submit a sequence of tasks enumerated by indices to the queue, with the specified priority. The sequence function takes one argument, the task index, and will be called once per index. Does not return a `BS::multi_future`, so the user must use `wait()` or some other method to ensure that the sequence finishes executing, otherwise bad things will happen. + * + * @tparam T1 The type of the first index. Should be a signed or unsigned integer. + * @tparam T2 The type of the index after the last index. Should be a signed or unsigned integer. + * @tparam F The type of the function used to define the sequence. + * @param first_index The first index in the sequence. + * @param index_after_last The index after the last index in the sequence. The sequence 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 `index_after_last <= first_index`, no tasks will be submitted. + * @param sequence The function used to define the sequence. Will be called once per index. Should take exactly one argument, the index. + * @param priority The priority of the tasks. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + */ + template , typename F> + void detach_sequence(const T1 first_index, const T2 index_after_last, F&& sequence, const priority_t priority = 0) + { + if (static_cast(index_after_last) > static_cast(first_index)) + { + const std::shared_ptr> sequence_ptr = std::make_shared>(std::forward(sequence)); + for (T i = static_cast(first_index); i < static_cast(index_after_last); ++i) + { + detach_task( + [sequence_ptr, i] + { + (*sequence_ptr)(i); + }, + priority); + } + } + } + + /** + * @brief Submit a function with no arguments and no return value into the task queue, with the specified priority. To submit a function with arguments, enclose it in a lambda expression. Does not return a future, so the user must use `wait()` or some other method to ensure that the task finishes executing, otherwise bad things will happen. + * + * @tparam F The type of the function. + * @param task The function to submit. + * @param priority The priority of the task. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + */ + template + void detach_task(F&& task, const priority_t priority = 0) + { + { + const std::scoped_lock tasks_lock(tasks_mutex); + if constexpr (priority_enabled) + tasks.emplace(std::forward(task), priority); + else + tasks.emplace(std::forward(task)); + } + task_available_cv.notify_one(); + } + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS + /** + * @brief Get a vector containing the underlying implementation-defined thread handles for each of the pool's threads, as obtained by `std::thread::native_handle()` (or `std::jthread::native_handle()` in C++20 and later). * * @return The native thread handles. */ - [[nodiscard]] std::vector get_native_handles() const + [[nodiscard]] std::vector get_native_handles() const { - std::vector native_handles(thread_count); - for (concurrency_t i = 0; i < thread_count; ++i) - { + std::vector native_handles(thread_count); + for (std::size_t i = 0; i < thread_count; ++i) native_handles[i] = threads[i].native_handle(); - } return native_handles; } #endif @@ -363,7 +1446,7 @@ public: * * @return The number of queued tasks. */ - [[nodiscard]] size_t get_tasks_queued() const + [[nodiscard]] std::size_t get_tasks_queued() const { const std::scoped_lock tasks_lock(tasks_mutex); return tasks.size(); @@ -374,7 +1457,7 @@ public: * * @return The number of running tasks. */ - [[nodiscard]] size_t get_tasks_running() const + [[nodiscard]] std::size_t get_tasks_running() const { const std::scoped_lock tasks_lock(tasks_mutex); return tasks_running; @@ -385,7 +1468,7 @@ public: * * @return The total number of tasks. */ - [[nodiscard]] size_t get_tasks_total() const + [[nodiscard]] std::size_t get_tasks_total() const { const std::scoped_lock tasks_lock(tasks_mutex); return tasks_running + tasks.size(); @@ -396,32 +1479,30 @@ public: * * @return The number of threads. */ - [[nodiscard]] concurrency_t get_thread_count() const + [[nodiscard]] std::size_t get_thread_count() const noexcept { return thread_count; } /** - * @brief Get a vector containing the unique identifiers for each of the pool's threads, as obtained by `std::thread::get_id()`. + * @brief Get a vector containing the unique identifiers for each of the pool's threads, as obtained by `std::thread::get_id()` (or `std::jthread::get_id()` in C++20 and later). * * @return The unique thread identifiers. */ - [[nodiscard]] std::vector get_thread_ids() const + [[nodiscard]] std::vector get_thread_ids() const { - std::vector thread_ids(thread_count); - for (concurrency_t i = 0; i < thread_count; ++i) - { + std::vector thread_ids(thread_count); + for (std::size_t i = 0; i < thread_count; ++i) thread_ids[i] = threads[i].get_id(); - } return thread_ids; } -#ifdef BS_THREAD_POOL_ENABLE_PAUSE /** - * @brief Check whether the pool is currently paused. Only enabled if `BS_THREAD_POOL_ENABLE_PAUSE` is defined. + * @brief Check whether the pool is currently paused. Only enabled if the flag `BS:tp::pause` is enabled in the template parameter. * * @return `true` if the pool is paused, `false` if it is not paused. */ + BS_THREAD_POOL_IF_PAUSE_ENABLED [[nodiscard]] bool is_paused() const { const std::scoped_lock tasks_lock(tasks_mutex); @@ -429,14 +1510,14 @@ public: } /** - * @brief Pause the pool. The workers will temporarily stop retrieving new tasks out of the queue, although any tasks already executed will keep running until they are finished. Only enabled if `BS_THREAD_POOL_ENABLE_PAUSE` is defined. + * @brief Pause the pool. The workers will temporarily stop retrieving new tasks out of the queue, although any tasks already executed will keep running until they are finished. Only enabled if the flag `BS:tp::pause` is enabled in the template parameter. */ + BS_THREAD_POOL_IF_PAUSE_ENABLED void pause() { const std::scoped_lock tasks_lock(tasks_mutex); paused = true; } -#endif /** * @brief Purge all the tasks waiting in the queue. Tasks that are currently running will not be affected, but any tasks still waiting in the queue will be discarded, and will never be executed by the threads. Please note that there is no way to restore the purged tasks. @@ -444,99 +1525,7 @@ public: void purge() { const std::scoped_lock tasks_lock(tasks_mutex); - while (!tasks.empty()) - tasks.pop(); - } - - /** - * @brief Submit a function with no arguments and no return value into the task queue, with the specified priority. To push a function with arguments, enclose it in a lambda expression. Does not return a future, so the user must use `wait()` or some other method to ensure that the task finishes executing, otherwise bad things will happen. - * - * @tparam F The type of the function. - * @param task The function to push. - * @param priority The priority of the task. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - */ - template - void detach_task(F&& task BS_THREAD_POOL_PRIORITY_INPUT) - { - { - const std::scoped_lock tasks_lock(tasks_mutex); - tasks.emplace(std::forward(task) BS_THREAD_POOL_PRIORITY_OUTPUT); - } - task_available_cv.notify_one(); - } - - /** - * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The block function takes two arguments, the start and end of the block, so that it is only called only once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Does not return a `multi_future`, so the user must use `wait()` or some other method to ensure that the loop finishes executing, otherwise bad things will happen. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - * @tparam F The type of the function to loop through. - * @param first_index The first index in the loop. - * @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 `index_after_last <= first_index`, no blocks will be submitted. - * @param block A function that 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. `block(start, end)` should typically involve a loop of the form `for (T i = start; i < end; ++i)`. - * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. - * @param priority The priority of the tasks. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - */ - template - void detach_blocks(const T first_index, const T index_after_last, F&& block, const size_t num_blocks = 0 BS_THREAD_POOL_PRIORITY_INPUT) - { - if (index_after_last > first_index) - { - const blocks blks(first_index, index_after_last, num_blocks ? num_blocks : thread_count); - for (size_t blk = 0; blk < blks.get_num_blocks(); ++blk) - detach_task( - [block = std::forward(block), start = blks.start(blk), end = blks.end(blk)] - { - block(start, end); - } BS_THREAD_POOL_PRIORITY_OUTPUT); - } - } - - /** - * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The loop function takes one argument, the loop index, so that it is called many times per block. Does not return a `multi_future`, so the user must use `wait()` or some other method to ensure that the loop finishes executing, otherwise bad things will happen. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - * @tparam F The type of the function to loop through. - * @param first_index The first index in the loop. - * @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 `index_after_last <= first_index`, no blocks will be submitted. - * @param loop The function to loop through. Will be called once per index, many times per block. Should take exactly one argument: the loop index. - * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. - * @param priority The priority of the tasks. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - */ - template - void detach_loop(const T first_index, const T index_after_last, F&& loop, const size_t num_blocks = 0 BS_THREAD_POOL_PRIORITY_INPUT) - { - if (index_after_last > first_index) - { - const blocks blks(first_index, index_after_last, num_blocks ? num_blocks : thread_count); - for (size_t blk = 0; blk < blks.get_num_blocks(); ++blk) - detach_task( - [loop = std::forward(loop), start = blks.start(blk), end = blks.end(blk)] - { - for (T i = start; i < end; ++i) - loop(i); - } BS_THREAD_POOL_PRIORITY_OUTPUT); - } - } - - /** - * @brief Submit a sequence of tasks enumerated by indices to the queue, with the specified priority. Does not return a `multi_future`, so the user must use `wait()` or some other method to ensure that the sequence finishes executing, otherwise bad things will happen. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - * @tparam F The type of the function used to define the sequence. - * @param first_index The first index in the sequence. - * @param index_after_last The index after the last index in the sequence. The sequence 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 `index_after_last <= first_index`, no tasks will be submitted. - * @param sequence The function used to define the sequence. Will be called once per index. Should take exactly one argument, the index. - * @param priority The priority of the tasks. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - */ - template - void detach_sequence(const T first_index, const T index_after_last, F&& sequence BS_THREAD_POOL_PRIORITY_INPUT) - { - for (T i = first_index; i < index_after_last; ++i) - detach_task( - [sequence = std::forward(sequence), i] - { - sequence(i); - } BS_THREAD_POOL_PRIORITY_OUTPUT); + tasks = {}; } /** @@ -544,7 +1533,7 @@ public: */ void reset() { - reset(0, [] {}); + reset(0, [](std::size_t) {}); } /** @@ -552,44 +1541,175 @@ public: * * @param num_threads The number of threads to use. */ - void reset(const concurrency_t num_threads) + void reset(const std::size_t num_threads) { - reset(num_threads, [] {}); + reset(num_threads, [](std::size_t) {}); } /** * @brief Reset the pool with the total number of hardware threads available, as reported by the implementation, and a new initialization function. 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 and initialization function. 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 init_task An initialization function to run in each thread before it starts to execute any submitted tasks. The function must take no arguments and have no return value. It will only be executed exactly once, when the thread is first constructed. + * @param init An initialization function to run in each thread before it starts executing any submitted tasks. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. It will be executed exactly once per thread, when the thread is first constructed. The initialization function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. */ - void reset(const std::function& init_task) + template + void reset(F&& init) { - reset(0, init_task); + reset(0, std::forward(init)); } /** * @brief Reset the pool with a new number of threads and a new initialization function. 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 and initialization function. 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 num_threads The number of threads to use. - * @param init_task An initialization function to run in each thread before it starts to execute any submitted tasks. The function must take no arguments and have no return value. It will only be executed exactly once, when the thread is first constructed. + * @param init An initialization function to run in each thread before it starts executing any submitted tasks. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. It will be executed exactly once per thread, when the thread is first constructed. The initialization function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. */ - void reset(const concurrency_t num_threads, const std::function& init_task) + template + void reset(const std::size_t num_threads, F&& init) { -#ifdef BS_THREAD_POOL_ENABLE_PAUSE - std::unique_lock tasks_lock(tasks_mutex); - const bool was_paused = paused; - paused = true; - tasks_lock.unlock(); -#endif - wait(); - destroy_threads(); - thread_count = determine_thread_count(num_threads); - threads = std::make_unique(thread_count); - create_threads(init_task); -#ifdef BS_THREAD_POOL_ENABLE_PAUSE - tasks_lock.lock(); - paused = was_paused; -#endif + if constexpr (pause_enabled) + { + std::unique_lock tasks_lock(tasks_mutex); + const bool was_paused = paused; + paused = true; + tasks_lock.unlock(); + reset_pool(num_threads, std::forward(init)); + tasks_lock.lock(); + paused = was_paused; + } + else + { + reset_pool(num_threads, std::forward(init)); + } + } + + /** + * @brief Set the thread pool's cleanup function. + * + * @param cleanup A cleanup function to run in each thread right before it is destroyed, which will happen when the pool is destructed or reset. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. The cleanup function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. + */ + template + void set_cleanup_func(F&& cleanup) + { + if constexpr (std::is_invocable_v) + { + cleanup_func = std::forward(cleanup); + } + else + { + cleanup_func = [cleanup = std::forward(cleanup)](std::size_t) + { + cleanup(); + }; + } + } + + /** + * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The block function takes two arguments, the start and end of the block, so that it is only called once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Returns a `BS::multi_future` that contains the futures for all of the blocks. + * + * @tparam T1 The type of the first index. Should be a signed or unsigned integer. + * @tparam T2 The type of the index after the last index. Should be a signed or unsigned integer. + * @tparam F The type of the function to loop through. + * @tparam R The return type of the function to loop through (can be `void`). + * @param first_index The first index in the loop. + * @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 `index_after_last <= first_index`, no blocks will be submitted, and an empty `BS::multi_future` will be returned. + * @param block A function that 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. `block(start, end)` should typically involve a loop of the form `for (T i = start; i < end; ++i)`. + * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. + * @param priority The priority of the tasks. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + * @return A `BS::multi_future` that can be used to wait for all the blocks to finish. If the block function returns a value, the `BS::multi_future` can also be used to obtain the values returned by each block. + */ + template , typename F, typename R = std::invoke_result_t, T, T>> + [[nodiscard]] multi_future submit_blocks(const T1 first_index, const T2 index_after_last, F&& block, const std::size_t num_blocks = 0, const priority_t priority = 0) + { + if (static_cast(index_after_last) > static_cast(first_index)) + { + const std::shared_ptr> block_ptr = std::make_shared>(std::forward(block)); + const blocks blks(static_cast(first_index), static_cast(index_after_last), num_blocks ? num_blocks : thread_count); + multi_future future; + future.reserve(blks.get_num_blocks()); + for (std::size_t blk = 0; blk < blks.get_num_blocks(); ++blk) + { + future.push_back(submit_task( + [block_ptr, start = blks.start(blk), end = blks.end(blk)] + { + return (*block_ptr)(start, end); + }, + priority)); + } + return future; + } + return {}; + } + + /** + * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The loop function takes one argument, the loop index, so that it is called many times per block. It must have no return value. Returns a `BS::multi_future` that contains the futures for all of the blocks. + * + * @tparam T1 The type of the first index. Should be a signed or unsigned integer. + * @tparam T2 The type of the index after the last index. Should be a signed or unsigned integer. + * @tparam F The type of the function to loop through. + * @param first_index The first index in the loop. + * @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 `index_after_last <= first_index`, no tasks will be submitted, and an empty `BS::multi_future` will be returned. + * @param loop The function to loop through. Will be called once per index, many times per block. Should take exactly one argument: the loop index. It cannot have a return value. + * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. + * @param priority The priority of the tasks. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + * @return A `BS::multi_future` that can be used to wait for all the blocks to finish. + */ + template , typename F> + [[nodiscard]] multi_future submit_loop(const T1 first_index, const T2 index_after_last, F&& loop, const std::size_t num_blocks = 0, const priority_t priority = 0) + { + if (static_cast(index_after_last) > static_cast(first_index)) + { + const std::shared_ptr> loop_ptr = std::make_shared>(std::forward(loop)); + const blocks blks(static_cast(first_index), static_cast(index_after_last), num_blocks ? num_blocks : thread_count); + multi_future future; + future.reserve(blks.get_num_blocks()); + for (std::size_t blk = 0; blk < blks.get_num_blocks(); ++blk) + { + future.push_back(submit_task( + [loop_ptr, start = blks.start(blk), end = blks.end(blk)] + { + for (T i = start; i < end; ++i) + (*loop_ptr)(i); + }, + priority)); + } + return future; + } + return {}; + } + + /** + * @brief Submit a sequence of tasks enumerated by indices to the queue, with the specified priority. The sequence function takes one argument, the task index, and will be called once per index. Returns a `BS::multi_future` that contains the futures for all of the tasks. + * + * @tparam T1 The type of the first index. Should be a signed or unsigned integer. + * @tparam T2 The type of the index after the last index. Should be a signed or unsigned integer. + * @tparam F The type of the function used to define the sequence. + * @tparam R The return type of the function used to define the sequence (can be `void`). + * @param first_index The first index in the sequence. + * @param index_after_last The index after the last index in the sequence. The sequence 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 `index_after_last <= first_index`, no tasks will be submitted, and an empty `BS::multi_future` will be returned. + * @param sequence The function used to define the sequence. Will be called once per index. Should take exactly one argument, the index. + * @param priority The priority of the tasks. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. + * @return A `BS::multi_future` that can be used to wait for all the tasks to finish. If the sequence function returns a value, the `BS::multi_future` can also be used to obtain the values returned by each task. + */ + template , typename F, typename R = std::invoke_result_t, T>> + [[nodiscard]] multi_future submit_sequence(const T1 first_index, const T2 index_after_last, F&& sequence, const priority_t priority = 0) + { + if (static_cast(index_after_last) > static_cast(first_index)) + { + const std::shared_ptr> sequence_ptr = std::make_shared>(std::forward(sequence)); + multi_future future; + future.reserve(static_cast(static_cast(index_after_last) > static_cast(first_index))); + for (T i = static_cast(first_index); i < static_cast(index_after_last); ++i) + { + future.push_back(submit_task( + [sequence_ptr, i] + { + return (*sequence_ptr)(i); + }, + priority)); + } + return future; + } + return {}; } /** @@ -598,144 +1718,58 @@ public: * @tparam F The type of the function. * @tparam R The return type of the function (can be `void`). * @param task The function to submit. - * @param priority The priority of the task. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. + * @param priority The priority of the task. Should be between -128 and +127 (a signed 8-bit integer). The default is 0. Only taken into account if the flag `BS:tp::priority` is enabled in the template parameter, otherwise has no effect. * @return A future to be used later to wait for the function to finish executing and/or obtain its returned value if it has one. */ template >> - [[nodiscard]] std::future submit_task(F&& task BS_THREAD_POOL_PRIORITY_INPUT) + [[nodiscard]] std::future submit_task(F&& task, const priority_t priority = 0) { - const std::shared_ptr> task_promise = std::make_shared>(); +#ifdef __cpp_lib_move_only_function + std::promise promise; + #define BS_THREAD_POOL_PROMISE_MEMBER_ACCESS promise. +#else + const std::shared_ptr> promise = std::make_shared>(); + #define BS_THREAD_POOL_PROMISE_MEMBER_ACCESS promise-> +#endif + std::future future = BS_THREAD_POOL_PROMISE_MEMBER_ACCESS get_future(); detach_task( - [task = std::forward(task), task_promise] + [task = std::forward(task), promise = std::move(promise)]() mutable { -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING +#ifdef __cpp_exceptions try { #endif if constexpr (std::is_void_v) { task(); - task_promise->set_value(); + BS_THREAD_POOL_PROMISE_MEMBER_ACCESS set_value(); } else { - task_promise->set_value(task()); + BS_THREAD_POOL_PROMISE_MEMBER_ACCESS set_value(task()); } -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING +#ifdef __cpp_exceptions } catch (...) { try { - task_promise->set_exception(std::current_exception()); + BS_THREAD_POOL_PROMISE_MEMBER_ACCESS set_exception(std::current_exception()); } catch (...) { } } #endif - } BS_THREAD_POOL_PRIORITY_OUTPUT); - return task_promise->get_future(); + }, + priority); + return future; } /** - * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The block function takes two arguments, the start and end of the block, so that it is only called only once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Returns a `multi_future` that contains the futures for all of the blocks. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - * @tparam F The type of the function to loop through. - * @tparam R The return type of the function to loop through (can be `void`). - * @param first_index The first index in the loop. - * @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 `index_after_last <= first_index`, no blocks will be submitted, and an empty `multi_future` will be returned. - * @param block A function that 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. `block(start, end)` should typically involve a loop of the form `for (T i = start; i < end; ++i)`. - * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. - * @param priority The priority of the tasks. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - * @return A `multi_future` that can be used to wait for all the blocks to finish. If the block function returns a value, the `multi_future` can also be used to obtain the values returned by each block. - */ - template , T, T>> - [[nodiscard]] multi_future submit_blocks(const T first_index, const T index_after_last, F&& block, const size_t num_blocks = 0 BS_THREAD_POOL_PRIORITY_INPUT) - { - if (index_after_last > first_index) - { - const blocks blks(first_index, index_after_last, num_blocks ? num_blocks : thread_count); - multi_future future; - future.reserve(blks.get_num_blocks()); - for (size_t blk = 0; blk < blks.get_num_blocks(); ++blk) - future.push_back(submit_task( - [block = std::forward(block), start = blks.start(blk), end = blks.end(blk)] - { - return block(start, end); - } BS_THREAD_POOL_PRIORITY_OUTPUT)); - return future; - } - return {}; - } - - /** - * @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue, with the specified priority. The loop function takes one argument, the loop index, so that it is called many times per block. It must have no return value. Returns a `multi_future` that contains the futures for all of the blocks. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - * @tparam F The type of the function to loop through. - * @param first_index The first index in the loop. - * @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 `index_after_last <= first_index`, no tasks will be submitted, and an empty `multi_future` will be returned. - * @param loop The function to loop through. Will be called once per index, many times per block. Should take exactly one argument: the loop index. It cannot have a return value. - * @param num_blocks The maximum number of blocks to split the loop into. The default is 0, which means the number of blocks will be equal to the number of threads in the pool. - * @param priority The priority of the tasks. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - * @return A `multi_future` that can be used to wait for all the blocks to finish. - */ - template - [[nodiscard]] multi_future submit_loop(const T first_index, const T index_after_last, F&& loop, const size_t num_blocks = 0 BS_THREAD_POOL_PRIORITY_INPUT) - { - if (index_after_last > first_index) - { - const blocks blks(first_index, index_after_last, num_blocks ? num_blocks : thread_count); - multi_future future; - future.reserve(blks.get_num_blocks()); - for (size_t blk = 0; blk < blks.get_num_blocks(); ++blk) - future.push_back(submit_task( - [loop = std::forward(loop), start = blks.start(blk), end = blks.end(blk)] - { - for (T i = start; i < end; ++i) - loop(i); - } BS_THREAD_POOL_PRIORITY_OUTPUT)); - return future; - } - return {}; - } - - /** - * @brief Submit a sequence of tasks enumerated by indices to the queue, with the specified priority. Returns a `multi_future` that contains the futures for all of the tasks. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - * @tparam F The type of the function used to define the sequence. - * @tparam R The return type of the function used to define the sequence (can be `void`). - * @param first_index The first index in the sequence. - * @param index_after_last The index after the last index in the sequence. The sequence 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 `index_after_last <= first_index`, no tasks will be submitted, and an empty `multi_future` will be returned. - * @param sequence The function used to define the sequence. Will be called once per index. Should take exactly one argument, the index. - * @param priority The priority of the tasks. Should be between -32,768 and 32,767 (a signed 16-bit integer). The default is 0. Only enabled if `BS_THREAD_POOL_ENABLE_PRIORITY` is defined. - * @return A `multi_future` that can be used to wait for all the tasks to finish. If the sequence function returns a value, the `multi_future` can also be used to obtain the values returned by each task. - */ - template , T>> - [[nodiscard]] multi_future submit_sequence(const T first_index, const T index_after_last, F&& sequence BS_THREAD_POOL_PRIORITY_INPUT) - { - if (index_after_last > first_index) - { - multi_future future; - future.reserve(static_cast(index_after_last - first_index)); - for (T i = first_index; i < index_after_last; ++i) - future.push_back(submit_task( - [sequence = std::forward(sequence), i] - { - return sequence(i); - } BS_THREAD_POOL_PRIORITY_OUTPUT)); - return future; - } - return {}; - } - -#ifdef BS_THREAD_POOL_ENABLE_PAUSE - /** - * @brief Unpause the pool. The workers will resume retrieving new tasks out of the queue. Only enabled if `BS_THREAD_POOL_ENABLE_PAUSE` is defined. + * @brief Unpause the pool. The workers will resume retrieving new tasks out of the queue. Only enabled if the flag `BS:tp::pause` is enabled in the template parameter. */ + BS_THREAD_POOL_IF_PAUSE_ENABLED void unpause() { { @@ -744,32 +1778,30 @@ public: } task_available_cv.notify_all(); } -#endif - -// Macros used internally to enable or disable pausing in the waiting and worker functions. -#ifdef BS_THREAD_POOL_ENABLE_PAUSE - #define BS_THREAD_POOL_PAUSED_OR_EMPTY (paused || tasks.empty()) -#else - #define BS_THREAD_POOL_PAUSED_OR_EMPTY tasks.empty() -#endif /** * @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 pool is paused, this function only waits for the currently running tasks (otherwise it would wait forever). Note: To wait for just one specific task, use `submit_task()` instead, and call the `wait()` member function of the generated future. * - * @throws `wait_deadlock` if called from within a thread of the same pool, which would result in a deadlock. Only enabled if `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK` is defined. + * @throws `wait_deadlock` if called from within a thread of the same pool, which would result in a deadlock. Only enabled if the flag `BS:tp::wait_deadlock_checks` is enabled in the template parameter. */ void wait() { -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - if (this_thread::get_pool() == this) - throw wait_deadlock(); +#ifdef __cpp_exceptions + if constexpr (wait_deadlock_checks_enabled) + { + if (this_thread::get_pool() == this) + throw wait_deadlock(); + } #endif std::unique_lock tasks_lock(tasks_mutex); waiting = true; tasks_done_cv.wait(tasks_lock, [this] { - return (tasks_running == 0) && BS_THREAD_POOL_PAUSED_OR_EMPTY; + if constexpr (pause_enabled) + return (tasks_running == 0) && (paused || tasks.empty()); + else + return (tasks_running == 0) && tasks.empty(); }); waiting = false; } @@ -781,22 +1813,27 @@ public: * @tparam P An `std::ratio` representing the length of each tick in seconds. * @param duration The amount of time to wait. * @return `true` if all tasks finished running, `false` if the duration expired but some tasks are still running. - * - * @throws `wait_deadlock` if called from within a thread of the same pool, which would result in a deadlock. Only enabled if `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK` is defined. + * @throws `wait_deadlock` if called from within a thread of the same pool, which would result in a deadlock. Only enabled if the flag `BS:tp::wait_deadlock_checks` is enabled in the template parameter. */ template bool wait_for(const std::chrono::duration& duration) { -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - if (this_thread::get_pool() == this) - throw wait_deadlock(); +#ifdef __cpp_exceptions + if constexpr (wait_deadlock_checks_enabled) + { + if (this_thread::get_pool() == this) + throw wait_deadlock(); + } #endif std::unique_lock tasks_lock(tasks_mutex); waiting = true; const bool status = tasks_done_cv.wait_for(tasks_lock, duration, [this] { - return (tasks_running == 0) && BS_THREAD_POOL_PAUSED_OR_EMPTY; + if constexpr (pause_enabled) + return (tasks_running == 0) && (paused || tasks.empty()); + else + return (tasks_running == 0) && tasks.empty(); }); waiting = false; return status; @@ -809,41 +1846,32 @@ public: * @tparam D An `std::chrono::duration` type used to indicate the time point. * @param timeout_time The time point at which to stop waiting. * @return `true` if all tasks finished running, `false` if the time point was reached but some tasks are still running. - * - * @throws `wait_deadlock` if called from within a thread of the same pool, which would result in a deadlock. Only enabled if `BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK` is defined. + * @throws `wait_deadlock` if called from within a thread of the same pool, which would result in a deadlock. Only enabled if the flag `BS:tp::wait_deadlock_checks` is enabled in the template parameter. */ template bool wait_until(const std::chrono::time_point& timeout_time) { -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - if (this_thread::get_pool() == this) - throw wait_deadlock(); +#ifdef __cpp_exceptions + if constexpr (wait_deadlock_checks_enabled) + { + if (this_thread::get_pool() == this) + throw wait_deadlock(); + } #endif std::unique_lock tasks_lock(tasks_mutex); waiting = true; const bool status = tasks_done_cv.wait_until(tasks_lock, timeout_time, [this] { - return (tasks_running == 0) && BS_THREAD_POOL_PAUSED_OR_EMPTY; + if constexpr (pause_enabled) + return (tasks_running == 0) && (paused || tasks.empty()); + else + return (tasks_running == 0) && tasks.empty(); }); waiting = false; return status; } -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - // ============== - // Public classes - // ============== - - /** - * @brief An exception that will be thrown by `wait()`, `wait_for()`, and `wait_until()` if the user tries to call them from within a thread of the same pool, which would result in a deadlock. - */ - struct wait_deadlock : public std::runtime_error - { - wait_deadlock() : std::runtime_error("BS::thread_pool::wait_deadlock"){}; - }; -#endif - private: // ======================== // Private member functions @@ -852,21 +1880,51 @@ private: /** * @brief Create the threads in the pool and assign a worker to each thread. * - * @param init_task An initialization function to run in each thread before it starts to execute any submitted tasks. + * @param num_threads The number of threads to use. + * @param init An initialization function to run in each thread before it starts executing any submitted tasks. */ - void create_threads(const std::function& init_task) + template + void create_threads(const std::size_t num_threads, F&& init) { + if constexpr (std::is_invocable_v) + { + init_func = std::forward(init); + } + else + { + init_func = [init = std::forward(init)](std::size_t) + { + init(); + }; + } + thread_count = determine_thread_count(num_threads); + threads = std::make_unique(thread_count); { const std::scoped_lock tasks_lock(tasks_mutex); tasks_running = thread_count; +#ifndef __cpp_lib_jthread workers_running = true; +#endif } - for (concurrency_t i = 0; i < thread_count; ++i) + for (std::size_t i = 0; i < thread_count; ++i) { - threads[i] = std::thread(&thread_pool::worker, this, i, init_task); + threads[i] = thread_t( + [this, i] +#ifdef __cpp_lib_jthread + (const std::stop_token& stop_token) + { + worker(stop_token, i); + } +#else + { + worker(i); + } +#endif + ); } } +#ifndef __cpp_lib_jthread /** * @brief Destroy the threads in the pool. */ @@ -877,280 +1935,439 @@ private: workers_running = false; } task_available_cv.notify_all(); - for (concurrency_t i = 0; i < thread_count; ++i) - { + for (std::size_t i = 0; i < thread_count; ++i) threads[i].join(); - } } +#endif /** * @brief Determine how many threads the pool should have, based on the parameter passed to the constructor or reset(). * - * @param num_threads The parameter passed to the constructor or `reset()`. If the parameter is a positive number, then the pool will be created with this number of threads. If the parameter is non-positive, or a parameter was not supplied (in which case it will have the default value of 0), then the pool will be created with the total number of hardware threads available, as obtained from `std::thread::hardware_concurrency()`. If the latter returns zero for some reason, then the pool will be created with just one thread. + * @param num_threads The parameter passed to the constructor or `reset()`. If the parameter is a positive number, then the pool will be created with this number of threads. If the parameter is non-positive, or a parameter was not supplied (in which case it will have the default value of 0), then the pool will be created with the total number of hardware threads available, as obtained from `thread_t::hardware_concurrency()`. If the latter returns zero for some reason, then the pool will be created with just one thread. * @return The number of threads to use for constructing the pool. */ - [[nodiscard]] static concurrency_t determine_thread_count(const concurrency_t num_threads) + [[nodiscard]] static std::size_t determine_thread_count(const std::size_t num_threads) noexcept { if (num_threads > 0) return num_threads; - if (std::thread::hardware_concurrency() > 0) - return std::thread::hardware_concurrency(); + if (thread_t::hardware_concurrency() > 0) + return thread_t::hardware_concurrency(); return 1; } + /** + * @brief Pop a task from the queue. + * + * @return The task. + */ + [[nodiscard]] task_t pop_task() + { + task_t task; + if constexpr (priority_enabled) + task = std::move(const_cast(tasks.top()).task); + else + task = std::move(tasks.front()); + tasks.pop(); + return task; + } + + /** + * @brief Reset the pool with a new number of threads and a new initialization function. This member function implements the actual reset, while the public member function `reset()` also handles the case where the pool is paused. + * + * @param num_threads The number of threads to use. + * @param init An initialization function to run in each thread before it starts executing any submitted tasks. + */ + template + void reset_pool(const std::size_t num_threads, F&& init) + { + wait(); +#ifndef __cpp_lib_jthread + destroy_threads(); +#endif + create_threads(num_threads, std::forward(init)); + } + /** * @brief A worker function to be assigned to each thread in the pool. Waits until it is notified by `detach_task()` that a task is available, and then retrieves the task from the queue and executes it. Once the task finishes, the worker notifies `wait()` in case it is waiting. * * @param idx The index of this thread. - * @param init_task An initialization function to run in this thread before it starts to execute any submitted tasks. */ - void worker(const concurrency_t idx, const std::function& init_task) + void worker(BS_THREAD_POOL_WORKER_TOKEN const std::size_t idx) { - this_thread::get_index.index = idx; - this_thread::get_pool.pool = this; - init_task(); - std::unique_lock tasks_lock(tasks_mutex); + this_thread::my_pool = this; + this_thread::my_index = idx; + init_func(idx); while (true) { + std::unique_lock tasks_lock(tasks_mutex); --tasks_running; - tasks_lock.unlock(); - if (waiting && (tasks_running == 0) && BS_THREAD_POOL_PAUSED_OR_EMPTY) - tasks_done_cv.notify_all(); - tasks_lock.lock(); - task_available_cv.wait(tasks_lock, - [this] - { - return !BS_THREAD_POOL_PAUSED_OR_EMPTY || !workers_running; - }); - if (!workers_running) - break; + if constexpr (pause_enabled) { -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - const std::function task = std::move(std::remove_const_t(tasks.top()).task); - tasks.pop(); -#else - const std::function task = std::move(tasks.front()); - tasks.pop(); -#endif - ++tasks_running; - tasks_lock.unlock(); - task(); - } - tasks_lock.lock(); - } - this_thread::get_index.index = std::nullopt; - this_thread::get_pool.pool = std::nullopt; - } - - // =============== - // Private classes - // =============== - - /** - * @brief A helper class to divide a range into blocks. Used by `detach_blocks()`, `submit_blocks()`, `detach_loop()`, and `submit_loop()`. - * - * @tparam T The type of the indices. Should be a signed or unsigned integer. - */ - template - class [[nodiscard]] blocks - { - public: - /** - * @brief Construct a `blocks` object with the given specifications. - * - * @param first_index_ The first index in the range. - * @param index_after_last_ The index after the last index in the range. - * @param num_blocks_ The desired number of blocks to divide the range into. - */ - blocks(const T first_index_, const T index_after_last_, const size_t num_blocks_) : first_index(first_index_), index_after_last(index_after_last_), num_blocks(num_blocks_) - { - if (index_after_last > first_index) - { - const size_t total_size = static_cast(index_after_last - first_index); - if (num_blocks > total_size) - num_blocks = total_size; - block_size = total_size / num_blocks; - remainder = total_size % num_blocks; - if (block_size == 0) - { - block_size = 1; - num_blocks = (total_size > 1) ? total_size : 1; - } + if (waiting && (tasks_running == 0) && (paused || tasks.empty())) + tasks_done_cv.notify_all(); } else { - num_blocks = 0; + if (waiting && (tasks_running == 0) && tasks.empty()) + tasks_done_cv.notify_all(); + } + task_available_cv.wait(tasks_lock BS_THREAD_POOL_WAIT_TOKEN, + [this] + { + if constexpr (pause_enabled) + return !(paused || tasks.empty()) BS_THREAD_POOL_OR_STOP_CONDITION; + else + return !tasks.empty() BS_THREAD_POOL_OR_STOP_CONDITION; + }); + if (BS_THREAD_POOL_STOP_CONDITION) + break; + { + task_t task = pop_task(); // NOLINT(misc-const-correctness) In C++23 this cannot be const since `std::move_only_function::operator()` is not a const member function. + ++tasks_running; + tasks_lock.unlock(); +#ifdef __cpp_exceptions + try + { +#endif + task(); +#ifdef __cpp_exceptions + } + catch (...) + { + } +#endif } } - - /** - * @brief Get the first index of a block. - * - * @param block The block number. - * @return The first index. - */ - [[nodiscard]] T start(const size_t block) const - { - return first_index + static_cast(block * block_size) + static_cast(block < remainder ? block : remainder); - } - - /** - * @brief Get the index after the last index of a block. - * - * @param block The block number. - * @return The index after the last index. - */ - [[nodiscard]] T end(const size_t block) const - { - return (block == num_blocks - 1) ? index_after_last : start(block + 1); - } - - /** - * @brief Get the number of blocks. Note that this may be different than the desired number of blocks that was passed to the constructor. - * - * @return The number of blocks. - */ - [[nodiscard]] size_t get_num_blocks() const - { - return num_blocks; - } - - private: - /** - * @brief The size of each block (except possibly the last block). - */ - size_t block_size = 0; - - /** - * @brief The first index in the range. - */ - T first_index = 0; - - /** - * @brief The index after the last index in the range. - */ - T index_after_last = 0; - - /** - * @brief The number of blocks. - */ - size_t num_blocks = 0; - - /** - * @brief The remainder obtained after dividing the total size by the number of blocks. - */ - size_t remainder = 0; - }; // class blocks - -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - /** - * @brief A helper class to store a task with an assigned priority. - */ - class [[nodiscard]] pr_task - { - friend class thread_pool; - - public: - /** - * @brief Construct a new task with an assigned priority by copying the task. - * - * @param task_ The task. - * @param priority_ The desired priority. - */ - explicit pr_task(const std::function& task_, const priority_t priority_ = 0) : task(task_), priority(priority_) {} - - /** - * @brief Construct a new task with an assigned priority by moving the task. - * - * @param task_ The task. - * @param priority_ The desired priority. - */ - explicit pr_task(std::function&& task_, const priority_t priority_ = 0) : task(std::move(task_)), priority(priority_) {} - - /** - * @brief Compare the priority of two tasks. - * - * @param lhs The first task. - * @param rhs The second task. - * @return `true` if the first task has a lower priority than the second task, `false` otherwise. - */ - [[nodiscard]] friend bool operator<(const pr_task& lhs, const pr_task& rhs) - { - return lhs.priority < rhs.priority; - } - - private: - /** - * @brief The task. - */ - std::function task = {}; - - /** - * @brief The priority of the task. - */ - priority_t priority = 0; - }; // class pr_task -#endif + cleanup_func(idx); + this_thread::my_index = std::nullopt; + this_thread::my_pool = std::nullopt; + } // ============ // Private data // ============ -#ifdef BS_THREAD_POOL_ENABLE_PAUSE /** - * @brief A flag indicating whether the workers should pause. When set to `true`, the workers temporarily stop retrieving new tasks out of the queue, although any tasks already executed will keep running until they are finished. When set to `false` again, the workers resume retrieving tasks. + * @brief A cleanup function to run in each thread right before it is destroyed, which will happen when the pool is destructed or reset. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. The cleanup function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. The default is an empty function, i.e., no cleanup will be performed. */ - bool paused = false; -#endif + function_t cleanup_func = [](std::size_t) {}; /** - * @brief A condition variable to notify `worker()` that a new task has become available. + * @brief An initialization function to run in each thread before it starts executing any submitted tasks. The function must have no return value, and can either take one argument, the thread index of type `std::size_t`, or zero arguments. It will be executed exactly once per thread, when the thread is first constructed. The initialization function must not throw any exceptions, as that will result in program termination. Any exceptions must be handled explicitly within the function. The default is an empty function, i.e., no initialization will be performed. */ - std::condition_variable task_available_cv = {}; + function_t init_func = [](std::size_t) {}; + + /** + * @brief A flag indicating whether the workers should pause. When set to `true`, the workers temporarily stop retrieving new tasks out of the queue, although any tasks already executed will keep running until they are finished. When set to `false` again, the workers resume retrieving tasks. Only enabled if the flag `BS:tp::pause` is enabled in the template parameter. + */ + std::conditional_t paused = {}; + +/** + * @brief A condition variable to notify `worker()` that a new task has become available. + */ +#ifdef __cpp_lib_jthread + std::condition_variable_any +#else + std::condition_variable +#endif + task_available_cv; /** * @brief A condition variable to notify `wait()` that the tasks are done. */ - std::condition_variable tasks_done_cv = {}; + std::condition_variable tasks_done_cv; /** * @brief A queue of tasks to be executed by the threads. */ -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - std::priority_queue tasks = {}; -#else - std::queue> tasks = {}; -#endif - - /** - * @brief A counter for the total number of currently running tasks. - */ - size_t tasks_running = 0; + std::conditional_t, std::queue> tasks; /** * @brief A mutex to synchronize access to the task queue by different threads. */ - mutable std::mutex tasks_mutex = {}; + mutable std::mutex tasks_mutex; + + /** + * @brief A counter for the total number of currently running tasks. + */ + std::size_t tasks_running = 0; /** * @brief The number of threads in the pool. */ - concurrency_t thread_count = 0; + std::size_t thread_count = 0; /** * @brief A smart pointer to manage the memory allocated for the threads. */ - std::unique_ptr threads = nullptr; + std::unique_ptr threads = nullptr; /** * @brief A flag indicating that `wait()` is active and expects to be notified whenever a task is done. */ bool waiting = false; +#ifndef __cpp_lib_jthread /** * @brief A flag indicating to the workers to keep running. When set to `false`, the workers terminate permanently. */ bool workers_running = false; -}; // class thread_pool -} // namespace BS #endif +}; // class thread_pool + +/** + * @brief A utility class to synchronize printing to an output stream by different threads. + */ +class [[nodiscard]] synced_stream +{ +public: + /** + * @brief Construct a new synced stream which prints to `std::cout`. + */ + explicit synced_stream() + { + add_stream(std::cout); + } + + /** + * @brief Construct a new synced stream which prints to the given output stream(s). + * + * @tparam T The types of the output streams to print to. + * @param streams The output streams to print to. + */ + template + explicit synced_stream(T&... streams) + { + (add_stream(streams), ...); + } + + /** + * @brief Add a stream to the list of output streams to print to. + * + * @param stream The stream. + */ + void add_stream(std::ostream& stream) + { + out_streams.push_back(&stream); + } + + /** + * @brief Get a reference to a vector containing pointers to the output streams to print to. + * + * @return The output streams. + */ + std::vector& get_streams() noexcept + { + return out_streams; + } + + /** + * @brief Print any number of items into the output stream. Ensures that no other threads print to this stream simultaneously, as long as they all exclusively use the same `BS::synced_stream` object to print. + * + * @tparam T The types of the items. + * @param items The items to print. + */ + template + void print(const T&... items) + { + const std::scoped_lock stream_lock(stream_mutex); + for (std::ostream* const stream : out_streams) + (*stream << ... << items); + } + + /** + * @brief Print any number of items into the output stream, followed by a newline character. Ensures that no other threads print to this stream simultaneously, as long as they all exclusively use the same `BS::synced_stream` object to print. + * + * @tparam T The types of the items. + * @param items The items to print. + */ + template + void println(T&&... items) + { + print(std::forward(items)..., '\n'); + } + + /** + * @brief Remove a stream from the list of output streams to print to. + * + * @param stream The stream. + */ + void remove_stream(std::ostream& stream) + { + out_streams.erase(std::remove(out_streams.begin(), out_streams.end(), &stream), out_streams.end()); + } + + /** + * @brief A stream manipulator to pass to a `BS::synced_stream` (an explicit cast of `std::endl`). Prints a newline character to the stream, and then flushes it. Should only be used if flushing is desired, otherwise a newline character should be used instead. + */ + inline static std::ostream& (&endl)(std::ostream&) = static_cast(std::endl); + + /** + * @brief A stream manipulator to pass to a `BS::synced_stream` (an explicit cast of `std::flush`). Used to flush the stream. + */ + inline static std::ostream& (&flush)(std::ostream&) = static_cast(std::flush); + +private: + /** + * @brief The output streams to print to. + */ + std::vector out_streams; + + /** + * @brief A mutex to synchronize printing. + */ + mutable std::mutex stream_mutex; +}; // class synced_stream + +#ifdef __cpp_lib_semaphore +using binary_semaphore = std::binary_semaphore; +template ::max()> +using counting_semaphore = std::counting_semaphore; +#else +/** + * @brief A polyfill for `std::counting_semaphore`, to be used if C++20 features are not available. A `counting_semaphore` is a synchronization primitive that allows more than one concurrent access to the same resource. The number of concurrent accessors is limited by the semaphore's counter, which is decremented when a thread acquires the semaphore and incremented when a thread releases the semaphore. If the counter is zero, a thread trying to acquire the semaphore will be blocked until another thread releases the semaphore. + * + * @tparam LeastMaxValue The least maximum value of the counter. (In this implementation, it is also the actual maximum value.) + */ +template ::max()> +class [[nodiscard]] counting_semaphore +{ + static_assert(LeastMaxValue >= 0, "The least maximum value for a counting semaphore must not be negative."); + +public: + /** + * @brief Construct a new counting semaphore with the given initial counter value. + * + * @param desired The initial counter value. + */ + constexpr explicit counting_semaphore(const std::ptrdiff_t desired) : counter(desired) {} + + // The copy and move constructors and assignment operators are deleted. The semaphore cannot be copied or moved. + counting_semaphore(const counting_semaphore&) = delete; + counting_semaphore(counting_semaphore&&) = delete; + counting_semaphore& operator=(const counting_semaphore&) = delete; + counting_semaphore& operator=(counting_semaphore&&) = delete; + ~counting_semaphore() = default; + + /** + * @brief Returns the internal counter's maximum possible value, which in this implementation is equal to `LeastMaxValue`. + * + * @return The internal counter's maximum possible value. + */ + [[nodiscard]] static constexpr std::ptrdiff_t max() noexcept + { + return LeastMaxValue; + } + + /** + * @brief Atomically decrements the internal counter by 1 if it is greater than 0; otherwise blocks until it is greater than 0 and can successfully decrement the internal counter. + */ + void acquire() + { + std::unique_lock lock(mutex); + cv.wait(lock, + [this] + { + return counter > 0; + }); + --counter; + } + + /** + * @brief Atomically increments the internal counter. Any thread(s) waiting for the counter to be greater than 0, such as due to being blocked in `acquire()`, will subsequently be unblocked. + * + * @param update The amount to increment the internal counter by. Defaults to 1. + */ + void release(const std::ptrdiff_t update = 1) + { + { + const std::scoped_lock lock(mutex); + counter += update; + } + cv.notify_all(); + } + + /** + * @brief Tries to atomically decrement the internal counter by 1 if it is greater than 0; no blocking occurs regardless. + * + * @return `true` if decremented the internal counter, `false` otherwise. + */ + bool try_acquire() + { + std::scoped_lock lock(mutex); + if (counter > 0) + { + --counter; + return true; + } + return false; + } + + /** + * @brief Tries to atomically decrement the internal counter by 1 if it is greater than 0; otherwise blocks until it is greater than 0 and can successfully decrement the internal counter, or the `rel_time` duration has been exceeded. + * + * @tparam Rep An arithmetic type representing the number of ticks to wait. + * @tparam Period An `std::ratio` representing the length of each tick in seconds. + * @param rel_time The duration the function must wait. Note that the function may wait for longer. + * @return `true` if decremented the internal counter, `false` otherwise. + */ + template + bool try_acquire_for(const std::chrono::duration& rel_time) + { + std::unique_lock lock(mutex); + if (!cv.wait_for(lock, rel_time, + [this] + { + return counter > 0; + })) + return false; + --counter; + return true; + } + + /** + * @brief Tries to atomically decrement the internal counter by 1 if it is greater than 0; otherwise blocks until it is greater than 0 and can successfully decrement the internal counter, or the `abs_time` time point has been passed. + * + * @tparam Clock The type of the clock used to measure time. + * @tparam Duration An `std::chrono::duration` type used to indicate the time point. + * @param abs_time The earliest time the function must wait until. Note that the function may wait for longer. + * @return `true` if decremented the internal counter, `false` otherwise. + */ + template + bool try_acquire_until(const std::chrono::time_point& abs_time) + { + std::unique_lock lock(mutex); + if (!cv.wait_until(lock, abs_time, + [this] + { + return counter > 0; + })) + return false; + --counter; + return true; + } + +private: + /** + * @brief The semaphore's counter. + */ + std::ptrdiff_t counter; + + /** + * @brief A condition variable used to wait for the counter. + */ + std::condition_variable cv; + + /** + * @brief A mutex used to synchronize access to the counter. + */ + mutable std::mutex mutex; +}; + +/** + * @brief A polyfill for `std::binary_semaphore`, to be used if C++20 features are not available. + */ +using binary_semaphore = counting_semaphore<1>; +#endif +} // namespace BS +#endif // BS_THREAD_POOL_HPP diff --git a/include/BS_thread_pool_utils.hpp b/include/BS_thread_pool_utils.hpp deleted file mode 100644 index aac6706..0000000 --- a/include/BS_thread_pool_utils.hpp +++ /dev/null @@ -1,203 +0,0 @@ -#ifndef BS_THREAD_POOL_UTILS_HPP -#define BS_THREAD_POOL_UTILS_HPP -/** - * @file BS_thread_pool_utils.hpp - * @author Barak Shoshany (baraksh@gmail.com) (https://baraksh.com) - * @version 4.1.0 - * @date 2024-03-22 - * @copyright Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 - * - * @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. - */ - -#include // std::chrono -#include // std::promise, std::shared_future -#include // std::initializer_list -#include // std::cout -#include // std::make_unique, std::unique_ptr -#include // std::mutex, std::scoped_lock -#include // std::endl, std::flush, std::ostream -#include // std::forward - -/** - * @brief A namespace used by Barak Shoshany's projects. - */ -namespace BS { -// Macros indicating the version of the thread pool utilities library. -#define BS_THREAD_POOL_UTILS_VERSION_MAJOR 4 -#define BS_THREAD_POOL_UTILS_VERSION_MINOR 1 -#define BS_THREAD_POOL_UTILS_VERSION_PATCH 0 - -/** - * @brief A utility class to allow simple signalling between threads. - */ -class [[nodiscard]] signaller -{ -public: - /** - * @brief Construct a new signaller. - */ - signaller() : promise(), future(promise.get_future()) {} - - // The copy constructor and copy assignment operator are deleted. The signaller works using a promise, which cannot be copied. - signaller(const signaller&) = delete; - signaller& operator=(const signaller&) = delete; - - // The move constructor and move assignment operator are defaulted. - signaller(signaller&&) = default; - signaller& operator=(signaller&&) = default; - - /** - * @brief Inform any waiting threads that the signaller is ready. - */ - void ready() - { - promise.set_value(); - } - - /** - * @brief Wait until the signaller is ready. - */ - void wait() - { - future.wait(); - } - -private: - /** - * @brief A promise used to set the state of the signaller. - */ - std::promise promise; - - /** - * @brief A future used to wait for the signaller. - */ - std::shared_future future; -}; // class signaller - -/** - * @brief A utility class to synchronize printing to an output stream by different threads. - */ -class [[nodiscard]] synced_stream -{ -public: - /** - * @brief Construct a new synced stream. - * - * @param stream The output stream to print to. The default value is `std::cout`. - */ - explicit synced_stream(std::ostream& stream = std::cout) : out_stream(stream) {} - - // The copy and move constructors and assignment operators are deleted. The synced stream uses a mutex, which cannot be copied or moved. - synced_stream(const synced_stream&) = delete; - synced_stream(synced_stream&&) = delete; - synced_stream& operator=(const synced_stream&) = delete; - synced_stream& operator=(synced_stream&&) = delete; - - /** - * @brief Print any number of items into the output stream. Ensures that no other threads print to this stream simultaneously, as long as they all exclusively use the same `synced_stream` object to print. - * - * @tparam T The types of the items. - * @param items The items to print. - */ - template - void print(T&&... items) - { - const std::scoped_lock stream_lock(stream_mutex); - (out_stream << ... << std::forward(items)); - } - - /** - * @brief Print any number of items into the output stream, followed by a newline character. Ensures that no other threads print to this stream simultaneously, as long as they all exclusively use the same `synced_stream` object to print. - * - * @tparam T The types of the items. - * @param items The items to print. - */ - template - void println(T&&... items) - { - print(std::forward(items)..., '\n'); - } - - /** - * @brief A stream manipulator to pass to a `synced_stream` (an explicit cast of `std::endl`). Prints a newline character to the stream, and then flushes it. Should only be used if flushing is desired, otherwise a newline character should be used instead. - */ - inline static std::ostream& (&endl)(std::ostream&) = static_cast(std::endl); - - /** - * @brief A stream manipulator to pass to a `synced_stream` (an explicit cast of `std::flush`). Used to flush the stream. - */ - inline static std::ostream& (&flush)(std::ostream&) = static_cast(std::flush); - -private: - /** - * @brief The output stream to print to. - */ - std::ostream& out_stream; - - /** - * @brief A mutex to synchronize printing. - */ - mutable std::mutex stream_mutex = {}; -}; // class synced_stream - -/** - * @brief A utility class to measure execution time for benchmarking purposes. - */ -class [[nodiscard]] timer -{ -public: - /** - * @brief Construct a new timer and immediately start measuring time. - */ - timer() = default; - - /** - * @brief Get the number of milliseconds that have elapsed since the object was constructed or since `start()` was last called, but keep the timer ticking. - * - * @return The number of milliseconds. - */ - [[nodiscard]] std::chrono::milliseconds::rep current_ms() const - { - return (std::chrono::duration_cast(std::chrono::steady_clock::now() - start_time)).count(); - } - - /** - * @brief Start (or restart) measuring time. Note that the timer starts ticking as soon as the object is created, so this is only necessary if we want to restart the clock later. - */ - void start() - { - start_time = std::chrono::steady_clock::now(); - } - - /** - * @brief Stop measuring time and store the elapsed time since the object was constructed or since `start()` was last called. - */ - void stop() - { - elapsed_time = std::chrono::steady_clock::now() - start_time; - } - - /** - * @brief Get the number of milliseconds stored when `stop()` was last called. - * - * @return The number of milliseconds. - */ - [[nodiscard]] std::chrono::milliseconds::rep ms() const - { - return (std::chrono::duration_cast(elapsed_time)).count(); - } - -private: - /** - * @brief The time point when measuring started. - */ - std::chrono::time_point start_time = std::chrono::steady_clock::now(); - - /** - * @brief The duration that has elapsed between `start()` and `stop()`. - */ - std::chrono::duration elapsed_time = std::chrono::duration::zero(); -}; // class timer -} // namespace BS -#endif diff --git a/modules/BS.thread_pool.cppm b/modules/BS.thread_pool.cppm new file mode 100644 index 0000000..1951240 --- /dev/null +++ b/modules/BS.thread_pool.cppm @@ -0,0 +1,56 @@ +/** + * ██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + * ██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + * ██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ + * + * @file BS.thread_pool.cppm + * @author Barak Shoshany (baraksh@gmail.com) (https://baraksh.com/) + * @version 5.0.0 + * @date 2024-12-19 + * @copyright Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 + * + * @brief `BS::thread_pool`: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library. This module file wraps the header file BS_thread_pool.hpp inside a C++20 module so it can be imported using `import BS.thread_pool`. + */ + +module; + +// A macro indicating to the library that it is being imported as a module, as well as the version of the module file, which must match the version of the header file. +#define BS_THREAD_POOL_MODULE 5, 0, 0 + +#include "BS_thread_pool.hpp" + +export module BS.thread_pool; + +export namespace BS { +using BS::binary_semaphore; +using BS::common_index_type_t; +using BS::counting_semaphore; +using BS::light_thread_pool; +using BS::multi_future; +using BS::pause_thread_pool; +using BS::pr; +using BS::priority_t; +using BS::priority_thread_pool; +using BS::synced_stream; +using BS::this_thread; +using BS::thread_pool; +using BS::thread_pool_import_std; +using BS::thread_pool_module; +using BS::thread_pool_native_extensions; +using BS::thread_pool_version; +using BS::tp; +using BS::version; +using BS::wait_deadlock; +using BS::wdc_thread_pool; + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS +using BS::get_os_process_affinity; +using BS::get_os_process_priority; +using BS::os_process_priority; +using BS::os_thread_priority; +using BS::set_os_process_affinity; +using BS::set_os_process_priority; +#endif +} // namespace BS diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8b8a899 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,382 @@ +[tool.pyright] +analyzeUnannotatedFunctions = true +deprecateTypingAliases = true +disableBytesTypePromotions = true +enableExperimentalFeatures = false +enableReachabilityAnalysis = true +enableTypeIgnoreComments = true +extraPaths = ["."] +pythonPlatform = "All" +pythonVersion = "3.13" +reportAbstractUsage = "error" +reportArgumentType = "error" +reportAssertAlwaysTrue = "error" +reportAssertTypeFailure = "error" +reportAssignmentType = "error" +reportAttributeAccessIssue = "error" +reportCallInDefaultInitializer = "error" +reportCallIssue = "error" +reportConstantRedefinition = "error" +reportDeprecated = "error" +reportDuplicateImport = "error" +reportFunctionMemberAccess = "error" +reportGeneralTypeIssues = "error" +reportImplicitOverride = "error" +reportImplicitStringConcatenation = "error" +reportImportCycles = "error" +reportIncompatibleMethodOverride = "error" +reportIncompatibleVariableOverride = "error" +reportIncompleteStub = "error" +reportInconsistentConstructor = "error" +reportInconsistentOverload = "error" +reportIndexIssue = "error" +reportInvalidStringEscapeSequence = "error" +reportInvalidStubStatement = "error" +reportInvalidTypeArguments = "error" +reportInvalidTypeForm = "error" +reportInvalidTypeVarUse = "error" +reportMatchNotExhaustive = "error" +reportMissingImports = "error" +reportMissingModuleSource = "error" +reportMissingParameterType = "error" +reportMissingTypeArgument = "error" +reportMissingTypeStubs = "error" +reportNoOverloadImplementation = "error" +reportOperatorIssue = "none" +reportOptionalCall = "error" +reportOptionalContextManager = "error" +reportOptionalIterable = "error" +reportOptionalMemberAccess = "error" +reportOptionalOperand = "error" +reportOptionalSubscript = "error" +reportOverlappingOverload = "error" +reportPossiblyUnboundVariable = "error" +reportPrivateImportUsage = "error" +reportPrivateUsage = "error" +reportPropertyTypeMismatch = "none" +reportRedeclaration = "error" +reportReturnType = "error" +reportSelfClsParameterName = "error" +reportShadowedImports = "error" +reportTypeCommentUsage = "error" +reportTypedDictNotRequiredAccess = "error" +reportUnboundVariable = "error" +reportUndefinedVariable = "error" +reportUnhashable = "error" +reportUninitializedInstanceVariable = "error" +reportUnknownArgumentType = "error" +reportUnknownLambdaType = "error" +reportUnknownMemberType = "none" +reportUnknownParameterType = "error" +reportUnknownVariableType = "error" +reportUnnecessaryCast = "error" +reportUnnecessaryComparison = "error" +reportUnnecessaryContains = "error" +reportUnnecessaryIsInstance = "error" +reportUnnecessaryTypeIgnoreComment = "error" +reportUnsupportedDunderAll = "error" +reportUntypedBaseClass = "error" +reportUntypedClassDecorator = "error" +reportUntypedFunctionDecorator = "error" +reportUntypedNamedTuple = "error" +reportUnusedCallResult = "error" +reportUnusedClass = "warning" +reportUnusedCoroutine = "error" +reportUnusedExcept = "error" +reportUnusedExpression = "error" +reportUnusedFunction = "warning" +reportUnusedImport = "warning" +reportUnusedVariable = "warning" +reportWildcardImportFromLibrary = "error" +strictDictionaryInference = true +strictListInference = true +strictParameterNoneValue = true +strictSetInference = true +typeCheckingMode = "strict" +useLibraryCodeForTypes = true + +[tool.ruff] +indent-width = 4 +line-length = 320 +target-version = "py313" + +[tool.ruff.format] +docstring-code-format = false +docstring-code-line-length = "dynamic" +indent-style = "space" +line-ending = "lf" +quote-style = "double" +skip-magic-trailing-comma = false + +[tool.ruff.lint] +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" +fixable = ["ALL"] +ignore = [ + "BLE001", + "C901", + "D200", + "D203", + "D205", + "D212", + "D400", + "D402", + "D415", + "E501", + "INP001", + "N814", + "N999", + "PLR0912", + "PLR0913", + "PLR2004", + "RUF009", + "S310", + "S602", + "S603", + "S607", + "SIM108", + "SLF001", + "T201", + "TCH003", + "TD002", + "TD003", + "UP015", +] +select = ["ALL"] +unfixable = [] + +[tool.ruff.lint.per-file-ignores] +"Interactive*" = ["ALL"] + +[tool.pylint.main] +analyse-fallback-blocks = false +clear-cache-post-run = false +exit-zero = false +extension-pkg-allow-list = [] +extension-pkg-whitelist = [] +fail-on = "" +fail-under = 10 +from-stdin = false +ignore-paths = [] +ignore-patterns = [] +ignored-modules = [] +init-hook = "" +jobs = 0 +limit-inference-results = 100 +load-plugins = [] +persistent = true +prefer-stubs = true +py-version = "3.13" +recursive = false +source-roots = [] +suggestion-mode = true +unsafe-load-any-extension = false + +[tool.pylint.basic] +argument-naming-style = "snake_case" +argument-rgx = "" +attr-naming-style = "snake_case" +attr-rgx = "" +bad-names = [] +bad-names-rgxs = "" +class-attribute-naming-style = "any" +class-attribute-rgx = "" +class-const-naming-style = "UPPER_CASE" +class-const-rgx = "" +class-naming-style = "PascalCase" +class-rgx = "" +const-naming-style = "UPPER_CASE" +const-rgx = "" +docstring-min-length = -1 +function-naming-style = "snake_case" +function-rgx = "" +good-names = ["_"] +good-names-rgxs = "" +include-naming-hint = true +inlinevar-naming-style = "any" +inlinevar-rgx = "" +method-naming-style = "snake_case" +method-rgx = "" +module-naming-style = "snake_case" +module-rgx = "" +name-group = [] +no-docstring-rgx = "" +property-classes = ["abc.abstractproperty"] +typealias-rgx = "" +typevar-rgx = "" +variable-naming-style = "snake_case" +variable-rgx = "" + +[tool.pylint.classes] +check-protected-access-in-special-methods = true +defining-attr-methods = [ + "__init__", + "__new__", + "__post_init__", + "asyncSetUp", + "setUp", +] +exclude-protected = [ + "_asdict", + "_fields", + "_make", + "_replace", + "_source", + "os._exit", +] +valid-classmethod-first-arg = ["cls"] +valid-metaclass-classmethod-first-arg = ["mcs"] + +[tool.pylint.design] +exclude-too-few-public-methods = [] +ignored-parents = [] +max-args = 5 +max-attributes = 7 +max-bool-expr = 5 +max-branches = 12 +max-locals = 15 +max-parents = 7 +max-public-methods = 20 +max-returns = 6 +max-statements = 50 +min-public-methods = 2 + +[tool.pylint.exceptions] +overgeneral-exceptions = ["builtins.BaseException", "builtins.Exception"] + +[tool.pylint.format] +expected-line-ending-format = "LF" +ignore-long-lines = "^\\s*(# )??$" +indent-after-paren = 4 +indent-string = " " +max-line-length = 1024 +max-module-lines = 8192 +single-line-class-stmt = false +single-line-if-stmt = false + +[tool.pylint.imports] +allow-any-import-level = [] +allow-reexport-from-package = false +allow-wildcard-with-all = false +deprecated-modules = [] +ext-import-graph = "" +import-graph = "" +int-import-graph = "" +known-standard-library = [] +known-third-party = [] +preferred-modules = [] + +[tool.pylint.logging] +logging-format-style = "new" +logging-modules = ["logging"] + +[tool.pylint."messages control"] +confidence = [] +disable = [ + "broad-exception-caught", + "consider-using-enumerate", + "expression-not-assigned", + "import-error", + "invalid-unary-operand-type", + "missing-module-docstring", + "named-expr-without-context", + "not-callable", + "pointless-statement", + "protected-access", + "too-few-public-methods", + "too-many-arguments", + "too-many-boolean-expressions", + "too-many-branches", + "too-many-instance-attributes", + "too-many-locals", + "too-many-nested-blocks", + "too-many-public-methods", + "ungrouped-imports", + "use-implicit-booleaness-not-comparison-to-string", + "use-implicit-booleaness-not-comparison-to-zero", + "wrong-import-order", + "wrong-import-position", +] +enable = ["all"] + +[tool.pylint.method_args] +timeout-methods = [ + "requests.api.delete", + "requests.api.get", + "requests.api.head", + "requests.api.options", + "requests.api.patch", + "requests.api.post", + "requests.api.put", + "requests.api.request", +] + +[tool.pylint.miscellaneous] +notes = ["TODO"] +notes-rgx = "" + +[tool.pylint.refactoring] +max-nested-blocks = 5 +never-returning-functions = ["argparse.parse_error", "sys.exit"] +suggest-join-with-non-empty-separator = true + +[tool.pylint.reports] +evaluation = "max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10))" +msg-template = "" +output-format = "text" +reports = true +score = true + +[tool.pylint.similarities] +ignore-comments = true +ignore-docstrings = true +ignore-imports = true +ignore-signatures = true +min-similarity-lines = 4 + +[tool.pylint.spelling] +max-spelling-suggestions = 4 +spelling-dict = "" +spelling-ignore-comment-directives = "fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy:" +spelling-ignore-words = "" +spelling-private-dict-file = "" +spelling-store-unknown-words = false + +[tool.pylint.typecheck] +contextmanager-decorators = ["contextlib.contextmanager"] +generated-members = [] +ignore-mixin-members = true +ignore-none = true +ignore-on-opaque-inference = false +ignored-checks-for-mixins = [ + "attribute-defined-outside-init", + "no-member", + "not-async-context-manager", + "not-context-manager", +] +ignored-classes = [ + "_thread._local", + "argparse.Namespace", + "optparse.Values", + "thread._local", +] +missing-member-hint = true +missing-member-hint-distance = 1 +missing-member-max-choices = 1 +mixin-class-rgx = ".*[Mm]ixin" +signature-mutators = [] + +[tool.pylint.variables] +additional-builtins = [] +allow-global-unused-variables = true +allowed-redefined-builtins = [] +callbacks = ["_cb", "cb_"] +dummy-variables-rgx = "^_.*" +ignored-argument-names = "^_.*" +init-import = true +redefining-builtins-modules = [ + "builtins", + "future.builtins", + "io", + "past.builtins", + "six.moves", +] diff --git a/scripts/clear_folder.py b/scripts/clear_folder.py new file mode 100644 index 0000000..fed58ab --- /dev/null +++ b/scripts/clear_folder.py @@ -0,0 +1,63 @@ +""" +██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ + +`BS::thread_pool`: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library +v5.0.0 (2024-12-19) +By Barak Shoshany +Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 + +This Python script is used in the library's development environment to clear all files a folder. It is not part of the library itself. +""" + +import argparse +import importlib +import importlib.util +import pathlib +import sys +from importlib.machinery import ModuleSpec +from types import ModuleType + +send2trash_spec: ModuleSpec | None = importlib.util.find_spec("send2trash") +if send2trash_spec is not None: + # Install with `pip install send2trash`. + send2trash: ModuleType = importlib.import_module("send2trash") + + def delete_files_in_folder(folder_path: pathlib.Path) -> None: + """Recursively delete all files in a folder.""" + for child in folder_path.iterdir(): + if child.is_file(): + send2trash.send2trash(paths=child) + else: + delete_files_in_folder(child) + send2trash.send2trash(folder_path) +else: + print("Note: Module send2trash not found; deleting files permanently.") + + def delete_files_in_folder(folder_path: pathlib.Path) -> None: + """Fallback function in case `send2trash` is not installed.""" + for child in folder_path.iterdir(): + if child.is_file(): + child.unlink() + else: + delete_files_in_folder(child) + folder_path.rmdir() + + +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +_ = parser.add_argument("folder", help="the folder to clear (relative to this script's location)") +parsed_args: argparse.Namespace = parser.parse_args(args=None if len(sys.argv) > 1 else ["--help"]) + +folder: pathlib.Path = (pathlib.Path(__file__).parent / parsed_args.folder).resolve() +if not folder.exists(): + print(f"Folder {folder} does not exist; creating empty.") +else: + try: + delete_files_in_folder(folder) + folder.mkdir() + print(f"Folder {folder} successfully cleared.") + except Exception as exc: + sys.exit(f"Error clearing folder {folder}: {exc}") diff --git a/scripts/compile_cpp.py b/scripts/compile_cpp.py new file mode 100644 index 0000000..31311df --- /dev/null +++ b/scripts/compile_cpp.py @@ -0,0 +1,409 @@ +""" +██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ + +`BS::thread_pool`: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library +v5.0.0 (2024-12-19) +By Barak Shoshany +Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 + +This Python script can be used to compile simple C++ programs (with only a few source and/or header files) using a variety of compilers, C++ standards, and other options. It also includes support for C++20 modules and C++23 Standard Library modules. It is used in the thread pool library's development environment to compile and run the test program using different compilers and C++ standards. It is not part of the library itself, but users of the library may find it useful, especially if they wish to use the library as a C++20 module. +""" + +import argparse +import os +import pathlib +import platform +import shutil +import subprocess +import sys +import time + +import yaml # Install with `pip install pyyaml`. + +separator: str = "=" * 60 + + +class Args: + """A class to collect the command line arguments with proper type checking.""" + + def __init__(self, parsed_ns: argparse.Namespace) -> None: + """Store the parsed arguments.""" + self.files: list[str] = parsed_ns.files + self.arch: str = parsed_ns.arch + self.as_module: bool = parsed_ns.as_module + self.compiler: str | None = parsed_ns.compiler + self.define: list[str] = parsed_ns.define if parsed_ns.define is not None else [] + self.flag: list[str] = parsed_ns.flag if parsed_ns.flag is not None else [] + self.ignore_yaml: bool = parsed_ns.ignore_yaml + self.include: list[str] = parsed_ns.include if parsed_ns.include is not None else [] + self.module: list[str] = parsed_ns.module if parsed_ns.module is not None else [] + self.output: str | None = parsed_ns.output + self.pass_args: list[str] = parsed_ns.pass_args if parsed_ns.pass_args is not None else [] + self.run: bool = parsed_ns.run + self.std_module: str | None = parsed_ns.std_module + self.std: str = parsed_ns.std + self.type: str = parsed_ns.type + self.verbose: bool = parsed_ns.verbose + + +# Parse the command-line arguments. +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +_ = parser.add_argument("files", nargs="+", help="the source file(s) to compile") +_ = parser.add_argument("-a", "--arch", choices=["amd64", "arm64"], default="amd64", help="the target architecture (MSVC only)") +_ = parser.add_argument("-c", "--compiler", choices=["cl", "clang++", "g++"], help="which compiler to use (auto determined if not specified)") +_ = parser.add_argument("-d", "--define", action="append", help="macros to define (use multiple times if more than one) [in addition to those in compile_cpp.yaml]") +_ = parser.add_argument("-f", "--flag", action="append", help="extra compiler flags to add (use multiple times if more than one) [in addition to those in compile_cpp.yaml]") +_ = parser.add_argument("-g", "--ignore-yaml", action="store_true", help="ignore the compile_cpp.yaml file") +_ = parser.add_argument("-i", "--include", action="append", help="the include folder to use (use multiple times if more than one) [in addition to those in compile_cpp.yaml]") +_ = parser.add_argument("-l", "--as-module", action="store_true", help="Compile file as module") +_ = parser.add_argument("-m", "--module", action="append", help='C++20 module files to use if desired, in the format "module_name=module_file,dependent_files,..." (use multiple times if more than one) [in addition to those in compile_cpp.yaml]') +_ = parser.add_argument("-o", "--output", help="the output folder (end with / to create, taken from compile_cpp.yaml if not specified) and/or executable name (auto determined if not specified)") +_ = parser.add_argument("-p", "--pass", action="append", dest="pass_args", help="pass command line arguments to the compiled program when running it, if -r is specified (use multiple times if more than one) [in addition to those in compile_cpp.yaml]") +_ = parser.add_argument("-r", "--run", action="store_true", help="run the program after compiling it") +_ = parser.add_argument("-s", "--std", choices=["c++17", "c++20", "c++23"], default="c++23", help="which C++ standard to use") +_ = parser.add_argument("-t", "--type", choices=["debug", "release"], default="debug", help="whether to compile in debug or release mode") +_ = parser.add_argument("-u", "--std-module", help="path to the standard library module (C++23 only, taken from compile_cpp.yaml if not specified, use 'auto' to auto-detect, 'disable' to explicitly disable)") +_ = parser.add_argument("-v", "--verbose", action="store_true", help="whether to print this script's diagnostic messages") +args = Args(parser.parse_args(args=None if len(sys.argv) > 1 else ["--help"])) + + +def print_if_verbose(message: str) -> None: + """Print a message, but only if the verbose flag is set.""" + if args.verbose: + print(message) + + +# Collect the full path(s) to the source file(s). +source_paths: list[pathlib.Path] = [pathlib.Path(file).resolve() for file in args.files] + +# Determine the compiler if it is not given. +compiler: str = "" +vs_pwsh_path: str = r"C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1" +if args.compiler is not None: + compiler = args.compiler +else: # noqa: PLR5501 + # On Windows, we default to MSVC if the Visual Studio Developer PowerShell script exists, otherwise we fall back to Clang, and then GCC. + if platform.system() == "Windows": + if pathlib.Path(vs_pwsh_path).exists(): + compiler = "cl" + elif shutil.which("clang++") is not None: + compiler = "clang++" + elif shutil.which("g++") is not None: + compiler = "g++" + # On Linux, we default to GCC if it is available, otherwise we fall back to Clang. + elif platform.system() == "Linux": + if shutil.which("g++") is not None: + compiler = "g++" + elif shutil.which("clang++") is not None: + compiler = "clang++" + # On macOS, we just check if Clang is available. + elif platform.system() == "Darwin" and shutil.which("clang++") is not None: + compiler = "clang++" +if compiler == "": + sys.exit("Error: No compiler found!") + +# If a file named `compile_cpp.yaml` exists in the current working directory, read the configuration from it. All options are added to those from the command line, except the output file/folder and path to the standard library module, which are only used if not provided in the command line. Note that all folders should be specified relative to the current working directory. +defines: list[str] = args.define[:] +flags: list[str] = args.flag[:] +includes: list[str] = args.include[:] +modules: dict[str, list[str]] = {name: files.split(",") for module in args.module for name, files in (module.split("="),)} +output: str | None = args.output +pass_args: list[str] = args.pass_args[:] +compile_yaml: pathlib.Path = pathlib.Path.cwd() / "compile_cpp.yaml" +std_module: str | None = args.std_module + +if not args.ignore_yaml and compile_yaml.exists(): + with compile_yaml.open("r") as file: + compile_config = yaml.safe_load(file) + if "defines" in compile_config: + defines.extend(compile_config["defines"]) + if "flags" in compile_config and compiler in compile_config["flags"]: + flags.extend(compile_config["flags"][compiler]) + if "includes" in compile_config: + includes.extend(compile_config["includes"]) + if "modules" in compile_config: + modules.update(compile_config["modules"]) + if output is None and "output" in compile_config: + output = compile_config["output"] + if "pass_args" in compile_config: + pass_args.extend(compile_config["pass_args"]) + if std_module is None and "std_module" in compile_config and platform.system() in compile_config["std_module"] and compiler in compile_config["std_module"][platform.system()] and len(compile_config["std_module"][platform.system()][compiler]) > 0: + std_module = compile_config["std_module"][platform.system()][compiler] + +# Importing the C++ Standard Library is only available in C++23 mode, and currently only officially supported by MSVC and Clang. If "disable" is specified for the standard library module, we skip it; this is used to avoid infinite recursion. +use_std_module: bool = not (std_module is None or std_module == "disable" or args.std != "c++23" or compiler not in ["cl", "clang++"]) + +if use_std_module and std_module is not None: + modules = {"std": [std_module], **modules} + +# Figure out the path to the MSVC or Clang std module, if relevant. +if use_std_module and "std" in modules and modules["std"][0].strip() == "auto": + success: bool = False + if platform.system() == "Windows" and compiler == "cl": + vc_version_path: pathlib.Path = pathlib.Path(r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt") + if vc_version_path.exists(): + with vc_version_path.open("r", encoding="utf-8") as vc_version_file: + vc_version: str = vc_version_file.read().strip() + vc_tools_path: str = rf"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\{vc_version}" + modules["std"][0] = rf"{vc_tools_path}\modules\std.ixx" + success = True + elif compiler == "clang++": + # Note: The Clang `std` module is only available with libc++. + if platform.system() == "Windows": + # On Windows, libc++ is most likely installed via MSYS2, so the `std` module should be at `C:\msys64\clang64\share\libc++\v1\std.cppm`. We calculate it relative to the path where `clang++.exe` is located, in case the MSYS2 installation folder is different. + clang_path: str | None = shutil.which("clang++") + if clang_path: + std_path: pathlib.Path = pathlib.Path(clang_path).parent.parent / "share" / "libc++" / "v1" / "std.cppm" + if std_path.exists(): + modules["std"][0] = str(std_path.absolute()) + success = True + elif platform.system() == "Darwin": + # On macOS, the `std` module should be at `/usr/local/Cellar/llvm//share/libc++/v1/std.cppm`. We use `find` to locate it without needing to know the exact LLVM version. + llvm_path: str = subprocess.check_output(["find", "/usr/local/Cellar/llvm", "-name", "std.cppm"], text=True).strip() + if llvm_path: + modules["std"][0] = llvm_path + success = True + elif platform.system() == "Linux": + # On Linux, the `std` module should be at `/usr/lib/llvm-/share/libc++/v1/std.cppm`. We use `find` to locate it without needing to know the exact LLVM version. + llvm_path: str = subprocess.check_output(["find", "/usr/lib", "-name", "std.cppm"], text=True).strip() + if llvm_path: + modules["std"][0] = llvm_path + success = True + if not success: + sys.exit('Error: "auto" specified for the standard library module path, but the script could not locate it. Please specify the path manually.') + +# Determine the name of the executable file and the build folder. +executable_path: pathlib.Path | None = None +build_folder: pathlib.Path +auto_executable: bool = False +if output is not None: + # Calculate the output path relative to the current working directory. Note that if the path is absolute, `pathlib` will automatically use the absolute path instead of a relative path. + output_path: pathlib.Path = (pathlib.Path.cwd() / output).resolve() + if output.endswith(("/", "\\")) or output_path.is_dir(): + # If the output path is a directory, we use it as the build folder, and automatically determine the name of the executable file. + build_folder = output_path.resolve() + auto_executable = True + elif output_path.is_absolute(): + # If the output path is an absolute path to a file, the build folder is the file's folder. + build_folder = output_path.parent.resolve() + executable_path = output_path + else: + # If the output path is just a file name, the build folder is the current working directory by default. + build_folder = pathlib.Path.cwd() + executable_path = build_folder / output_path +else: + # If there is no output path at all, the build folder is the current working directory by default, and we automatically determine the name of the executable file. + build_folder = pathlib.Path.cwd() + auto_executable = True + +# If the user did not provide an output file name, we use the name of the first source file, appending the compiler, mode, and C++ standard. +short_compiler: str = "clang" if compiler == "clang++" else "gcc" if compiler == "g++" else "msvc" +suffix: str = f"{args.type}-{short_compiler}-cpp{args.std[-2:]}" +if auto_executable: + extension: str = ".exe" if platform.system() == "Windows" else "" + executable: str = f"{source_paths[0].stem}_{suffix}{extension}" + executable_path = build_folder / executable +if executable_path is None: + sys.exit("Error: Could not determine executable file!") + +# Determine if the build folder exists, and create it if not. +if not pathlib.Path(build_folder).exists(): + pathlib.Path(build_folder).mkdir() + +# If modules are specified, pre-compile them by calling this script recursively, unless the current file is itself a module. As a special case, if the std module is used, we add it even if the current file is a module, in case the module itself wants to import the std module. +module_paths: dict[str, list[pathlib.Path]] = {} +if not args.as_module: + module_paths = {name: [(pathlib.Path.cwd() / file).resolve() for file in files] for name, files in modules.items()} +elif use_std_module and "std" in modules: + module_paths = {"std": [(pathlib.Path.cwd() / modules["std"][0]).resolve()]} +if len(modules) > 0 and (args.std in ["c++20", "c++23"]): + for name, paths in module_paths.items(): + module_flags: list[str] = args.flag[:] + module_extension: str + if compiler == "cl": + module_extension = ".ifc" + module_flags = ["/interface", "/TP", "/c"] + elif compiler == "clang++": + module_extension = ".pcm" + module_flags = ["--precompile", "-Wno-include-angled-in-module-purview", "-Wno-reserved-module-identifier", "-xc++-module"] + else: # compiler == "g++" + # Note: Creating an object file can be disabled with `-fmodule-only`, and it doesn't seem like the object file is actually needed, only the `.gcm` file. However, we create the object file anyway so we can check if the module is up to date, since there appears to be no way to control the name of the `.gcm` file. + module_extension = ".o" + module_flags = ["-fmodules-ts", "-xc++", "-c", f"-fmodule-mapper=|@g++-mapper-server -r{build_folder}"] + # Only the first file on the list needs to be compiled; the rest are dependencies that are only used to check if the compiled module is up to date. + module_file: pathlib.Path = paths[0] + module_output_path: pathlib.Path = build_folder / f"{module_file.stem}_module_{suffix}{module_extension}" + if compiler == "cl": + flags.extend(["/reference", f"{name}={module_output_path.resolve()}"]) + if name != "std" or not args.as_module: + flags.append(str(build_folder / f"{module_output_path.stem}.obj")) + elif compiler == "clang++": + flags.append(f"-fmodule-file={name}={module_output_path.resolve()}") + else: # compiler == "g++" + flags.append("-fmodules-ts") + flags.append(f"-fmodule-mapper=|@g++-mapper-server -r{build_folder}") + if module_output_path.exists(): + module_output_mod: float = module_output_path.stat().st_mtime + if not any((path.exists() and path.stat().st_mtime > module_output_mod) for path in paths): + print_if_verbose(f'Module "{name}" is up to date, skipping compilation.') + continue + try: + command: list[str] = [ + "python" if platform.system() == "Windows" else "python3", + str(pathlib.Path(__file__).resolve()), + str(module_file), + f"-o={module_output_path.resolve()}", + f"-a={args.arch}", + f"-c={compiler}", + f"-s={args.std}", + f"-t={args.type}", + # Note: Not adding the extra options from the configuration file, since they will be added by the script anyway. + *[f"-d={define}" for define in args.define], + *[f"-i={include}" for include in args.include], + *[f"-f={flag}" for flag in (args.flag + module_flags)], + # If compiling the `std` module itself, we need to pass `-u disable` to avoid infinite recursion. Otherwise, we pass along the specified module path if it exists. + *(["-u=disable"] if name == "std" else [f"-u={std_module}"] if std_module is not None else []), + "-l", + *(["-v"] if args.verbose else []), + ] + print_if_verbose(separator) + print_if_verbose(f'Compiling module "{name}" with command: {subprocess.list2cmdline(command)}') + compile_result: subprocess.CompletedProcess[str] = subprocess.run( + args=command, + check=False, + text=True, + ) + if compile_result.returncode != 0: + sys.exit(f"Module compilation failed with return code: {compile_result.returncode}.") + except Exception as exc: + sys.exit(f"Could not compile module due to exception: {exc}.") + print_if_verbose("Compiling program...") + +# Collect the full paths to the include folders, relative to the current working directory. +include_paths: list[pathlib.Path] = [(pathlib.Path.cwd() / folder).resolve() for folder in includes] + +# On macOS, make sure we are using Homebrew Clang, if available, instead of Apple Clang, which does not support C++20 modules. +compiler_path: str | None +if compiler == "clang++" and platform.system() == "Darwin": + compiler_path = "/usr/local/opt/llvm/bin/clang++" + if not pathlib.Path(compiler_path).exists(): + compiler_path = shutil.which(compiler) +else: + compiler_path = shutil.which(compiler) + +# Determine the command to execute based on the chosen compiler and parameters. +command: list[str] +if compiler == "clang++": + command = [ + compiler_path if compiler_path is not None else compiler, + *flags, + *[str(path.resolve()) for path in source_paths], + f"-std={args.std}", + "-g3" if args.type == "debug" else "-O3", + "-o", + str(executable_path), + *([] if platform.system() == "Windows" else ["-pthread"]), + *[item for include in [["-I", str(path.resolve())] for path in include_paths] for item in include], + *([f"-D{define}" for define in defines]), + ] +elif compiler == "g++": + if len(modules) > 0 and (args.std in ["c++20", "c++23"]): + print_if_verbose( + "NOTE: GCC v14.2.0 appears to have an internal compiler error when compiling programs containing modules with any optimization flags other than -Og enabled. Until this is fixed, if you wish to use compiler optimizations, please either include the library as a header file or use a different compiler.", + ) + command = [ + compiler_path if compiler_path is not None else compiler, + *flags, + *[str(path.resolve()) for path in source_paths], + f"-std={args.std}", + "-ggdb3" if args.type == "debug" else "-O3" if not (len(modules) > 0 and (args.std in ["c++20", "c++23"])) else "-Og", + "-o", + str(executable_path), + *([] if platform.system() == "Windows" else ["-pthread"]), + *[item for include in [["-I", str(path.resolve())] for path in include_paths] for item in include], + *([f"-D{define}" for define in defines]), + ] +else: # compiler == "cl" + command = [ + compiler_path if compiler_path is not None else compiler, + *flags, + *[str(path.resolve()) for path in source_paths], + f"/std:{"c++latest" if args.std == "c++23" else args.std}", + *(["/Zi", f"/Fd:{executable_path.with_suffix(".pdb")}"] if args.type == "debug" else ["/O2"]), + *([f"/Fe:{executable_path}"] if not args.as_module else ["/ifcOutput", str(executable_path)]), + f"/Fo:{executable_path.with_suffix(".obj")}", + *[item for include in [["/I", str(path.resolve())] for path in include_paths] for item in include], + "/permissive-", + "/EHsc", + "/nologo", + "/Zc:__cplusplus", + *([f"/D{define}" for define in defines]), + ] + +# For MSVC we also need to invoke the Visual Studio Developer PowerShell script. +if compiler == "cl": + command = ["pwsh.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", f"& '{vs_pwsh_path}' -Arch {args.arch} -HostArch {args.arch} | Out-Null; {subprocess.list2cmdline(command)}"] + +print_if_verbose(separator) +print_if_verbose(f"Compiler: {compiler}") +print_if_verbose(f"C++ Standard: {args.std.upper()}") +print_if_verbose(f"Type: {args.type.title()}") +print_if_verbose(f"Source file(s): [{", ".join([str(path) for path in source_paths])}]") +print_if_verbose(f"Defines: [{", ".join(defines)}]") +print_if_verbose(f"Flags: [{", ".join(flags)}]") +print_if_verbose(f"Includes: [{(", ".join([str(path) for path in include_paths]))}]") +if args.std in ["c++20", "c++23"]: + print_if_verbose(f"Modules: [{", ".join(f"{name}={path[0]}" for name, path in module_paths.items()) if not args.as_module else ""}]") +else: + print_if_verbose("Modules: ") +print_if_verbose(f"Build folder: {build_folder}") +print_if_verbose(f"Binary file: {executable_path.name}") +print_if_verbose(f"Command: {subprocess.list2cmdline(command)}") + +# Perform the actual compilation. +print_if_verbose(separator) +print_if_verbose("Compiling...") +success = False +try: + compile_start: float = time.perf_counter() + compile_result: subprocess.CompletedProcess[str] = subprocess.run( + args=command, + check=False, + text=True, + ) + if compile_result.returncode == 0: + print_if_verbose(f"Compilation completed successfully in {time.perf_counter() - compile_start:.2f} seconds.") + success = True + else: + sys.exit(f"Compilation failed with return code: {compile_result.returncode}.") +except Exception as exc: + sys.exit(f"Could not compile due to exception: {exc}.") + +# If there were compilation errors, delete the executable file if it exists, so the IDE won't mistakenly run the old version anyway. +if not success and executable_path.exists(): + executable_path.unlink() + +if args.run: + # We run the program in the build folder. + os.chdir(build_folder) + print_if_verbose(f"Running program{f" with arguments {" ".join(pass_args)}" if len(pass_args) > 0 else ""}...") + print_if_verbose(separator) + try: + run_start: float = time.perf_counter() + run_result: subprocess.CompletedProcess[str] = subprocess.run( + args=[executable_path, *pass_args], + check=False, + text=True, + ) + print_if_verbose(separator) + if run_result.returncode == 0: + print_if_verbose(f"Program executed successfully in {time.perf_counter() - run_start:.2f} seconds.") + else: + sys.exit(f"Program failed with return code: {run_result.returncode}.") + except Exception as exc: + print_if_verbose(separator) + sys.exit(f"Could not run program due to exception: {exc}.") diff --git a/scripts/test_all.py b/scripts/test_all.py new file mode 100644 index 0000000..b4d5d18 --- /dev/null +++ b/scripts/test_all.py @@ -0,0 +1,87 @@ +""" +██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ + +`BS::thread_pool`: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library +v5.0.0 (2024-12-19) +By Barak Shoshany +Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 + +This Python script is used in the library's development environment to compile and run the test program using all possible combinations of compilers and C++ standards available in the system, in order to test compatibility. +""" + +import argparse +import pathlib +import platform +import shutil +import subprocess +import sys +import time + +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +_ = parser.add_argument("--compile-only", action="store_true", help="only check that the program compiles successfully with all compilers, without running it") +parsed_args: argparse.Namespace = parser.parse_args() + + +def print_message(message: str) -> None: + """Print a message with indicator characters to differentiate it from messages from the compile script.""" + print(f"\n >>>>> {message} <<<<<\n") + + +# Determine which compilers are available. +vs_pwsh_path: str = r"C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1" +compilers: list[str] = [] +if pathlib.Path(vs_pwsh_path).exists(): + compilers.append("cl") +if shutil.which("clang++") is not None: + compilers.append("clang++") +# On macOS, g++ is by default just an alias for clang++, so we skip it. +if shutil.which("g++") is not None and platform.system() != "Darwin": + compilers.append("g++") + +# Compile using all available compilers using all relevant C++ standards. +standards: list[str] = ["c++17", "c++20", "c++23"] +workspace_path: pathlib.Path = pathlib.Path(__file__).parent.parent.resolve() +source_path: pathlib.Path = workspace_path / "tests" / "BS_thread_pool_test.cpp" +script_path: pathlib.Path = workspace_path / "scripts" / "compile_cpp.py" +try: + compile_start: float = time.perf_counter() + for compiler in compilers: + for std in standards: + print_message(f"Compiling with {compiler} using {std.upper()} standard...") + warnings_as_errors: list[str] = ["-f/WX"] if compiler == "cl" else ["-f-Werror"] + command: list[str] = [ + "python" if platform.system() == "Windows" else "python3", + str(script_path.resolve()), + str(source_path.resolve()), + "-c", + compiler, + "-s", + std, + "-t", + "release", + *warnings_as_errors, + *(["-r"] if not parsed_args.compile_only else []), + "-p", + "stdout", + "-p", + "log", + "-p", + "tests", + "-v", + ] + compile_result: subprocess.CompletedProcess[str] = subprocess.run( + args=command, + check=False, + text=True, + ) + if compile_result.returncode != 0: + print_message("Compilation failed, aborting script!") + sys.exit(compile_result.returncode) +except Exception as exc: + print_message(f"Could not compile due to exception: {exc}.") + sys.exit(1) +print_message(f"All tests completed successfully in {time.perf_counter() - compile_start:.2f} seconds.") diff --git a/tests/BS_thread_pool_test.cpp b/tests/BS_thread_pool_test.cpp index fd74a81..aad1daa 100644 --- a/tests/BS_thread_pool_test.cpp +++ b/tests/BS_thread_pool_test.cpp @@ -1,135 +1,97 @@ /** + * ██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + * ██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ + * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ + * ██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ + * * @file BS_thread_pool_test.cpp - * @author Barak Shoshany (baraksh@gmail.com) (https://baraksh.com) - * @version 4.1.0 - * @date 2024-03-22 + * @author Barak Shoshany (baraksh@gmail.com) (https://baraksh.com/) + * @version 5.0.0 + * @date 2024-12-19 * @copyright Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 * - * @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, modern, and easy-to-use C++17/C++20/C++23 thread pool library. This program tests all aspects of the library, but is not needed in order to use the library. */ -#include // std::min, std::shuffle, std::sort, std::unique -#include // std::atomic -#include // std::chrono -#include // std::abs, std::cos, std::exp, std::llround, std::log, std::round, std::sqrt -#include // std::size_t -#include // std::int_fast64_t -#include // std::exit, std::quick_exit -#include // std::localtime, std::strftime, std::time, std::time_t, std::tm -#include // std::ofstream -#include // std::function -#include // std::future -#include // std::setprecision, std::setw -#include // std::fixed -#include // std::numeric_limits -#include // std::mutex, std::scoped_lock -#include // std::mt19937_64, std::random_device, std::uniform_int_distribution -#include // std::ostringstream -#include // std::string, std::to_string -#include // std::string_view -#include // std::this_thread, std::thread -#include // std::ignore -#include // std::as_const, std::forward, std::move, std::pair -#include // std::vector - -// By default, the test program enables all the optional features by defining the suitable macros, so it can test them. However, if the macro `BS_THREAD_POOL_LIGHT_TEST` is defined during compilation, the optional features will not be tested, and in addition, exception handling will be disabled. -#ifndef BS_THREAD_POOL_LIGHT_TEST - #define BS_THREAD_POOL_ENABLE_NATIVE_HANDLES - #define BS_THREAD_POOL_ENABLE_PAUSE - #define BS_THREAD_POOL_ENABLE_PRIORITY - #define BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK -#else - #define BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING -#endif - -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING - #include // std::runtime_error - #if defined(__APPLE__) - #include // std::terminate +// We need to include since if we're using `import std` it will not define any feature-test macros, including `__cpp_lib_modules`, which we need to check if `import std` is supported in the first place. +#ifdef __has_include + #if __has_include() + #include // NOLINT(misc-include-cleaner) #endif #endif -// Include the header files for the thread pool library and its utilities. -#include "BS_thread_pool.hpp" -#include "BS_thread_pool_utils.hpp" +// If the macro `BS_THREAD_POOL_IMPORT_STD` is defined, import the C++ Standard Library as a module. Otherwise, include the relevant Standard Library header files. This is currently only officially supported by MSVC with Microsoft STL and LLVM Clang (NOT Apple Clang) with LLVM libc++. It is not supported by GCC with any standard library, or any compiler with GNU libstdc++. We also check that the feature is enabled by checking `__cpp_lib_modules`. However, MSVC defines this macro even in C++20 mode, which is not standards-compliant, so we check that we are in C++23 mode; MSVC currently reports `__cplusplus` as `202004L` for C++23 mode, so we use that value. +#if defined(BS_THREAD_POOL_IMPORT_STD) && defined(__cpp_lib_modules) && (__cplusplus >= 202004L) && (defined(_MSC_VER) || (defined(__clang__) && defined(_LIBCPP_VERSION) && !defined(__apple_build_version__))) +import std; +constexpr bool using_import_std = true; +#else + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include -// 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 1 -#define BS_THREAD_POOL_TEST_VERSION_PATCH 0 - -#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. + #ifdef __cpp_exceptions + #include + #include + #endif + #ifdef __cpp_lib_format + #include + #endif +constexpr bool using_import_std = false; #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. +// If the macro `BS_THREAD_POOL_TEST_IMPORT_MODULE` is defined, import the thread pool library as a module. Otherwise, include the header file. We also check that we are in C++20 or later. We can't use `__cpp_modules` to check if modules are supported, because Clang does not define it even in C++20 mode; its support for C++20 modules is only partial, but it does seem to work for this particular library. +#define BS_THREAD_POOL_TEST_VERSION 5, 0, 0 +#if defined(BS_THREAD_POOL_TEST_IMPORT_MODULE) && (__cplusplus >= 202002L) +import BS.thread_pool; +static_assert(BS::thread_pool_module, "The flag BS::thread_pool_module is set to false, but the library was imported as a module. Aborting compilation."); +static_assert(BS::thread_pool_version == BS::version(BS_THREAD_POOL_TEST_VERSION), "The versions of BS_thread_pool_test.cpp and the BS.thread_pool module do not match. Aborting compilation."); +#else + #include "BS_thread_pool.hpp" +static_assert(!BS::thread_pool_module, "The flag BS::thread_pool_module is set to true, but the library was not imported as a module. Aborting compilation."); +static_assert(BS::thread_pool_version == BS::version(BS_THREAD_POOL_TEST_VERSION), "The versions of BS_thread_pool_test.cpp and BS_thread_pool.hpp do not match. Aborting compilation."); #endif -using int64 = std::int_fast64_t; -using std::size_t; +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS +static_assert(BS::thread_pool_native_extensions, "Cannot test the native extensions, as the thread pool module was compiled without enabling them using the macro BS_THREAD_POOL_NATIVE_EXTENSIONS. Aborting compilation."); +#endif -// ================ -// Global variables -// ================ - -// Whether to perform the benchmarks. -bool enable_benchmarks = true; - -// Whether to output to a log file in addition to the standard output. -bool enable_log = true; - -// Whether to perform the long deadlock tests. Defaults to false since they can take much longer than the other tests. -bool enable_long_deadlock_tests = false; - -// Whether to perform the tests. -bool enable_tests = true; - -// A global synced stream which prints to the standard output. -BS::synced_stream sync_cout; - -// A global stream object used to access the log file. -std::ofstream log_file; - -// A global synced stream which prints to the log file. -BS::synced_stream sync_file(log_file); - -// A global variable to measure how many checks succeeded. -size_t tests_succeeded = 0; - -// A global variable to measure how many checks failed. -size_t tests_failed = 0; +// A global synced stream which prints to the standard output and/or the log file. +BS::synced_stream sync_out; // ====================== // Functions for printing // ====================== -/** - * @brief Print any number of items into the standard output and the log file, syncing both independently. - * - * @tparam T The types of the items. - * @param items The items to print. - */ -template -void dual_print(T&&... items) -{ - sync_cout.print(std::forward(items)...); - if (enable_log) - sync_file.print(std::forward(items)...); -} - -/** - * @brief Print any number of items into both std::cout and the log file, syncing both independently. Also prints a newline character, and flushes the stream. - * - * @tparam T The types of the items. - * @param items The items to print. - */ -template -void dual_println(T&&... items) -{ - dual_print(std::forward(items)..., BS::synced_stream::endl); -} - /** * @brief Print a stylized header. * @@ -138,16 +100,25 @@ void dual_println(T&&... items) */ void print_header(const std::string_view text, const char symbol = '=') { - dual_println(); - dual_println(std::string(text.length(), symbol)); - dual_println(text); - dual_println(std::string(text.length(), symbol)); + sync_out.println(BS::synced_stream::flush); + sync_out.println(std::string(text.length(), symbol)); + sync_out.println(text); + sync_out.println(std::string(text.length(), symbol)); } // ================================= // Functions for checking conditions // ================================= +/** + * @brief A struct to keep count of the number of tests that succeeded and failed. + */ +struct test_results +{ + inline static std::size_t tests_succeeded = 0; + inline static std::size_t tests_failed = 0; +}; + /** * @brief Check if a condition is met, report the result, and keep count of the total number of successes and failures. * @@ -157,13 +128,13 @@ void check(const bool condition) { if (condition) { - dual_println("-> PASSED!"); - ++tests_succeeded; + sync_out.println("-> passed."); + ++test_results::tests_succeeded; } else { - dual_println("-> FAILED!"); - ++tests_failed; + sync_out.println("-> FAILED!"); + ++test_results::tests_failed; } } @@ -173,19 +144,27 @@ void check(const bool condition) * @param condition The condition to check. */ template -void check(const T1 expected, const T2 obtained) +void check(const T1& expected, const T2& obtained) { - dual_print("Expected: ", expected, ", obtained: ", obtained); - if (expected == obtained) - { - dual_println(" -> PASSED!"); - ++tests_succeeded; - } - else - { - dual_println(" -> FAILED!"); - ++tests_failed; - } + sync_out.print("- Expected: ", expected, ", obtained: ", obtained, ' '); + check(expected == static_cast(obtained)); +} + +/** + * @brief Check if all of the flags in a container are equal to the desired value. + * + * @param flags The container. + * @param value The desired value. + * @return `true` if all flags are equal to the desired value, or `false` otherwise. + */ +template +bool all_flags_equal(const T& flags, const V& value) +{ + return std::all_of(flags.begin(), flags.end(), + [&value](const auto& flag) + { + return flag == value; + }); } /** @@ -197,12 +176,11 @@ void check(const T1 expected, const T2 obtained) template bool all_flags_set(const T& flags) { - for (size_t i = 0; i < flags.size(); ++i) - { - if (!flags[i]) - return false; - } - return true; + return std::all_of(flags.begin(), flags.end(), + [](const bool flag) + { + return flag; + }); } /** @@ -214,12 +192,11 @@ bool all_flags_set(const T& flags) template bool no_flags_set(const T& flags) { - for (size_t i = 0; i < flags.size(); ++i) - { - if (flags[i]) - return false; - } - return true; + return std::none_of(flags.begin(), flags.end(), + [](const bool flag) + { + return flag; + }); } // ======================================= @@ -260,17 +237,16 @@ std::pair random_pair(const T min, const T max) T first = dist(twister); T second; do - { second = dist(twister); - } while (second == first); + while (second == first); if (second < first) return {second, first}; return {first, second}; } -// ======================= -// Miscellaneous functions -// ======================= +// ======================================== +// Functions for detecting various features +// ======================================== /** * @brief Make a string out of items of various types. @@ -280,10 +256,10 @@ std::pair random_pair(const T min, const T max) * @return The generated string. */ template -std::string make_string(T&&... items) +std::string make_string(const T&... items) { - std::ostringstream out; // NOLINT(misc-const-correctness) - (out << ... << std::forward(items)); + std::ostringstream out; + (out << ... << items); return out.str(); } @@ -294,12 +270,59 @@ std::string make_string(T&&... items) */ std::string detect_compiler() { -#if defined(__clang__) +#if defined(__apple_build_version__) + return make_string("Apple Clang v", __clang_major__, '.', __clang_minor__, '.', __clang_patchlevel__); +#elif defined(__clang__) return make_string("Clang v", __clang_major__, '.', __clang_minor__, '.', __clang_patchlevel__); #elif defined(__GNUC__) return make_string("GCC v", __GNUC__, '.', __GNUC_MINOR__, '.', __GNUC_PATCHLEVEL__); -#elif defined(_MSC_VER) - return make_string("MSVC v", _MSC_FULL_VER); +#elif defined(_MSC_FULL_VER) + std::string msvc_full_ver = make_string(_MSC_FULL_VER); + return make_string("MSVC v", msvc_full_ver.substr(0, 2), '.', msvc_full_ver.substr(2, 2), '.', msvc_full_ver.substr(4)); +#else + return "Other"; +#endif +} + +/** + * @brief Detect the C++ standard used to compile this program. + * + * @return A string describing the C++ standard. + */ +std::string detect_cpp_standard() +{ +#if __cplusplus == 201703L + return "C++17"; +#elif __cplusplus == 202002L + return "C++20"; +#elif (__cplusplus == 202302L) || (__cplusplus == 202004L) // MSVC currently uses 202004L for /std:c++latest. + return "C++23"; +#else + return make_string("Other (__cplusplus = ", __cplusplus, ")"); +#endif +} + +/** + * @brief Detect the C++ standard library used to compile this program. + * + * @return A string describing the C++ standard library. + */ +std::string detect_lib() +{ +#if defined(_LIBCPP_VERSION) + return make_string("LLVM libc++ v", _LIBCPP_VERSION / 10000, '.', (_LIBCPP_VERSION / 100) % 100, '.', _LIBCPP_VERSION % 100); // NOLINT(readability-magic-numbers) +#elif defined(_GLIBCXX_RELEASE) + std::string out = make_string("GNU libstdc++ v", _GLIBCXX_RELEASE); + #ifdef __GLIBCXX__ + out += make_string(" (", __GLIBCXX__, ')'); + #endif + return out; +#elif defined(_MSVC_STL_VERSION) + std::string out = make_string("Microsoft STL v", _MSVC_STL_VERSION); + #ifdef _MSVC_STL_UPDATE + out += make_string(" (", _MSVC_STL_UPDATE, ')'); + #endif + return out; #else return "Other"; #endif @@ -324,32 +347,91 @@ std::string detect_os() } /** - * @brief Get a string representing the current time. If MSVC is detected, `localtime_s` will be used to avoid warning C4996. - * - * @return The string. + * @brief Detect available C++ features and print them out. */ -std::string get_time() +void print_features() { - constexpr std::string_view error = "Error obtaining current time!"; - std::string time_string = "YYYY-MM-DD_HH.MM.SS"; - std::tm local_tm = {}; - const std::time_t epoch = std::time(nullptr); -#if defined(_MSC_VER) - if (localtime_s(&local_tm, &epoch) != 0) - { - dual_println(error); - return ""; - } + constexpr int width = 33; + sync_out.print(std::left); + + sync_out.print(std::setw(width), "__cpp_concepts:"); +#ifdef __cpp_concepts + sync_out.println(__cpp_concepts); #else - local_tm = *std::localtime(&epoch); + sync_out.println("N/A"); #endif - const size_t bytes = std::strftime(time_string.data(), time_string.length() + 1, "%Y-%m-%d_%H.%M.%S", &local_tm); - if (bytes != time_string.length()) - { - dual_println(error); - return ""; - } - return time_string; + + sync_out.print(std::setw(width), "__cpp_exceptions:"); +#ifdef __cpp_exceptions + sync_out.println(__cpp_exceptions); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_impl_three_way_comparison:"); +#ifdef __cpp_impl_three_way_comparison + sync_out.println(__cpp_impl_three_way_comparison); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_lib_format:"); +#ifdef __cpp_lib_format + sync_out.println(__cpp_lib_format); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_lib_int_pow2:"); +#ifdef __cpp_lib_int_pow2 + sync_out.println(__cpp_lib_int_pow2); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_lib_jthread:"); +#ifdef __cpp_lib_jthread + sync_out.println(__cpp_lib_jthread); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_lib_modules:"); +#ifdef __cpp_lib_modules + sync_out.println(__cpp_lib_modules); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_lib_move_only_function:"); +#ifdef __cpp_lib_move_only_function + sync_out.println(__cpp_lib_move_only_function); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_lib_semaphore:"); +#ifdef __cpp_lib_semaphore + sync_out.println(__cpp_lib_semaphore); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__cpp_modules:"); +#ifdef __cpp_modules + sync_out.println(__cpp_modules); +#else + sync_out.println("N/A"); +#endif + + sync_out.print(std::setw(width), "__has_include():"); +#if __has_include() + sync_out.println("true"); +#else + sync_out.println("false"); +#endif + + sync_out.println(std::right); } // ========================================= @@ -361,21 +443,21 @@ std::string get_time() * * @param pool The thread pool to check. */ -std::vector obtain_unique_threads(BS::thread_pool& pool) +std::vector obtain_unique_threads(BS::thread_pool<>& pool) { - const BS::concurrency_t num_tasks = pool.get_thread_count() * 2; + const std::size_t num_tasks = pool.get_thread_count() * 2; std::vector thread_ids(num_tasks); - std::atomic total_count = 0; - BS::signaller signal; + std::atomic total_count = 0; + BS::counting_semaphore sem(0); for (std::thread::id& tid : thread_ids) { pool.detach_task( - [&total_count, &tid, &signal, thread_count = pool.get_thread_count()] + [&total_count, &tid, &sem, thread_count = pool.get_thread_count(), num_tasks] { tid = std::this_thread::get_id(); if (++total_count == thread_count) - signal.ready(); - signal.wait(); + sem.release(static_cast(num_tasks)); + sem.acquire(); }); } pool.wait(); @@ -391,12 +473,12 @@ std::vector obtain_unique_threads(BS::thread_pool& pool) void check_constructor() { BS::thread_pool pool; - dual_println("Checking that the thread pool reports a number of threads equal to the hardware concurrency..."); + sync_out.println("Checking that the thread pool reports a number of threads equal to the hardware concurrency..."); check(std::thread::hardware_concurrency(), pool.get_thread_count()); - dual_println("Checking that the manually counted number of unique thread IDs is equal to the reported number of threads..."); + sync_out.println("Checking that the manually counted number of unique thread IDs is equal to the reported number of threads..."); const std::vector unique_threads = obtain_unique_threads(pool); check(pool.get_thread_count(), unique_threads.size()); - dual_println("Checking that the unique thread IDs obtained are the same as those reported by get_thread_ids()..."); + sync_out.println("Checking that the unique thread IDs obtained are the same as those reported by get_thread_ids()..."); std::vector threads_from_pool = pool.get_thread_ids(); std::sort(threads_from_pool.begin(), threads_from_pool.end()); check(threads_from_pool == unique_threads); @@ -408,15 +490,15 @@ void check_constructor() void check_reset() { BS::thread_pool pool; - pool.reset(std::thread::hardware_concurrency() * 2); - dual_println("Checking that after reset() the thread pool reports a number of threads equal to double the hardware concurrency..."); + pool.reset(static_cast(std::thread::hardware_concurrency()) * 2); + sync_out.println("Checking that after reset() the thread pool reports a number of threads equal to double the hardware concurrency..."); check(std::thread::hardware_concurrency() * 2, pool.get_thread_count()); - dual_println("Checking that after reset() the manually counted number of unique thread IDs is equal to the reported number of threads..."); + sync_out.println("Checking that after reset() the manually counted number of unique thread IDs is equal to the reported number of threads..."); check(pool.get_thread_count(), obtain_unique_threads(pool).size()); pool.reset(std::thread::hardware_concurrency()); - dual_println("Checking that after a second reset() the thread pool reports a number of threads equal to the hardware concurrency..."); + sync_out.println("Checking that after a second reset() the thread pool reports a number of threads equal to the hardware concurrency..."); check(std::thread::hardware_concurrency(), pool.get_thread_count()); - dual_println("Checking that after a second reset() the manually counted number of unique thread IDs is equal to the reported number of threads..."); + sync_out.println("Checking that after a second reset() the manually counted number of unique thread IDs is equal to the reported number of threads..."); check(pool.get_thread_count(), obtain_unique_threads(pool).size()); } @@ -425,20 +507,20 @@ void check_reset() // ======================================= /** - * @brief A class to detect when a copy or move constructor has been invoked. + * @brief A class used to detect when a copy or move constructor has been invoked. */ class [[nodiscard]] detect_copy_move { public: detect_copy_move() = default; - detect_copy_move(const detect_copy_move&) : copied(true) {} + detect_copy_move(const detect_copy_move& /*other*/) : copied(true) {} - detect_copy_move(detect_copy_move&&) noexcept : moved(true) {} + detect_copy_move(detect_copy_move&& /*other*/) noexcept : moved(true) {} detect_copy_move& operator=(const detect_copy_move&) = delete; - detect_copy_move& operator=(detect_copy_move&&) = delete; + ~detect_copy_move() = default; [[nodiscard]] bool get_copied() const { @@ -463,10 +545,10 @@ private: void check_task(const std::string_view which_func) { BS::thread_pool pool; - dual_println("Checking that ", which_func, " works for a function with no arguments or return value..."); + sync_out.println("Checking that ", which_func, " works for a function with no arguments or return value..."); { bool flag = false; - const std::function func = [&flag] + const auto func = [&flag] { flag = true; }; @@ -481,10 +563,10 @@ void check_task(const std::string_view which_func) } check(flag); } - dual_println("Checking that ", which_func, " works for a function with one argument and no return value..."); + sync_out.println("Checking that ", which_func, " works for a function with one argument and no return value..."); { bool flag = false; - const std::function func = [](bool& flag_) + const auto func = [](bool& flag_) { flag_ = true; }; @@ -508,11 +590,11 @@ void check_task(const std::string_view which_func) } check(flag); } - dual_println("Checking that ", which_func, " works for a function with two arguments and no return value..."); + sync_out.println("Checking that ", which_func, " works for a function with two arguments and no return value..."); { bool flag1 = false; bool flag2 = false; - const std::function func = [](bool& flag1_, bool& flag2_) + const auto func = [](bool& flag1_, bool& flag2_) { flag1_ = flag2_ = true; }; @@ -538,20 +620,20 @@ void check_task(const std::string_view which_func) } if (which_func == "submit_task()") { - dual_println("Checking that submit_task() works for a function with no arguments and a return value..."); + sync_out.println("Checking that submit_task() works for a function with no arguments and a return value..."); { bool flag = false; - const std::function func = [&flag] + const auto func = [&flag] { return (flag = true); }; std::future flag_future = pool.submit_task(func); check(flag_future.get() && flag); } - dual_println("Checking that submit_task() works for a function with one argument and a return value..."); + sync_out.println("Checking that submit_task() works for a function with one argument and a return value..."); { bool flag = false; - const std::function func = [](bool& flag_) + const auto func = [](bool& flag_) { return (flag_ = true); }; @@ -562,11 +644,11 @@ void check_task(const std::string_view which_func) }); check(flag_future.get() && flag); } - dual_println("Checking that submit_task() works for a function with two arguments and a return value..."); + sync_out.println("Checking that submit_task() works for a function with two arguments and a return value..."); { bool flag1 = false; bool flag2 = false; - const std::function func = [](bool& flag1_, bool& flag2_) + const auto func = [](bool& flag1_, bool& flag2_) { return (flag1_ = flag2_ = true); }; @@ -578,11 +660,11 @@ void check_task(const std::string_view which_func) check(flag_future.get() && flag1 && flag2); } } - dual_println("Checking that ", which_func, " does not create unnecessary copies of the function object..."); + sync_out.println("Checking that ", which_func, " does not create unnecessary copies of the function object..."); { bool copied = false; bool moved = false; - std::function test_copy = [detect = detect_copy_move(), &copied, &moved] + auto test_copy = [detect = detect_copy_move(), &copied, &moved] { copied = detect.get_copied(); moved = detect.get_moved(); @@ -598,12 +680,12 @@ void check_task(const std::string_view which_func) } check(!copied && moved); } - dual_println("Checking that ", which_func, " correctly accepts arguments passed by value, reference, and constant reference..."); + sync_out.println("Checking that ", which_func, " correctly accepts arguments passed by value, reference, and constant reference..."); { { - dual_println("Value:"); - const int64 pass_me_by_value = 0; - const std::function func_value = [](int64 passed_by_value) + sync_out.println("Value:"); + const std::int64_t pass_me_by_value = 0; + const auto func_value = [](std::int64_t passed_by_value) { if (++passed_by_value != 1) static_cast(0); @@ -629,9 +711,9 @@ void check_task(const std::string_view which_func) check(pass_me_by_value == 0); } { - dual_println("Reference:"); - int64 pass_me_by_ref = 0; - const std::function func_ref = [](int64& passed_by_ref) + sync_out.println("Reference:"); + std::int64_t pass_me_by_ref = 0; + const auto func_ref = [](std::int64_t& passed_by_ref) { ++passed_by_ref; }; @@ -656,12 +738,12 @@ void check_task(const std::string_view which_func) check(pass_me_by_ref == 1); } { - dual_println("Constant reference:"); - int64 pass_me_by_cref = 0; - BS::signaller signal; - const std::function func_cref = [&signal](const int64& passed_by_cref) + sync_out.println("Constant reference:"); + std::int64_t pass_me_by_cref = 0; + BS::binary_semaphore sem(0); + const auto func_cref = [&sem](const std::int64_t& passed_by_cref) { - signal.wait(); + sem.acquire(); check(passed_by_cref == 1); }; if (which_func == "detach_task()") @@ -672,7 +754,7 @@ void check_task(const std::string_view which_func) func_cref(pass_me_by_cref); }); ++pass_me_by_cref; - signal.ready(); + sem.release(); pool.wait(); } else @@ -683,7 +765,7 @@ void check_task(const std::string_view which_func) func_cref(pass_me_by_cref); }); ++pass_me_by_cref; - signal.ready(); + sem.release(); future.wait(); } } @@ -696,7 +778,7 @@ void check_task(const std::string_view which_func) class [[nodiscard]] flag_class { public: - explicit flag_class(BS::thread_pool& pool_) : pool(&pool_) {} + explicit flag_class(BS::thread_pool<>& pool_) : pool(&pool_) {} void set_flag_no_args() { @@ -789,7 +871,7 @@ public: private: bool flag = false; - BS::thread_pool* pool; + BS::thread_pool<>* pool; }; // class flag_class /** @@ -798,7 +880,7 @@ private: void check_member_function() { BS::thread_pool pool; - dual_println("Checking that detach_task() works for a member function with no arguments or return value..."); + sync_out.println("Checking that detach_task() works for a member function with no arguments or return value..."); { flag_class flag(pool); pool.detach_task( @@ -809,7 +891,7 @@ void check_member_function() pool.wait(); check(flag.get_flag()); } - dual_println("Checking that detach_task() works for a member function with one argument and no return value..."); + sync_out.println("Checking that detach_task() works for a member function with one argument and no return value..."); { flag_class flag(pool); pool.detach_task( @@ -820,7 +902,7 @@ void check_member_function() pool.wait(); check(flag.get_flag()); } - dual_println("Checking that submit_task() works for a member function with no arguments or return value..."); + sync_out.println("Checking that submit_task() works for a member function with no arguments or return value..."); { flag_class flag(pool); pool.submit_task( @@ -831,7 +913,7 @@ void check_member_function() .wait(); check(flag.get_flag()); } - dual_println("Checking that submit_task() works for a member function with one argument and no return value..."); + sync_out.println("Checking that submit_task() works for a member function with one argument and no return value..."); { flag_class flag(pool); pool.submit_task( @@ -842,7 +924,7 @@ void check_member_function() .wait(); check(flag.get_flag()); } - dual_println("Checking that submit_task() works for a member function with no arguments and a return value..."); + sync_out.println("Checking that submit_task() works for a member function with no arguments and a return value..."); { flag_class flag(pool); std::future flag_future = pool.submit_task( @@ -852,7 +934,7 @@ void check_member_function() }); check(flag_future.get() && flag.get_flag()); } - dual_println("Checking that submit_task() works for a member function with one argument and a return value..."); + sync_out.println("Checking that submit_task() works for a member function with one argument and a return value..."); { flag_class flag(pool); std::future flag_future = pool.submit_task( @@ -870,38 +952,131 @@ void check_member_function() void check_member_function_within_object() { BS::thread_pool pool; - dual_println("Checking that detach_task() works within an object for a member function with no arguments or return value..."); + sync_out.println("Checking that detach_task() works within an object for a member function with no arguments or return value..."); { flag_class flag(pool); flag.detach_test_flag_no_args(); } - dual_println("Checking that detach_task() works within an object for a member function with one argument and no return value..."); + sync_out.println("Checking that detach_task() works within an object for a member function with one argument and no return value..."); { flag_class flag(pool); flag.detach_test_flag_one_arg(); } - dual_println("Checking that submit_task() works within an object for a member function with no arguments or return value..."); + sync_out.println("Checking that submit_task() works within an object for a member function with no arguments or return value..."); { flag_class flag(pool); flag.submit_test_flag_no_args(); } - dual_println("Checking that submit_task() works within an object for a member function with one argument and no return value..."); + sync_out.println("Checking that submit_task() works within an object for a member function with one argument and no return value..."); { flag_class flag(pool); flag.submit_test_flag_one_arg(); } - dual_println("Checking that submit_task() works within an object for a member function with no arguments and a return value..."); + sync_out.println("Checking that submit_task() works within an object for a member function with no arguments and a return value..."); { flag_class flag(pool); flag.submit_test_flag_no_args_return(); } - dual_println("Checking that submit_task() works within an object for a member function with one argument and a return value..."); + sync_out.println("Checking that submit_task() works within an object for a member function with one argument and a return value..."); { flag_class flag(pool); flag.submit_test_flag_one_arg_return(); } } +std::atomic check_callables_flag = false; + +void normal_func() +{ + check_callables_flag = true; +} + +struct functor +{ + void operator()() + { + check_callables_flag = true; + } +}; + +struct has_member_function +{ + static void member_function() + { + check_callables_flag = true; + } +}; + +/** + * @brief Check that different callable types are accepted by the thread pool. + */ +void check_callables() +{ + BS::thread_pool pool; + + sync_out.println("Checking normal function..."); + pool.submit_task(normal_func).wait(); + check(check_callables_flag); + + sync_out.println("Checking function pointer..."); + check_callables_flag = false; + void (*const func_ptr)() = normal_func; + pool.submit_task(func_ptr).wait(); + check(check_callables_flag); + + sync_out.println("Checking pointer to static member function..."); + check_callables_flag = false; + auto member_func_ptr = has_member_function::member_function; + pool.submit_task(member_func_ptr).wait(); + check(check_callables_flag); + + sync_out.println("Checking lambda expression..."); + check_callables_flag = false; + const auto lambda = [] + { + check_callables_flag = true; + }; + pool.submit_task(lambda).wait(); + check(check_callables_flag); + + sync_out.println("Checking std::function..."); + check_callables_flag = false; + const std::function function = [] + { + check_callables_flag = true; + }; + pool.submit_task(function).wait(); + check(check_callables_flag); + +#ifdef __cpp_lib_move_only_function + sync_out.println("Checking std::move_only_function..."); + check_callables_flag = false; + std::move_only_function move_only_function = [] + { + check_callables_flag = true; + }; + pool.submit_task(std::move(move_only_function)).wait(); + check(check_callables_flag); +#else + sync_out.println("Note: std::move_only_function not available, skipping the corresponding test."); +#endif + + sync_out.println("Checking std::bind..."); + check_callables_flag = false; + const auto lambda_for_bind = [](std::atomic& flag) + { + flag = true; + }; + pool.submit_task(std::bind(lambda_for_bind, std::ref(check_callables_flag))).wait(); + check(check_callables_flag); + + sync_out.println("Checking functor..."); + check_callables_flag = false; + const functor functor_instance; + pool.submit_task(functor_instance).wait(); + check(check_callables_flag); +} + // ===================================== // Functions to verify waiting for tasks // ===================================== @@ -913,9 +1088,9 @@ void check_wait() { constexpr std::chrono::milliseconds sleep_time(10); BS::thread_pool pool; - const BS::concurrency_t num_tasks = pool.get_thread_count() * 10; + const std::size_t num_tasks = pool.get_thread_count() * 10; std::vector> flags(num_tasks); - for (BS::concurrency_t i = 0; i < num_tasks; ++i) + for (std::size_t i = 0; i < num_tasks; ++i) { pool.detach_task( [&flags, i, sleep_time] @@ -924,7 +1099,7 @@ void check_wait() flags[i] = true; }); } - dual_println("Waiting for tasks..."); + sync_out.println("Waiting for tasks..."); pool.wait(); check(all_flags_set(flags)); } @@ -935,35 +1110,37 @@ void check_wait() void check_wait_blocks() { constexpr std::chrono::milliseconds sleep_time(100); - constexpr BS::concurrency_t num_waiting_tasks = 4; + constexpr std::size_t num_waiting_tasks = 4; BS::thread_pool pool; - BS::signaller signal; - dual_println("Checking that wait() correctly blocks all external threads that call it..."); + BS::binary_semaphore sem(0); + sync_out.println("Checking that wait() correctly blocks all external threads that call it..."); pool.detach_task( - [&signal] + [&sem] { - dual_println("Task submitted to pool 1 and waiting to be released..."); - signal.wait(); - dual_println("Task released."); + sync_out.println("Task submitted to pool 1 and waiting to be released..."); + sem.acquire(); + sync_out.println("Task released."); }); BS::thread_pool temp_pool(num_waiting_tasks); std::vector> flags(num_waiting_tasks); - const std::function waiting_task = [&flags, &pool](const BS::concurrency_t task_num) + const auto waiting_task = [&flags, &pool](const std::size_t task_num) { - dual_println("Task ", task_num, " submitted to pool 2 and waiting for pool 1's task to finish..."); + sync_out.println("Task ", task_num, " submitted to pool 2 and waiting for pool 1's task to finish..."); pool.wait(); - dual_println("Task ", task_num, " finished waiting."); + sync_out.println("Task ", task_num, " finished waiting."); flags[task_num] = true; }; - for (BS::concurrency_t i = 0; i < num_waiting_tasks; ++i) + for (std::size_t i = 0; i < num_waiting_tasks; ++i) + { temp_pool.detach_task( [&waiting_task, i] { waiting_task(i); }); + } std::this_thread::sleep_for(sleep_time); check(no_flags_set(flags)); - signal.ready(); + sem.release(); temp_pool.wait(); check(all_flags_set(flags)); } @@ -976,7 +1153,7 @@ void check_wait_for() constexpr std::chrono::milliseconds long_sleep_time(250); constexpr std::chrono::milliseconds short_sleep_time(10); BS::thread_pool pool; - dual_println("Checking that wait_for() works..."); + sync_out.println("Checking that wait_for() works..."); std::atomic done = false; pool.detach_task( [&done, long_sleep_time] @@ -984,10 +1161,10 @@ void check_wait_for() std::this_thread::sleep_for(long_sleep_time); done = true; }); - dual_println("Task that lasts ", long_sleep_time.count(), "ms submitted. Waiting for ", short_sleep_time.count(), "ms..."); + sync_out.println("Task that lasts ", long_sleep_time.count(), "ms submitted. Waiting for ", short_sleep_time.count(), "ms..."); pool.wait_for(short_sleep_time); check(!done); - dual_println("Waiting for ", long_sleep_time.count() * 2, "ms..."); + sync_out.println("Waiting for ", long_sleep_time.count() * 2, "ms..."); pool.wait_for(long_sleep_time * 2); check(done); } @@ -1000,7 +1177,7 @@ void check_wait_until() constexpr std::chrono::milliseconds long_sleep_time(250); constexpr std::chrono::milliseconds short_sleep_time(10); BS::thread_pool pool; - dual_println("Checking that wait_until() works..."); + sync_out.println("Checking that wait_until() works..."); std::atomic done = false; pool.detach_task( [&done, long_sleep_time] @@ -1009,10 +1186,10 @@ void check_wait_until() done = true; }); const std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - dual_println("Task that lasts ", long_sleep_time.count(), "ms submitted. Waiting until ", short_sleep_time.count(), "ms from submission time..."); + sync_out.println("Task that lasts ", long_sleep_time.count(), "ms submitted. Waiting until ", short_sleep_time.count(), "ms from submission time..."); pool.wait_until(now + short_sleep_time); check(!done); - dual_println("Waiting until ", long_sleep_time.count() * 2, "ms from submission time..."); + sync_out.println("Waiting until ", long_sleep_time.count() * 2, "ms from submission time..."); pool.wait_until(now + long_sleep_time * 2); check(done); } @@ -1026,16 +1203,16 @@ BS::thread_pool check_wait_multiple_deadlock_pool; void check_wait_multiple_deadlock() { constexpr std::chrono::milliseconds sleep_time(500); - constexpr size_t n_waiting_tasks = 1000; - dual_println("Checking for deadlocks when waiting for tasks..."); + constexpr std::size_t n_waiting_tasks = 1000; + sync_out.println("Checking for deadlocks when waiting for tasks..."); BS::thread_pool pool(1); pool.detach_task( [sleep_time] { std::this_thread::sleep_for(sleep_time); }); - std::atomic count = 0; - for (size_t j = 0; j < n_waiting_tasks; ++j) + std::atomic count = 0; + for (std::size_t j = 0; j < n_waiting_tasks; ++j) { check_wait_multiple_deadlock_pool.detach_task( [&pool, &count] @@ -1047,28 +1224,28 @@ void check_wait_multiple_deadlock() bool passed = false; while (true) { - const size_t old_count = count; + const std::size_t old_count = count; check_wait_multiple_deadlock_pool.wait_for(sleep_time * 2); if (count == n_waiting_tasks) { - dual_println("All waiting tasks successfully finished!"); + sync_out.println("All waiting tasks successfully finished!"); passed = true; break; } if (count == old_count) { - dual_println("Error: deadlock detected!"); + sync_out.println("Error: deadlock detected!"); passed = false; break; } - dual_println(count, " tasks out of ", n_waiting_tasks, " finished waiting..."); + sync_out.println(count, " tasks out of ", n_waiting_tasks, " finished waiting..."); } check(passed); } -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK +#ifdef __cpp_exceptions // An auxiliary thread pool used by check_wait_self_deadlock(). It's a global variable so that the program will not get stuck upon destruction of this pool if a deadlock actually occurs. -BS::thread_pool check_wait_self_deadlock_pool; +BS::wdc_thread_pool check_wait_self_deadlock_pool; /** * @brief Check that calling wait() from within a thread of the same pool throws an exception instead of creating a deadlock. @@ -1076,7 +1253,7 @@ BS::thread_pool check_wait_self_deadlock_pool; void check_wait_self_deadlock() { constexpr std::chrono::milliseconds sleep_time(100); - dual_println("Checking for deadlocks when waiting from within a thread of the same pool..."); + sync_out.println("Checking for deadlocks when waiting from within a thread of the same pool..."); std::atomic passed = false; check_wait_self_deadlock_pool.detach_task( [&passed] @@ -1085,7 +1262,7 @@ void check_wait_self_deadlock() { check_wait_self_deadlock_pool.wait(); } - catch (const BS::thread_pool::wait_deadlock&) + catch (const BS::wait_deadlock&) { passed = true; } @@ -1109,18 +1286,18 @@ void check_wait_self_deadlock() * @param which_func A string naming the function to check. * @return `true` if the check succeeded, `false` otherwise. */ -bool check_loop_no_return(BS::thread_pool& pool, const int64 random_start, const int64 random_end, const BS::concurrency_t num_tasks, const std::string_view which_func) +bool check_loop_no_return(BS::thread_pool<>& pool, const std::int64_t random_start, const std::int64_t random_end, const std::size_t num_tasks, const std::string_view which_func) { - dual_println("Verifying that ", which_func, " from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " modifies all indices exactly once..."); - const size_t num_indices = static_cast(random_end - random_start); - std::vector> flags(num_indices); + sync_out.println("Verifying that ", which_func, " from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " modifies all indices exactly once..."); + const std::size_t num_indices = static_cast(random_end - random_start); + std::vector> flags(num_indices); std::atomic indices_out_of_range = false; - const std::function loop = [&flags, random_start, random_end, &indices_out_of_range](const int64 idx) + const auto loop = [&flags, random_start, random_end, &indices_out_of_range](const std::int64_t idx) { if (idx < random_start || idx > random_end) indices_out_of_range = true; else - ++flags[static_cast(idx - random_start)]; + ++flags[static_cast(idx - random_start)]; }; if (which_func == "detach_loop()") { @@ -1133,15 +1310,10 @@ bool check_loop_no_return(BS::thread_pool& pool, const int64 random_start, const } if (indices_out_of_range) { - dual_println("Error: Loop indices out of range!"); + sync_out.println("Error: Loop indices out of range!"); return false; } - for (size_t i = 0; i < num_indices; ++i) - { - if (flags[i] != 1) - return false; - } - return true; + return all_flags_equal(flags, 1); } /** @@ -1149,80 +1321,80 @@ bool check_loop_no_return(BS::thread_pool& pool, const int64 random_start, const */ void check_loop() { - constexpr int64 range = 1000000; - constexpr size_t repeats = 10; + constexpr std::int64_t range = 1000000; + constexpr std::size_t repeats = 10; BS::thread_pool pool; - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); - check(check_loop_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "detach_loop()")); + const std::pair indices = random_pair(-range, range); + check(check_loop_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "detach_loop()")); } - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); - check(check_loop_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "submit_loop()")); + const std::pair indices = random_pair(-range, range); + check(check_loop_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "submit_loop()")); } - dual_println("Verifying that detach_loop() with identical start and end indices does nothing..."); + sync_out.println("Verifying that detach_loop() with identical start and end indices does nothing..."); { - std::atomic count = 0; - const int64 index = random(-range, range); - dual_println("Range: ", index, " to ", index); + std::atomic count = 0; + const std::int64_t index = random(-range, range); + sync_out.println("Range: ", index, " to ", index); pool.detach_loop(index, index, - [&count](const int64) + [&count](const std::int64_t) { ++count; }); pool.wait(); check(count == 0); } - dual_println("Verifying that submit_loop() with identical start and end indices does nothing..."); + sync_out.println("Verifying that submit_loop() with identical start and end indices does nothing..."); { - std::atomic count = 0; - const int64 index = random(-range, range); - dual_println("Range: ", index, " to ", index); + std::atomic count = 0; + const std::int64_t index = random(-range, range); + sync_out.println("Range: ", index, " to ", index); pool.submit_loop(index, index, - [&count](const int64) + [&count](const std::int64_t) { ++count; }) .wait(); check(count == 0); } - dual_println("Verifying that detach_loop() with end index smaller than the start index does nothing..."); + sync_out.println("Verifying that detach_loop() with end index smaller than the start index does nothing..."); { - std::atomic count = 0; - const std::pair indices = random_pair(-range, range); - dual_println("Range: ", indices.second, " to ", indices.first); + std::atomic count = 0; + const std::pair indices = random_pair(-range, range); + sync_out.println("Range: ", indices.second, " to ", indices.first); pool.detach_loop(indices.second, indices.first, - [&count](const int64) + [&count](const std::int64_t) { ++count; }); pool.wait(); check(count == 0); } - dual_println("Verifying that submit_loop() with end index smaller than the start index does nothing..."); + sync_out.println("Verifying that submit_loop() with end index smaller than the start index does nothing..."); { - std::atomic count = 0; - const std::pair indices = random_pair(-range, range); - dual_println("Range: ", indices.second, " to ", indices.first); + std::atomic count = 0; + const std::pair indices = random_pair(-range, range); + sync_out.println("Range: ", indices.second, " to ", indices.first); pool.submit_loop(indices.second, indices.first, - [&count](const int64) + [&count](const std::int64_t) { ++count; }) .wait(); check(count == 0); } - dual_println("Trying detach_loop() with a number of tasks larger than the number of indices:"); + sync_out.println("Trying detach_loop() with a number of tasks larger than the number of indices:"); { - const int64 start = random(-range, range); - check(check_loop_no_return(pool, start, start + random(0, pool.get_thread_count() * 2), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "detach_loop()")); + const std::int64_t start = random(-range, range); + check(check_loop_no_return(pool, start, start + random(0, static_cast(pool.get_thread_count() * 2)), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "detach_loop()")); } - dual_println("Trying submit_loop() with a number of tasks larger than the number of indices:"); + sync_out.println("Trying submit_loop() with a number of tasks larger than the number of indices:"); { - const int64 start = random(-range, range); - check(check_loop_no_return(pool, start, start + random(0, pool.get_thread_count() * 2), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "submit_loop()")); + const std::int64_t start = random(-range, range); + check(check_loop_no_return(pool, start, start + random(0, static_cast(pool.get_thread_count() * 2)), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "submit_loop()")); } } @@ -1236,13 +1408,13 @@ void check_loop() * @param which_func A string naming the function to check. * @return `true` if the check succeeded, `false` otherwise. */ -bool check_blocks_no_return(BS::thread_pool& pool, const int64 random_start, const int64 random_end, const BS::concurrency_t num_tasks, const std::string_view which_func) +bool check_blocks_no_return(BS::thread_pool<>& pool, const std::int64_t random_start, const std::int64_t random_end, const std::size_t num_tasks, const std::string_view which_func) { - dual_println("Verifying that ", which_func, " from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " modifies all indices exactly once..."); - const size_t num_indices = static_cast(random_end - random_start); - std::vector> flags(num_indices); + sync_out.println("Verifying that ", which_func, " from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " modifies all indices exactly once..."); + const std::size_t num_indices = static_cast(random_end - random_start); + std::vector> flags(num_indices); std::atomic indices_out_of_range = false; - const std::function loop = [&flags, random_start, random_end, &indices_out_of_range](const int64 start, const int64 end) + const auto loop = [&flags, random_start, random_end, &indices_out_of_range](const std::int64_t start, const std::int64_t end) { if (start < random_start || end > random_end) { @@ -1250,8 +1422,8 @@ bool check_blocks_no_return(BS::thread_pool& pool, const int64 random_start, con } else { - for (int64 i = start; i < end; ++i) - ++flags[static_cast(i - random_start)]; + for (std::int64_t i = start; i < end; ++i) + ++flags[static_cast(i - random_start)]; } }; if (which_func == "detach_blocks()") @@ -1265,15 +1437,10 @@ bool check_blocks_no_return(BS::thread_pool& pool, const int64 random_start, con } if (indices_out_of_range) { - dual_println("Error: Loop indices out of range!"); + sync_out.println("Error: Block indices out of range!"); return false; } - for (size_t i = 0; i < num_indices; ++i) - { - if (flags[i] != 1) - return false; - } - return true; + return all_flags_equal(flags, 1); } /** @@ -1284,19 +1451,19 @@ bool check_blocks_no_return(BS::thread_pool& pool, const int64 random_start, con * @param random_end The last index in the loop plus 1. * @param num_tasks The number of tasks. */ -void check_blocks_return(BS::thread_pool& pool, const int64 random_start, const int64 random_end, const BS::concurrency_t num_tasks) +void check_blocks_return(BS::thread_pool<>& pool, const std::int64_t random_start, const std::int64_t random_end, const std::size_t num_tasks) { - dual_println("Verifying that submit_blocks() from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " correctly sums all indices..."); - const std::function loop = [](const int64 start, const int64 end) + sync_out.println("Verifying that submit_blocks() from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " correctly sums all indices..."); + const auto loop = [](const std::int64_t start, const std::int64_t end) { - int64 total = 0; - for (int64 i = start; i < end; ++i) + std::int64_t total = 0; + for (std::int64_t i = start; i < end; ++i) total += i; return total; }; - const std::vector sums_vector = pool.submit_blocks(random_start, random_end, loop, num_tasks).get(); - int64 sum = 0; - for (const int64 partial_sum : sums_vector) + const std::vector sums_vector = pool.submit_blocks(random_start, random_end, loop, num_tasks).get(); + std::int64_t sum = 0; + for (const std::int64_t partial_sum : sums_vector) sum += partial_sum; check(std::abs(random_start - random_end) * (random_start + random_end - 1) / 2, sum); } @@ -1306,85 +1473,85 @@ void check_blocks_return(BS::thread_pool& pool, const int64 random_start, const */ void check_blocks() { - constexpr int64 range = 1000000; - constexpr size_t repeats = 10; + constexpr std::int64_t range = 1000000; + constexpr std::size_t repeats = 10; BS::thread_pool pool; - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); - check(check_blocks_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "detach_blocks()")); + const std::pair indices = random_pair(-range, range); + check(check_blocks_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "detach_blocks()")); } - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); - check(check_blocks_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "submit_blocks()")); + const std::pair indices = random_pair(-range, range); + check(check_blocks_no_return(pool, indices.first, indices.second, random(1, pool.get_thread_count()), "submit_blocks()")); } - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); - check_blocks_return(pool, indices.first, indices.second, random(1, pool.get_thread_count())); + const std::pair indices = random_pair(-range, range); + check_blocks_return(pool, indices.first, indices.second, random(1, pool.get_thread_count())); } - dual_println("Verifying that detach_blocks() with identical start and end indices does nothing..."); + sync_out.println("Verifying that detach_blocks() with identical start and end indices does nothing..."); { - std::atomic count = 0; - const int64 index = random(-range, range); - dual_println("Range: ", index, " to ", index); + std::atomic count = 0; + const std::int64_t index = random(-range, range); + sync_out.println("Range: ", index, " to ", index); pool.detach_blocks(index, index, - [&count](const int64, const int64) + [&count](const std::int64_t, const std::int64_t) { ++count; }); pool.wait(); check(count == 0); } - dual_println("Verifying that submit_blocks() with identical start and end indices does nothing..."); + sync_out.println("Verifying that submit_blocks() with identical start and end indices does nothing..."); { - std::atomic count = 0; - const int64 index = random(-range, range); - dual_println("Range: ", index, " to ", index); + std::atomic count = 0; + const std::int64_t index = random(-range, range); + sync_out.println("Range: ", index, " to ", index); pool.submit_blocks(index, index, - [&count](const int64, const int64) + [&count](const std::int64_t, const std::int64_t) { ++count; }) .wait(); check(count == 0); } - dual_println("Verifying that detach_blocks() with end index smaller than the start index does nothing..."); + sync_out.println("Verifying that detach_blocks() with end index smaller than the start index does nothing..."); { - std::atomic count = 0; - const std::pair indices = random_pair(-range, range); - dual_println("Range: ", indices.second, " to ", indices.first); + std::atomic count = 0; + const std::pair indices = random_pair(-range, range); + sync_out.println("Range: ", indices.second, " to ", indices.first); pool.detach_blocks(indices.second, indices.first, - [&count](const int64, const int64) + [&count](const std::int64_t, const std::int64_t) { ++count; }); pool.wait(); check(count == 0); } - dual_println("Verifying that submit_blocks() with end index smaller than the start index does nothing..."); + sync_out.println("Verifying that submit_blocks() with end index smaller than the start index does nothing..."); { - std::atomic count = 0; - const std::pair indices = random_pair(-range, range); - dual_println("Range: ", indices.second, " to ", indices.first); + std::atomic count = 0; + const std::pair indices = random_pair(-range, range); + sync_out.println("Range: ", indices.second, " to ", indices.first); pool.submit_blocks(indices.second, indices.first, - [&count](const int64, const int64) + [&count](const std::int64_t, const std::int64_t) { ++count; }) .wait(); check(count == 0); } - dual_println("Trying detach_blocks() with a number of tasks larger than the number of indices:"); + sync_out.println("Trying detach_blocks() with a number of tasks larger than the number of indices:"); { - const int64 start = random(-range, range); - check(check_blocks_no_return(pool, start, start + random(0, pool.get_thread_count() * 2), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "detach_blocks()")); + const std::int64_t start = random(-range, range); + check(check_blocks_no_return(pool, start, start + random(0, static_cast(pool.get_thread_count() * 2)), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "detach_blocks()")); } - dual_println("Trying submit_blocks() with a number of tasks larger than the number of indices:"); + sync_out.println("Trying submit_blocks() with a number of tasks larger than the number of indices:"); { - const int64 start = random(-range, range); - check(check_blocks_no_return(pool, start, start + random(0, pool.get_thread_count() * 2), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "submit_blocks()")); + const std::int64_t start = random(-range, range); + check(check_blocks_no_return(pool, start, start + random(0, static_cast(pool.get_thread_count() * 2)), random(pool.get_thread_count() * 2, pool.get_thread_count() * 4), "submit_blocks()")); } } @@ -1401,18 +1568,18 @@ void check_blocks() * @param which_func A string naming the function to check. * @return `true` if the check succeeded, `false` otherwise. */ -bool check_sequence_no_return(BS::thread_pool& pool, const int64 random_start, const int64 random_end, const std::string_view which_func) +bool check_sequence_no_return(BS::thread_pool<>& pool, const std::int64_t random_start, const std::int64_t random_end, const std::string_view which_func) { - dual_println("Verifying that ", which_func, " from ", random_start, " to ", random_end, " modifies all indices exactly once..."); - const size_t num_indices = static_cast(random_end - random_start); - std::vector> flags(num_indices); + sync_out.println("Verifying that ", which_func, " from ", random_start, " to ", random_end, " modifies all indices exactly once..."); + const std::size_t num_indices = static_cast(random_end - random_start); + std::vector> flags(num_indices); std::atomic indices_out_of_range = false; - const std::function sequence = [&flags, random_start, random_end, &indices_out_of_range](const int64 index) + const auto sequence = [&flags, random_start, random_end, &indices_out_of_range](const std::int64_t index) { if (index < random_start || index > random_end) indices_out_of_range = true; else - ++flags[static_cast(index - random_start)]; + ++flags[static_cast(index - random_start)]; }; if (which_func == "detach_sequence()") { @@ -1425,15 +1592,10 @@ bool check_sequence_no_return(BS::thread_pool& pool, const int64 random_start, c } if (indices_out_of_range) { - dual_println("Error: Sequence indices out of range!"); + sync_out.println("Error: Sequence indices out of range!"); return false; } - for (size_t i = 0; i < num_indices; ++i) - { - if (flags[i] != 1) - return false; - } - return true; + return all_flags_equal(flags, 1); } /** @@ -1443,19 +1605,19 @@ bool check_sequence_no_return(BS::thread_pool& pool, const int64 random_start, c * @param random_start The first index in the sequence. * @param random_end The last index in the sequence plus 1. */ -void check_sequence_return(BS::thread_pool& pool, const int64 random_start, const int64 random_end) +void check_sequence_return(BS::thread_pool<>& pool, const std::int64_t random_start, const std::int64_t random_end) { - dual_println("Verifying that submit_sequence() from ", random_start, " to ", random_end, " correctly sums all squares of indices..."); - const std::function sequence = [](const int64 index) + sync_out.println("Verifying that submit_sequence() from ", random_start, " to ", random_end, " correctly sums all squares of indices..."); + const auto sequence = [](const std::int64_t index) { return index * index; }; - const std::vector sums_vector = pool.submit_sequence(random_start, random_end, sequence).get(); - int64 sum = 0; - for (const int64 partial_sum : sums_vector) + const std::vector sums_vector = pool.submit_sequence(random_start, random_end, sequence).get(); + std::int64_t sum = 0; + for (const std::int64_t partial_sum : sums_vector) sum += partial_sum; - int64 correct_sum = 0; - for (int64 i = random_start; i < random_end; i++) + std::int64_t correct_sum = 0; + for (std::int64_t i = random_start; i < random_end; i++) correct_sum += i * i; check(correct_sum, sum); } @@ -1465,70 +1627,70 @@ void check_sequence_return(BS::thread_pool& pool, const int64 random_start, cons */ void check_sequence() { - constexpr int64 range = 1000; - constexpr size_t repeats = 10; + constexpr std::int64_t range = 1000; + constexpr std::size_t repeats = 10; BS::thread_pool pool; - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); + const std::pair indices = random_pair(-range, range); check(check_sequence_no_return(pool, indices.first, indices.second, "detach_sequence()")); } - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); + const std::pair indices = random_pair(-range, range); check(check_sequence_no_return(pool, indices.first, indices.second, "submit_sequence()")); } - for (size_t i = 0; i < repeats; ++i) + for (std::size_t i = 0; i < repeats; ++i) { - const std::pair indices = random_pair(-range, range); + const std::pair indices = random_pair(-range, range); check_sequence_return(pool, indices.first, indices.second); } - dual_println("Verifying that detach_sequence() with identical start and end indices does nothing..."); + sync_out.println("Verifying that detach_sequence() with identical start and end indices does nothing..."); { - std::atomic count = 0; - const int64 index = random(-range, range); - dual_println("Range: ", index, " to ", index); + std::atomic count = 0; + const std::int64_t index = random(-range, range); + sync_out.println("Range: ", index, " to ", index); pool.detach_sequence(index, index, - [&count](const int64) + [&count](const std::int64_t) { ++count; }); pool.wait(); check(count == 0); } - dual_println("Verifying that submit_sequence() with identical start and end indices does nothing..."); + sync_out.println("Verifying that submit_sequence() with identical start and end indices does nothing..."); { - std::atomic count = 0; - const int64 index = random(-range, range); - dual_println("Range: ", index, " to ", index); + std::atomic count = 0; + const std::int64_t index = random(-range, range); + sync_out.println("Range: ", index, " to ", index); pool.submit_sequence(index, index, - [&count](const int64) + [&count](const std::int64_t) { ++count; }) .wait(); check(count == 0); } - dual_println("Verifying that detach_sequence() with end index smaller than the start index does nothing..."); + sync_out.println("Verifying that detach_sequence() with end index smaller than the start index does nothing..."); { - std::atomic count = 0; - const std::pair indices = random_pair(-range, range); - dual_println("Range: ", indices.second, " to ", indices.first); + std::atomic count = 0; + const std::pair indices = random_pair(-range, range); + sync_out.println("Range: ", indices.second, " to ", indices.first); pool.detach_sequence(indices.second, indices.first, - [&count](const int64) + [&count](const std::int64_t) { ++count; }); pool.wait(); check(count == 0); } - dual_println("Verifying that submit_sequence() with end index smaller than the start index does nothing..."); + sync_out.println("Verifying that submit_sequence() with end index smaller than the start index does nothing..."); { - std::atomic count = 0; - const std::pair indices = random_pair(-range, range); - dual_println("Range: ", indices.second, " to ", indices.first); + std::atomic count = 0; + const std::pair indices = random_pair(-range, range); + sync_out.println("Range: ", indices.second, " to ", indices.first); pool.submit_sequence(indices.second, indices.first, - [&count](const int64) + [&count](const std::int64_t) { ++count; }) @@ -1547,81 +1709,73 @@ void check_sequence() void check_task_monitoring() { constexpr std::chrono::milliseconds sleep_time(300); - const size_t num_threads = std::min(std::thread::hardware_concurrency(), 4); - dual_println("Creating pool with ", num_threads, " threads."); - BS::thread_pool pool(static_cast(num_threads)); - dual_println("Submitting ", num_threads * 3, " tasks."); - std::vector signals(num_threads * 3); - for (size_t i = 0; i < num_threads * 3; ++i) + const std::size_t num_threads = std::min(std::thread::hardware_concurrency(), 4); + sync_out.println("Creating pool with ", num_threads, " threads."); + BS::thread_pool pool(num_threads); + sync_out.println("Submitting ", num_threads * 3, " tasks."); + BS::counting_semaphore sem(0); + for (std::size_t i = 0; i < num_threads * 3; ++i) + { pool.detach_task( - [i, &signal = signals[i]] + [i, &sem] { - signal.wait(); - dual_println("Task ", i, " released."); - } -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - , - static_cast(-static_cast(i)) -#endif - ); + sem.acquire(); + sync_out.println("Task ", i, " released."); + }); + } std::this_thread::sleep_for(sleep_time); - dual_println("After submission, should have: ", num_threads * 3, " tasks total, ", num_threads, " tasks running, ", num_threads * 2, " tasks queued..."); - dual_print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); + sync_out.println("After submission, should have: ", num_threads * 3, " tasks total, ", num_threads, " tasks running, ", num_threads * 2, " tasks queued..."); + sync_out.print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); check(pool.get_tasks_total() == num_threads * 3 && pool.get_tasks_running() == num_threads && pool.get_tasks_queued() == num_threads * 2); - for (size_t i = 0; i < num_threads; ++i) - signals[i].ready(); + sem.release(static_cast(num_threads)); std::this_thread::sleep_for(sleep_time); - dual_println("After releasing ", num_threads, " tasks, should have: ", num_threads * 2, " tasks total, ", num_threads, " tasks running, ", num_threads, " tasks queued..."); - dual_print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); + sync_out.println("After releasing ", num_threads, " tasks, should have: ", num_threads * 2, " tasks total, ", num_threads, " tasks running, ", num_threads, " tasks queued..."); + sync_out.print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); check(pool.get_tasks_total() == num_threads * 2 && pool.get_tasks_running() == num_threads && pool.get_tasks_queued() == num_threads); - for (size_t i = num_threads; i < num_threads * 2; ++i) - signals[i].ready(); + sem.release(static_cast(num_threads)); std::this_thread::sleep_for(sleep_time); - dual_println("After releasing ", num_threads, " more tasks, should have: ", num_threads, " tasks total, ", num_threads, " tasks running, ", 0, " tasks queued..."); - dual_print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); + sync_out.println("After releasing ", num_threads, " more tasks, should have: ", num_threads, " tasks total, ", num_threads, " tasks running, ", 0, " tasks queued..."); + sync_out.print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); check(pool.get_tasks_total() == num_threads && pool.get_tasks_running() == num_threads && pool.get_tasks_queued() == 0); - for (size_t i = num_threads * 2; i < num_threads * 3; ++i) - signals[i].ready(); + sem.release(static_cast(num_threads)); std::this_thread::sleep_for(sleep_time); - dual_println("After releasing the final ", num_threads, " tasks, should have: ", 0, " tasks total, ", 0, " tasks running, ", 0, " tasks queued..."); - dual_print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); + sync_out.println("After releasing the final ", num_threads, " tasks, should have: ", 0, " tasks total, ", 0, " tasks running, ", 0, " tasks queued..."); + sync_out.print("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued "); check(pool.get_tasks_total() == 0 && pool.get_tasks_running() == 0 && pool.get_tasks_queued() == 0); } -#ifdef BS_THREAD_POOL_ENABLE_PAUSE /** * @brief Check that pausing works. */ void check_pausing() { constexpr std::chrono::milliseconds sleep_time(200); - BS::thread_pool pool; - dual_println("Checking that the pool correctly reports that it is not paused after construction..."); + BS::pause_thread_pool pool; + sync_out.println("Checking that the pool correctly reports that it is not paused after construction..."); check(!pool.is_paused()); - dual_println("Pausing pool."); + sync_out.println("Pausing pool."); pool.pause(); - dual_println("Checking that the pool correctly reports that it is paused..."); + sync_out.println("Checking that the pool correctly reports that it is paused..."); check(pool.is_paused()); - dual_println("Submitting task and waiting."); + sync_out.println("Submitting task and waiting."); std::atomic flag = false; pool.detach_task( [&flag] { flag = true; - dual_println("Task executed."); + sync_out.println("Task executed."); }); std::this_thread::sleep_for(sleep_time); - dual_println("Verifying that the task has not been executed..."); + sync_out.println("Verifying that the task has not been executed..."); check(!flag); - dual_println("Unpausing pool and waiting."); + sync_out.println("Unpausing pool and waiting."); pool.unpause(); std::this_thread::sleep_for(sleep_time); - dual_println("Verifying that the task has been executed..."); + sync_out.println("Verifying that the task has been executed..."); check(flag); - dual_println("Checking that the pool correctly reports that it is not paused..."); + sync_out.println("Checking that the pool correctly reports that it is not paused..."); check(!pool.is_paused()); } -#endif /** * @brief Check that purge() works. @@ -1630,28 +1784,30 @@ void check_purge() { constexpr std::chrono::milliseconds long_sleep_time(200); constexpr std::chrono::milliseconds short_sleep_time(100); - constexpr size_t num_tasks = 10; + constexpr std::size_t num_tasks = 10; BS::thread_pool pool(1); - dual_println("Submitting ", num_tasks, " tasks to the pool."); + sync_out.println("Submitting ", num_tasks, " tasks to the pool."); std::vector> flags(num_tasks); - for (size_t i = 0; i < num_tasks; ++i) + for (std::size_t i = 0; i < num_tasks; ++i) + { pool.detach_task( [&flags, i, long_sleep_time] { std::this_thread::sleep_for(long_sleep_time); - dual_println("Task ", i, " done."); + sync_out.println("Task ", i, " done."); flags[i] = true; }); + } std::this_thread::sleep_for(short_sleep_time); - dual_println("Purging the pool and waiting for tasks..."); + sync_out.println("Purging the pool and waiting for tasks..."); pool.purge(); pool.wait(); - dual_println("Checking that only the first task was executed..."); + sync_out.println("Checking that only the first task was executed..."); flags[0] = !flags[0]; check(no_flags_set(flags)); } -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING +#ifdef __cpp_exceptions // ====================================== // Functions to verify exception handling // ====================================== @@ -1661,7 +1817,7 @@ void check_purge() */ struct test_exception : public std::runtime_error { - test_exception() : std::runtime_error("Exception thrown!"){}; + test_exception() : std::runtime_error("Exception thrown!") {}; }; /** @@ -1669,7 +1825,7 @@ struct test_exception : public std::runtime_error */ void throws() { - dual_println("Throwing exception..."); + sync_out.println("Throwing exception..."); throw test_exception(); }; @@ -1679,7 +1835,7 @@ void throws() void check_exceptions_submit() { BS::thread_pool pool; - dual_println("Checking that exceptions are forwarded correctly by submit_task()..."); + sync_out.println("Checking that exceptions are forwarded correctly by submit_task()..."); bool caught = false; std::future future = pool.submit_task(throws); try @@ -1699,7 +1855,7 @@ void check_exceptions_submit() void check_exceptions_multi_future() { BS::thread_pool pool; - dual_println("Checking that exceptions are forwarded correctly by BS::multi_future..."); + sync_out.println("Checking that exceptions are forwarded correctly by BS::multi_future..."); bool caught = false; BS::multi_future future; future.push_back(pool.submit_task(throws)); @@ -1728,31 +1884,31 @@ void check_exceptions_multi_future() * @param num_tasks The number of tasks to split the calculation into. * @return `true` if the single-threaded and multithreaded results are equal, `false` otherwise. */ -bool check_vector_of_size(BS::thread_pool& pool, const size_t vector_size, const BS::concurrency_t num_tasks) +bool check_vector_of_size(BS::thread_pool<>& pool, const std::size_t vector_size, const std::size_t num_tasks) { - constexpr int64 value_range = 1000000; - std::vector vector_1(vector_size); - std::vector vector_2(vector_size); - for (size_t i = 0; i < vector_size; ++i) + constexpr std::int64_t value_range = 1000000; + std::vector vector_1(vector_size); + std::vector vector_2(vector_size); + for (std::size_t i = 0; i < vector_size; ++i) { vector_1[i] = random(-value_range, value_range); vector_2[i] = random(-value_range, value_range); } - dual_println("Adding two vectors with ", vector_size, " elements using ", num_tasks, " tasks..."); - std::vector sum_single(vector_size); - for (size_t i = 0; i < vector_size; ++i) + sync_out.println("Adding two vectors with ", vector_size, " elements using ", num_tasks, " tasks..."); + std::vector sum_single(vector_size); + for (std::size_t i = 0; i < vector_size; ++i) sum_single[i] = vector_1[i] + vector_2[i]; - std::vector sum_multi(vector_size); - pool.submit_blocks( + std::vector sum_multi(vector_size); + pool.submit_blocks( 0, vector_size, - [&sum_multi, &vector_1, &vector_2](const size_t start, const size_t end) + [&sum_multi, &vector_1, &vector_2](const std::size_t start, const std::size_t end) { - for (size_t i = start; i < end; ++i) + for (std::size_t i = start; i < end; ++i) sum_multi[i] = vector_1[i] + vector_2[i]; }, num_tasks) .wait(); - for (size_t i = 0; i < vector_size; ++i) + for (std::size_t i = 0; i < vector_size; ++i) { if (sum_single[i] != sum_multi[i]) return false; @@ -1765,151 +1921,171 @@ bool check_vector_of_size(BS::thread_pool& pool, const size_t vector_size, const */ void check_vectors() { - constexpr size_t size_range = 1000000; - constexpr size_t repeats = 10; + constexpr std::size_t size_range = 1000000; + constexpr std::size_t repeats = 10; BS::thread_pool pool; - for (size_t i = 0; i < repeats; ++i) - check(check_vector_of_size(pool, random(0, size_range), random(1, pool.get_thread_count()))); + for (std::size_t i = 0; i < repeats; ++i) + check(check_vector_of_size(pool, random(0, size_range), random(1, pool.get_thread_count()))); } // ================================= // Functions to verify task priority // ================================= -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY +// Priorities are 8-bit integers, but `std::uniform_int_distribution` needs at least a 16-bit integer. +using rand_priority_t = std::int16_t; + /** * @brief Check that task priority works as expected with all task submission methods. */ void check_priority() { constexpr std::chrono::milliseconds sleep_time(200); - constexpr size_t num_tasks = 10; - // Set the pool to have only 1 thread, so it can only run 1 task at a time. This will ensure the tasks will be executed in priority order. Also initialize the pool with a task that waits until released, to simulate pausing in case it is disabled. - BS::signaller signal; - BS::thread_pool pool(1, - [&signal] - { - signal.wait(); - }); + constexpr std::size_t num_tasks = 10; + // Set the pool to have only 1 thread, so it can only run 1 task at a time. This will ensure the tasks will be executed in priority order. + BS::thread_pool pool(1); + pool.pause(); // Create a shuffled lists of priorities. std::vector priorities; - for (size_t i = 0; i < num_tasks - 1; ++i) - priorities.push_back((i % 2 == 0) ? random(0, BS::pr::highest) : random(BS::pr::lowest, 0)); + priorities.reserve(num_tasks - 1); + for (std::size_t i = 0; i < num_tasks - 1; ++i) + priorities.push_back(static_cast((i % 2 == 0) ? random(0, BS::pr::highest) : random(BS::pr::lowest, 0))); + priorities.push_back(BS::pr::lowest); priorities.push_back(0); + priorities.push_back(BS::pr::highest); std::shuffle(priorities.begin(), priorities.end(), std::mt19937_64(std::random_device()())); // Submit tasks using various methods in random priority order. std::vector execution_order; std::mutex exec_mutex; - std::function execute_task_priority = [&execution_order, &exec_mutex](const BS::priority_t priority) + const auto execute_task_priority = [&execution_order, &exec_mutex](const BS::priority_t priority) { const std::scoped_lock lock(exec_mutex); - dual_println("Task with priority ", priority, " executed."); + sync_out.println("Task with priority ", static_cast(priority), " executed."); execution_order.push_back(priority); }; const std::vector functions = {"detach_task", "submit_task", "detach_sequence", "submit_sequence", "detach_loop", "submit_loop", "detach_blocks", "submit_blocks"}; for (const BS::priority_t priority : priorities) { - const std::string_view func = functions[random(0, functions.size() - 1)]; - dual_println("Launching ", func, "() with priority ", priority, "..."); + const std::string_view func = functions[random(0, functions.size() - 1)]; + sync_out.println("Launching ", func, "() with priority ", static_cast(priority), "..."); if (func == "detach_task") + { pool.detach_task( [priority, &execute_task_priority] { execute_task_priority(priority); }, priority); + } else if (func == "submit_task") + { std::ignore = pool.submit_task( [priority, &execute_task_priority] { execute_task_priority(priority); }, priority); + } else if (func == "detach_sequence") + { pool.detach_sequence( 0, 1, - [priority, &execute_task_priority](int64) + [priority, &execute_task_priority](std::int64_t) { execute_task_priority(priority); }, priority); + } else if (func == "submit_sequence") + { std::ignore = pool.submit_sequence( 0, 1, - [priority, &execute_task_priority](int64) + [priority, &execute_task_priority](std::int64_t) { execute_task_priority(priority); }, priority); + } else if (func == "detach_loop") + { pool.detach_loop( 0, 1, - [priority, &execute_task_priority](int64) + [priority, &execute_task_priority](std::int64_t) { execute_task_priority(priority); }, 0, priority); + } else if (func == "submit_loop") + { std::ignore = pool.submit_loop( 0, 1, - [priority, &execute_task_priority](int64) + [priority, &execute_task_priority](std::int64_t) { execute_task_priority(priority); }, 0, priority); + } else if (func == "detach_blocks") + { pool.detach_blocks( 0, 1, - [priority, &execute_task_priority](int64, int64) + [priority, &execute_task_priority](std::int64_t, std::int64_t) { execute_task_priority(priority); }, 0, priority); + } else if (func == "submit_blocks") + { std::ignore = pool.submit_blocks( 0, 1, - [priority, &execute_task_priority](int64, int64) + [priority, &execute_task_priority](std::int64_t, std::int64_t) { execute_task_priority(priority); }, 0, priority); + } } - // Release the waiting task so the tasks can be executed, then check that they were executed in the correct order. - dual_println("Checking execution order..."); + // Unpause the pool so the tasks can be executed, then check that they were executed in the correct order. + sync_out.println("Checking execution order..."); std::this_thread::sleep_for(sleep_time); - signal.ready(); + pool.unpause(); pool.wait(); std::sort(priorities.rbegin(), priorities.rend()); check(execution_order == priorities); } -#endif -// ========================================================= -// Functions to verify thread initialization and this_thread -// ========================================================= +// ======================================================================= +// Functions to verify thread initialization, cleanup, and BS::this_thread +// ======================================================================= /** * @brief Check that thread initialization functions and get_index() work. */ void check_init() { - dual_println("Comparing thread indices with a function passed to the constructor..."); - std::vector> thread_indices(std::thread::hardware_concurrency()); - BS::thread_pool pool( - [&thread_indices] + sync_out.println("Comparing thread indices reported by get_index() using an initialization function passed to reset():"); + std::vector> thread_indices(std::thread::hardware_concurrency()); + std::atomic correct = true; + BS::thread_pool pool; + pool.reset( + [&thread_indices, &correct](std::size_t idx) { - BS::this_thread::optional_index idx = BS::this_thread::get_index(); - if (idx.has_value()) - thread_indices[idx.value()] = idx.value(); + const std::optional reported_idx = BS::this_thread::get_index(); + if (reported_idx.has_value()) + thread_indices[idx] = reported_idx.value(); else - check(false); + correct = false; }); pool.wait(); - bool correct = true; - for (size_t i = 0; i < thread_indices.size(); ++i) + sync_out.println("Checking that all reported indices have values..."); + check(correct); + correct = true; + for (std::size_t i = 0; i < thread_indices.size(); ++i) { if (thread_indices[i] != i) { @@ -1917,43 +2093,56 @@ void check_init() break; } } + sync_out.println("Checking that all reported indices are correct..."); check(correct); - dual_println("Comparing thread indices with a function passed to reset()..."); - pool.reset( - [&thread_indices] + + sync_out.println("Verifying that the index of the main thread has no value..."); + const std::optional main_idx = BS::this_thread::get_index(); + check(!main_idx.has_value()); + + sync_out.println("Verifying that the index of an independent thread has no value..."); + std::thread test_thread( + [] { - BS::this_thread::optional_index idx = BS::this_thread::get_index(); - if (idx.has_value()) - thread_indices[idx.value()] = std::thread::hardware_concurrency() - idx.value(); - else - check(false); + const std::optional ind_idx = BS::this_thread::get_index(); + check(!ind_idx.has_value()); }); - pool.wait(); - correct = true; - for (size_t i = 0; i < thread_indices.size(); ++i) + test_thread.join(); +} + +/** + * @brief Check that thread cleanup functions work. + */ +void check_cleanup() +{ + sync_out.println("Comparing thread indices reported by get_index() using a cleanup function passed to set_cleanup_func():"); + std::vector> thread_indices(std::thread::hardware_concurrency()); + std::atomic correct = true; { - if (thread_indices[i] != std::thread::hardware_concurrency() - i) + BS::thread_pool pool; + pool.set_cleanup_func( + [&thread_indices, &correct](std::size_t idx) + { + const std::optional reported_idx = BS::this_thread::get_index(); + if (reported_idx.has_value()) + thread_indices[idx] = reported_idx.value(); + else + correct = false; + }); + } + sync_out.println("Checking that all reported indices have values..."); + check(correct); + correct = true; + for (std::size_t i = 0; i < thread_indices.size(); ++i) + { + if (thread_indices[i] != i) { correct = false; break; } } + sync_out.println("Checking that all reported indices are correct..."); check(correct); - { - dual_println("Verifying that the index of the main thread has no value..."); - const BS::this_thread::optional_index idx = BS::this_thread::get_index(); - check(!idx.has_value()); - } - { - dual_println("Verifying that the index of an independent thread has no value..."); - std::thread test_thread( - [] - { - const BS::this_thread::optional_index idx = BS::this_thread::get_index(); - check(!idx.has_value()); - }); - test_thread.join(); - } } /** @@ -1961,14 +2150,14 @@ void check_init() */ void check_get_pool() { - dual_println("Checking that all threads report the correct pool..."); - std::vector> thread_pool_ptrs1(std::thread::hardware_concurrency()); - std::vector> thread_pool_ptrs2(std::thread::hardware_concurrency()); - const std::function>&)> store_pointers = [](std::vector>& ptrs) + sync_out.println("Checking that all threads report the correct pool..."); + std::vector> thread_pool_ptrs1(std::thread::hardware_concurrency()); + std::vector> thread_pool_ptrs2(std::thread::hardware_concurrency()); + const auto store_pointers = [](std::vector>& ptrs) { - BS::this_thread::optional_pool ptr = BS::this_thread::get_pool(); + const auto ptr = BS::this_thread::get_pool(); if (ptr.has_value()) - ptrs[BS::this_thread::get_index().value()] = ptr.value(); + ptrs[*BS::this_thread::get_index()] = *ptr; else check(false); }; @@ -1984,38 +2173,228 @@ void check_get_pool() }); pool1.wait(); pool2.wait(); - const std::function>&, BS::thread_pool&)> check_pointers = [](const std::vector>& ptrs, const BS::thread_pool& pool) + const auto check_pointers = [](const std::vector>& ptrs, const BS::thread_pool<>& pool) { - bool correct = true; - for (const std::atomic& ptr : ptrs) - { - if (ptr != &pool) - { - correct = false; - break; - } - } - check(correct); + check(all_flags_equal(ptrs, (void*)&pool)); }; check_pointers(thread_pool_ptrs1, pool1); check_pointers(thread_pool_ptrs2, pool2); { - dual_println("Verifying that the pool pointer of the main thread has no value..."); - const BS::this_thread::optional_pool ptr = BS::this_thread::get_pool(); + sync_out.println("Verifying that the pool pointer of the main thread has no value..."); + const auto ptr = BS::this_thread::get_pool(); check(!ptr.has_value()); } { - dual_println("Verifying that the pool pointer of an independent thread has no value..."); + sync_out.println("Verifying that the pool pointer of an independent thread has no value..."); std::thread test_thread( [] { - const BS::this_thread::optional_pool ptr = BS::this_thread::get_pool(); + const auto ptr = BS::this_thread::get_pool(); check(!ptr.has_value()); }); test_thread.join(); } } +// ========================================================= +// Functions to verify proper handling of parallelized tasks +// ========================================================= + +/** + * @brief A class used to count how many times the copy and move constructors have been invoked since the creation of the initial object. + */ +class [[nodiscard]] count_copy_move +{ +public: + count_copy_move(std::atomic* copied_, std::atomic* moved_) : copied(copied_), moved(moved_) {} + + count_copy_move(const count_copy_move& other) : copied(other.copied), moved(other.moved) + { + ++(*copied); + } + + count_copy_move(count_copy_move&& other) noexcept : copied(other.copied), moved(other.moved) + { + ++(*moved); + } + + count_copy_move& operator=(const count_copy_move&) = delete; + count_copy_move& operator=(count_copy_move&&) = delete; + ~count_copy_move() = default; + +private: + std::atomic* copied = nullptr; + std::atomic* moved = nullptr; +}; // class count_copy_move + +/** + * @brief Check, for a specific member function which parallelizes loops or sequences of tasks, that the callable object does not get copied in the process. + * + * @param which_func A string naming the function to check. + */ +void check_copy(const std::string_view which_func) +{ + BS::thread_pool pool; + const std::size_t num_tasks = pool.get_thread_count() * 10; + sync_out.println("Checking ", which_func, "..."); + std::atomic copied = 0; + std::atomic moved = 0; + auto task = [detect = count_copy_move(&copied, &moved)](auto&&...) {}; + if (which_func == "detach_blocks()") + pool.detach_blocks(0, num_tasks, std::move(task), num_tasks); + else if (which_func == "detach_loop()") + pool.detach_loop(0, num_tasks, std::move(task)); + else if (which_func == "detach_sequence()") + pool.detach_sequence(0, num_tasks, std::move(task)); + else if (which_func == "submit_blocks()") + std::ignore = pool.submit_blocks(0, num_tasks, std::move(task), num_tasks); + else if (which_func == "submit_loop()") + std::ignore = pool.submit_loop(0, num_tasks, std::move(task)); + else if (which_func == "submit_sequence()") + std::ignore = pool.submit_sequence(0, num_tasks, std::move(task)); + pool.wait(); + sync_out.println("Copy count: "); + check(0, copied.load()); // Note: Move count will be unpredictable if priority is on, so we don't check it. +} + +/** + * @brief Check, for all member functions which parallelize loops or sequences of tasks, that the callable object does not get copied in the process. + */ +void check_copy_all() +{ + check_copy("detach_blocks()"); + check_copy("detach_loop()"); + check_copy("detach_sequence()"); + check_copy("submit_blocks()"); + check_copy("submit_loop()"); + check_copy("submit_sequence()"); +} + +/** + * @brief A class used to detect if an object was destructed prematurely. + */ +class detect_destruct +{ +public: + explicit detect_destruct(std::atomic* object_exists_) : object_exists(object_exists_) + { + *object_exists = true; + }; + + detect_destruct(const detect_destruct&) = delete; + detect_destruct(detect_destruct&&) noexcept = delete; + detect_destruct& operator=(const detect_destruct&) = delete; + detect_destruct& operator=(detect_destruct&&) = delete; + + ~detect_destruct() + { + *object_exists = false; + }; + +private: + std::atomic* object_exists = nullptr; +}; + +/** + * @brief Check, for a specific member function which parallelizes loops or sequences of tasks, that if a task that captures a shared pointer is submitted, the pointer is correctly shared between all the iterations of the task. + * + * @param which_func A string naming the function to check. + */ +void check_shared_ptr(const std::string_view which_func) +{ + BS::thread_pool pool; + constexpr std::chrono::milliseconds sleep_time(10); + const std::size_t num_tasks = pool.get_thread_count() * 10; + std::atomic object_exists = false; + std::atomic uses_before_destruct = 0; + std::atomic uses_after_destruct = 0; + sync_out.println("Checking ", which_func, "..."); + { + std::shared_ptr ptr = std::make_shared(&object_exists); + auto task = [ptr, &object_exists, &uses_before_destruct, &uses_after_destruct, &sleep_time](auto&&...) + { + std::this_thread::sleep_for(sleep_time); + if (object_exists) + ++uses_before_destruct; + else + ++uses_after_destruct; + }; + if (which_func == "detach_blocks()") + pool.detach_blocks(0, num_tasks, std::move(task), num_tasks); + else if (which_func == "detach_loop()") + pool.detach_loop(0, num_tasks, std::move(task)); + else if (which_func == "detach_sequence()") + pool.detach_sequence(0, num_tasks, std::move(task)); + else if (which_func == "submit_blocks()") + std::ignore = pool.submit_blocks(0, num_tasks, std::move(task), num_tasks); + else if (which_func == "submit_loop()") + std::ignore = pool.submit_loop(0, num_tasks, std::move(task)); + else if (which_func == "submit_sequence()") + std::ignore = pool.submit_sequence(0, num_tasks, std::move(task)); + ptr.reset(); + } + pool.wait(); + std::this_thread::sleep_for(sleep_time); + sync_out.println("Uses before destruct:"); + check(num_tasks, uses_before_destruct.load()); + sync_out.println("Uses after destruct:"); + check(0, uses_after_destruct.load()); +} + +/** + * @brief Check, for all member functions which parallelize loops or sequences of tasks, that if a task that captures a shared pointer is submitted, the pointer is correctly shared between all the iterations of the task. + */ +void check_shared_ptr_all() +{ + check_shared_ptr("detach_blocks()"); + check_shared_ptr("detach_loop()"); + check_shared_ptr("detach_sequence()"); + check_shared_ptr("submit_blocks()"); + check_shared_ptr("submit_loop()"); + check_shared_ptr("submit_sequence()"); +} + +/** + * @brief Check that a task is destructed immediately after it executes, and therefore does not artificially extend the lifetime of any captured objects. + */ +void check_task_destruct() +{ + constexpr std::chrono::milliseconds sleep_time(20); + BS::thread_pool pool; + std::atomic object_exists = false; + { + const std::shared_ptr ptr = std::make_shared(&object_exists); + pool.submit_task([ptr] {}).wait(); + } + std::this_thread::sleep_for(sleep_time); + check(!object_exists); +} + +/** + * @brief Check that the type trait `BS::common_index_type` works as expected. + */ +void check_common_index_type() +{ + // NOLINTBEGIN(misc-redundant-expression) + sync_out.println("Checking std::int8_t..."); + check(std::is_same_v, std::int8_t> && std::is_same_v, std::int16_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int16_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::int16_t..."); + check(std::is_same_v, std::int16_t> && std::is_same_v, std::int16_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int16_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::int32_t..."); + check(std::is_same_v, std::int32_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::int64_t..."); + check(std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::uint8_t..."); + check(std::is_same_v, std::int16_t> && std::is_same_v, std::int16_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint8_t> && std::is_same_v, std::uint16_t> && std::is_same_v, std::uint32_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::uint16_t..."); + check(std::is_same_v, std::int32_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int32_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint16_t> && std::is_same_v, std::uint16_t> && std::is_same_v, std::uint32_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::uint32_t..."); + check(std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::int64_t> && std::is_same_v, std::uint32_t> && std::is_same_v, std::uint32_t> && std::is_same_v, std::uint32_t> && std::is_same_v, std::uint64_t>); + sync_out.println("Checking std::uint64_t..."); + check(std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t> && std::is_same_v, std::uint64_t>); + // NOLINTEND(misc-redundant-expression) +} + // ================================ // Functions to check for deadlocks // ================================ @@ -2033,8 +2412,8 @@ template void check_deadlock(const F&& task) { constexpr std::chrono::milliseconds sleep_time(200); - constexpr size_t tries = 10000; - size_t try_n = 0; + constexpr std::size_t tries = 10000; + std::size_t try_n = 0; check_deadlock_pool.detach_task( [&try_n, &task] { @@ -2045,64 +2424,389 @@ void check_deadlock(const F&& task) bool passed = false; while (true) { - const size_t old_try_n = try_n; + const std::size_t old_try_n = try_n; check_deadlock_pool.wait_for(sleep_time); if (try_n == tries) { - dual_println("Successfully finished all tries!"); + sync_out.println("Successfully finished all tries!"); passed = true; break; } if (try_n == old_try_n) { - dual_println("Error: deadlock detected!"); + sync_out.println("Error: deadlock detected!"); passed = false; break; } - dual_println("Finished ", try_n, " tries out of ", tries, "..."); + sync_out.println("Finished ", try_n, " tries out of ", tries, "..."); } check(passed); } -// ========================== -// Functions for benchmarking -// ========================== +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS +// ==================================== +// Functions to check native extensions +// ==================================== + +/** + * @brief A map between pre-defined OS process priorities and their string representations. + */ +const std::map os_process_priority_map = {{BS::os_process_priority::idle, "idle"}, {BS::os_process_priority::below_normal, "below_normal"}, {BS::os_process_priority::normal, "normal"}, {BS::os_process_priority::above_normal, "above_normal"}, {BS::os_process_priority::high, "high"}, {BS::os_process_priority::realtime, "realtime"}}; + +/** + * @brief Get the string representation of an OS process priority. + * + * @param priority A `std::optional` object. + * @return A string containing the name of the priority, or "unknown" if the priority is not recognized, or "N/A" if the optional value is not set. + */ +std::string os_process_priority_name(const std::optional& priority) +{ + if (priority.has_value()) + { + const std::map::const_iterator it = os_process_priority_map.find(*priority); + return (it != os_process_priority_map.end()) ? it->second : "unknown"; + } + return "N/A"; +} + +/** + * @brief A map between pre-defined OS thread priorities and their string representations. + */ +const std::map os_thread_priority_map = {{BS::os_thread_priority::idle, "idle"}, {BS::os_thread_priority::lowest, "lowest"}, {BS::os_thread_priority::below_normal, "below_normal"}, {BS::os_thread_priority::normal, "normal"}, {BS::os_thread_priority::above_normal, "above_normal"}, {BS::os_thread_priority::highest, "highest"}, {BS::os_thread_priority::realtime, "realtime"}}; + +/** + * @brief Get the string representation of an OS thread priority. + * + * @param priority A `std::optional` object. + * @return A string containing the name of the priority, or "unknown" if the priority is not recognized, or "N/A" if the optional value is not set. + */ +std::string os_thread_priority_name(const std::optional& priority) +{ + if (priority.has_value()) + { + const std::map::const_iterator it = os_thread_priority_map.find(*priority); + return (it != os_thread_priority_map.end()) ? it->second : "unknown"; + } + return "N/A"; +} + +/** + * @brief Check if a condition is met, report the result, but do not keep count of the total number of successes and failures, because failure is expected if the test is not run as root. + * + * @param condition The condition to check. + */ +void check_root(const bool condition) +{ + if (condition) + { + sync_out.println("-> passed."); + ++test_results::tests_succeeded; + } + else + { + sync_out.println("-> failed, most likely due to insufficient permissions; ignoring."); + } +} + +/** + * @brief Check if the expected result has been obtained, report the result, but do not keep count of the total number of successes and failures, because failure is expected if the test is not run as root. + * + * @param condition The condition to check. + */ +template +void check_root(const T1& expected, const T2& obtained) +{ + sync_out.print("- Expected: ", expected, ", obtained: ", obtained, ' '); + check_root(expected == static_cast(obtained)); +} + +/** + * @brief Check that getting and setting OS process priorities works. + */ +void check_os_process_priorities() +{ + sync_out.println("Checking OS process priorities..."); + sync_out.println("NOTE: This test must be run as admin/root, otherwise it will fail!"); + // We go over the priorities in reverse order because on Linux, a non-root user can only decrease the priority, so if we start from the lowest priority, all tests will fail except the first one. + const std::vector priorities = {BS::os_process_priority::realtime, BS::os_process_priority::high, BS::os_process_priority::above_normal, BS::os_process_priority::normal, BS::os_process_priority::below_normal, BS::os_process_priority::idle}; + for (BS::os_process_priority priority : priorities) + { + sync_out.print("Setting OS process priority to ", os_process_priority_name(priority), ' '); + // On Windows we should be able to set all the priorities even as non-admin; realtime will "succeed" but actually set the priority to high. On Linux, only root can increase the priority beyond normal. + #ifdef _WIN32 + check(BS::set_os_process_priority(priority)); + #else + if (priority >= BS::os_process_priority::normal) + check(BS::set_os_process_priority(priority)); + else + check_root(BS::set_os_process_priority(priority)); + #endif + const std::optional new_priority = BS::get_os_process_priority(); + sync_out.print("Obtaining new OS process priority "); + check(new_priority.has_value()); + #ifdef _WIN32 + if (priority != BS::os_process_priority::realtime) + check(os_process_priority_name(priority), os_process_priority_name(new_priority)); + else + check_root(os_process_priority_name(priority), os_process_priority_name(new_priority)); + #else + if (priority >= BS::os_process_priority::normal) + check(os_process_priority_name(priority), os_process_priority_name(new_priority)); + else + check_root(os_process_priority_name(priority), os_process_priority_name(new_priority)); + #endif + } + // Set the priority back to normal after the test ends. This will fail on Linux if not root. + sync_out.println("Setting priority back to normal..."); + #ifdef _WIN32 + check(BS::set_os_process_priority(BS::os_process_priority::normal)); + #else + check_root(BS::set_os_process_priority(BS::os_process_priority::normal)); + #endif +} + +/** + * @brief Check that getting and setting OS thread priorities works. + */ +void check_os_thread_priorities() +{ + BS::thread_pool pool; + pool.detach_task( + [] + { + sync_out.println("Checking OS thread priorities for pool threads..."); + #ifdef __linux__ + sync_out.println("NOTE: On Linux, this test must be run as root, otherwise it will fail!"); + #endif + const std::vector priorities = {BS::os_thread_priority::realtime, BS::os_thread_priority::highest, BS::os_thread_priority::above_normal, BS::os_thread_priority::normal, BS::os_thread_priority::below_normal, BS::os_thread_priority::lowest, BS::os_thread_priority::idle}; + for (BS::os_thread_priority priority : priorities) + { + sync_out.print("Setting OS thread priority to ", os_thread_priority_name(priority), ' '); + // On Windows we should be able to set all the priorities even as non-admin, including realtime. On Linux, only root can increase the priority beyond normal. (Also, note that on WSL, even root cannot set the priority to highest or above.) + #ifdef _WIN32 + check(BS::this_thread::set_os_thread_priority(priority)); + #else + if (priority <= BS::os_thread_priority::normal) + check(BS::this_thread::set_os_thread_priority(priority)); + else + check_root(BS::this_thread::set_os_thread_priority(priority)); + #endif + const std::optional new_priority = BS::this_thread::get_os_thread_priority(); + sync_out.print("Obtaining new OS thread priority "); + check(new_priority.has_value()); + #ifdef _WIN32 + check(os_thread_priority_name(priority), os_thread_priority_name(new_priority)); + #else + check_root(os_thread_priority_name(priority), os_thread_priority_name(new_priority)); + #endif + } + // Set the priority back to normal after the test ends. This will fail on Linux/macOS if not running as root. + sync_out.println("Setting priority back to normal..."); + #ifdef _WIN32 + check(BS::this_thread::set_os_thread_priority(BS::os_thread_priority::normal)); + #else + check_root(BS::this_thread::set_os_thread_priority(BS::os_thread_priority::normal)); + #endif + }); +} + +/** + * @brief Check that getting and setting OS thread names works. + */ +void check_os_thread_names() +{ + sync_out.println("Checking OS thread names..."); + const std::string name = "BS_thread_pool"; + sync_out.println("Setting main thread name to \"", name, "\"..."); + check(BS::this_thread::set_os_thread_name(name)); + sync_out.println("Obtaining new OS thread name..."); + std::optional new_name = BS::this_thread::get_os_thread_name(); + if (new_name.has_value()) + { + check(true); + check(name, *new_name); + } + else + { + check(false); + } +} + +/** + * @brief Convert a `std::vector` representing CPU affinity to a string of 0s and 1s. + * + * @param affinity The affinity. + * @return The string. + */ +std::string affinity_to_string(const std::optional>& affinity) +{ + if (affinity.has_value()) + { + const std::size_t num_bits = affinity->size(); + std::string str(num_bits, ' '); + for (std::size_t i = 0; i < num_bits; ++i) + str[num_bits - i - 1] = (*affinity)[i] ? '1' : '0'; + return str; + } + return "N/A"; +} + +/** + * @brief Check that getting and setting OS process affinity works. + */ +void check_os_process_affinity() +{ + sync_out.println("Checking OS process affinity..."); + + sync_out.print("Obtaining initial process affinity "); + const std::optional> initial_affinity = BS::get_os_process_affinity(); + check(initial_affinity.has_value()); + sync_out.println("Initial affinity is: ", affinity_to_string(initial_affinity)); + const std::size_t num_bits = initial_affinity.has_value() ? initial_affinity->size() : std::thread::hardware_concurrency(); + + sync_out.print("Setting affinity to CPU 1 only "); + std::vector cpu_1_in(num_bits, false); + cpu_1_in[0] = true; + check(BS::set_os_process_affinity(cpu_1_in)); + sync_out.print("Obtaining new affinity "); + const std::optional> cpu_1_out = BS::get_os_process_affinity(); + check(cpu_1_out.has_value()); + check(affinity_to_string(cpu_1_in), affinity_to_string(cpu_1_out)); + + sync_out.print("Setting affinity to alternating CPUs "); + std::vector alternating_in(num_bits, false); + for (std::size_t i = 0; i < num_bits; ++i) + alternating_in[i] = (i % 2 == 1); + check(BS::set_os_process_affinity(alternating_in)); + sync_out.print("Obtaining new affinity "); + const std::optional> alternating_out = BS::get_os_process_affinity(); + check(alternating_out.has_value()); + check(affinity_to_string(alternating_in), affinity_to_string(alternating_out)); + + if (initial_affinity.has_value()) + { + sync_out.print("Setting affinity back to initial value "); + check(BS::set_os_process_affinity(*initial_affinity)); + sync_out.print("Obtaining new affinity "); + const std::optional> initial_out = BS::get_os_process_affinity(); + check(initial_out.has_value()); + check(affinity_to_string(initial_affinity), affinity_to_string(initial_out)); + } +} + +/** + * @brief Check that getting and setting OS thread affinity works. + */ +void check_os_thread_affinity() +{ + BS::thread_pool pool; + pool.detach_task( + [] + { + // Since the thread affinity must be a subset of the process affinity, we first set its affinity to all CPUs if it wasn't already. + const std::optional> initial_process_affinity = BS::get_os_process_affinity(); + const std::size_t num_process_bits = initial_process_affinity.has_value() ? initial_process_affinity->size() : std::thread::hardware_concurrency(); + const std::vector all_enabled(num_process_bits, true); + BS::set_os_process_affinity(all_enabled); + + sync_out.println("Checking OS thread affinity for pool threads..."); + + sync_out.print("Obtaining initial thread affinity "); + const std::optional> initial_affinity = BS::this_thread::get_os_thread_affinity(); + check(initial_affinity.has_value()); + sync_out.println("Initial affinity is: ", affinity_to_string(initial_affinity)); + const std::size_t num_bits = initial_affinity.has_value() ? initial_affinity->size() : std::thread::hardware_concurrency(); + + sync_out.print("Setting affinity to CPU 1 only "); + std::vector cpu_1_in(num_bits, false); + cpu_1_in[0] = true; + check(BS::this_thread::set_os_thread_affinity(cpu_1_in)); + sync_out.print("Obtaining new affinity "); + const std::optional> cpu_1_out = BS::this_thread::get_os_thread_affinity(); + check(cpu_1_out.has_value()); + check(affinity_to_string(cpu_1_in), affinity_to_string(cpu_1_out)); + + sync_out.print("Setting affinity to alternating CPUs "); + std::vector alternating_in(num_bits, false); + for (std::size_t i = 0; i < num_bits; ++i) + alternating_in[i] = (i % 2 == 1); + check(BS::this_thread::set_os_thread_affinity(alternating_in)); + sync_out.print("Obtaining new affinity "); + const std::optional> alternating_out = BS::this_thread::get_os_thread_affinity(); + check(alternating_out.has_value()); + check(affinity_to_string(alternating_in), affinity_to_string(alternating_out)); + + if (initial_affinity.has_value()) + { + sync_out.print("Setting affinity back to initial value "); + check(BS::this_thread::set_os_thread_affinity(*initial_affinity)); + sync_out.print("Obtaining new affinity "); + const std::optional> initial_out = BS::this_thread::get_os_thread_affinity(); + check(initial_out.has_value()); + check(affinity_to_string(initial_affinity), affinity_to_string(initial_out)); + } + + if (initial_process_affinity.has_value()) + BS::set_os_process_affinity(*initial_process_affinity); + }); +} + +/** + * @brief Try to set the OS priority of this thread to the highest possible value. Also set the name of the thread for debugging purposes. + */ +void try_os_thread_priority() +{ + if (!BS::this_thread::set_os_thread_priority(BS::os_thread_priority::realtime)) + if (!BS::this_thread::set_os_thread_priority(BS::os_thread_priority::highest)) + BS::this_thread::set_os_thread_priority(BS::os_thread_priority::above_normal); + std::optional idx = BS::this_thread::get_index(); + if (idx.has_value()) + BS::this_thread::set_os_thread_name(make_string("Benchmark #", *idx)); + else + BS::this_thread::set_os_thread_name("Benchmark main"); +} +#endif + +// ======================== +// Functions for benchmarks +// ======================== + +/** + * @brief A struct to store the mean and standard deviation of the results of a test. + */ +struct [[nodiscard]] mean_sd +{ + mean_sd(const double mean_, const double sd_) : mean(mean_), sd(sd_) {} + + double mean = 0; + double sd = 0; +}; /** * @brief Print the timing of a specific test. * - * @param num_tasks The number of tasks. - * @param mean_sd An `std::pair` containing the mean as the first member and standard deviation as the second member. + * @param stats A struct containing the mean and standard deviation. + * @param pixels_per_ms The number of pixels per millisecond. */ -void print_timing(const BS::concurrency_t num_tasks, const std::pair& mean_sd) +void print_timing(const mean_sd& stats, const double pixels_per_ms) { - constexpr int width_tasks = 4; constexpr int width_mean = 6; constexpr int width_sd = 4; - if (num_tasks == 0) - dual_print("Single-threaded"); - else - dual_print("With ", std::setw(width_tasks), num_tasks, " task", (num_tasks > 1) ? "s" : ""); - dual_println(", mean execution time was ", std::setw(width_mean), mean_sd.first, " ms with standard deviation ", std::setw(width_sd), mean_sd.second, " ms."); + constexpr int width_pms = 7; + sync_out.println("-> Mean: ", std::setw(width_mean), stats.mean, " ms, standard deviation: ", std::setw(width_sd), stats.sd, " ms, speed: ", std::setw(width_pms), pixels_per_ms, " pixels/ms."); } /** - * @brief Find the minimum element in a vector. + * @brief Find the index of the minimum element in a vector. * * @tparam T The type of elements in the vector. * @param vec The vector. * @return The index of the smallest element in the vector. */ template -size_t min_element(const std::vector& vec) +std::size_t min_element_index(const std::vector& vec) { - size_t smallest = 0; - for (size_t i = 1; i < vec.size(); ++i) - { - if (vec[i] < vec[smallest]) - smallest = i; - } - return smallest; + return static_cast(std::distance(vec.begin(), std::min_element(vec.begin(), vec.end()))); } /** @@ -2110,230 +2814,574 @@ size_t min_element(const std::vector& vec) * * @param timings A vector of the timings corresponding to different numbers of tasks. */ -void print_speedup(const std::vector& timings, const std::vector& try_tasks) +void print_speedup(const std::vector& timings, const std::vector& try_tasks) { - const size_t min_el = min_element(timings); + const std::size_t min_el = min_element_index(timings); const double max_speedup = std::round((timings[0] / timings[min_el]) * 10) / 10; - const BS::concurrency_t num_tasks = try_tasks[min_el]; - dual_println("Maximum speedup obtained by multithreading vs. single-threading: ", max_speedup, "x, using ", num_tasks, " tasks."); + const std::size_t num_tasks = try_tasks[min_el]; + sync_out.println("Maximum speedup obtained by multithreading vs. single-threading: ", max_speedup, "x, using ", num_tasks, " tasks."); } /** * @brief Calculate the mean and standard deviation of a set of integers. * * @param timings The integers. - * @return An `std::pair` containing the mean as the first member and standard deviation as the second member. + * @return A struct containing the mean and standard deviation. */ -std::pair analyze(const std::vector& timings) +mean_sd analyze(const std::vector& timings) { double mean = 0; - for (size_t i = 0; i < timings.size(); ++i) + for (std::size_t i = 0; i < timings.size(); ++i) mean += static_cast(timings[i]) / static_cast(timings.size()); double variance = 0; - for (size_t i = 0; i < timings.size(); ++i) + for (std::size_t i = 0; i < timings.size(); ++i) variance += (static_cast(timings[i]) - mean) * (static_cast(timings[i]) - mean) / static_cast(timings.size()); return {mean, std::sqrt(variance)}; } /** - * @brief A function to generate vector elements. Chosen arbitrarily to simulate a typical numerical calculation. - * - * @param idx The element index. - * @return The value of the element. + * @brief A class to save the Mandelbrot image in. Note that rows and columns are inverted compared to the usual matrix syntax, so that `image(x, y)` corresponds to the pixel at coordinates (x, y) where x is the horizontal axis (i.e. column number) and y is the vertical axis (i.e. row number). The width is the number of columns and the height is the number of rows. */ -double generate_element(const size_t idx) +template +class [[nodiscard]] image_matrix { - return std::log(std::sqrt(std::exp(std::cos(idx)))); +public: + image_matrix() = default; + + image_matrix(const std::size_t width_, const std::size_t height_) : width(width_), height(height_), pixels(std::make_unique(width_ * height_)) {} + + [[nodiscard]] T& operator()(std::size_t x, std::size_t y) + { + return pixels[(y * width) + x]; + } + + [[nodiscard]] T operator()(std::size_t x, std::size_t y) const + { + return pixels[(y * width) + x]; + } + + [[nodiscard]] T& operator[](std::size_t i) + { + return pixels[i]; + } + + [[nodiscard]] T operator[](std::size_t i) const + { + return pixels[i]; + } + + [[nodiscard]] std::size_t get_height() const + { + return height; + } + + [[nodiscard]] std::size_t get_width() const + { + return width; + } + +private: + std::size_t width = 0; + std::size_t height = 0; + std::unique_ptr pixels = nullptr; +}; // class matrix + +// The maximum number of iterations to try before deciding whether a point is in the Mandelbrot set. +constexpr std::size_t max_iter = 2000; + +/** + * @brief Find the escape time of a point. + * + * @param c The point. + * @return The escape time, that is, the number of iterations before the point escapes the Mandelbrot set, with an additional fractional part to eliminate color banding; or `max_iter` if the point doesn't escape within the maximum number of iterations. + */ +double mandelbrot_escape(const std::complex c) +{ + // Define the escape radius. A point c is considered to have "escaped" the Mandelbrot set if, after fewer than `max_iter` iterations of the formula z = z^2 + c starting at z = 0, we get |z| > r. Since the Mandelbrot set is contained within a closed disk of radius 2, the escape radius must be at least 2. However, with that choice we will see the actual disk in the image, because any point outside the disk (but still in the output image) will automatically have an iteration count of 1. For the region plotted by this program by default, an escape radius of 4 is enough, but a higher radius generally produces smoother color gradients. + constexpr double r = 1024; + std::complex z = c; + std::size_t iter = 1; + while (std::norm(z) <= (r * r) && iter < max_iter) + { + z = z * z + c; + ++iter; + }; + // If the point did not escape within the maximum number of iterations, then it is (most likely) in the Mandelbrot set, and we return the maximum number of iterations as is. + if (iter == max_iter) + return static_cast(max_iter); + // If the point escapes, calculate a continuous value to be used for coloring that points in the image. The iteration count is an integer, which would cause color banding in the final image, as there are large regions with the same iteration count and therefore the same color. We resolve this by adding a fractional part. After the loop ends, z has just escaped the radius r, so we are guaranteed that |r| < |z| < |r^2 + c|. Neglecting c (which we can do if r is large enough), this means log_r(|z|) is in the range [1, 2], and therefore log_2(log_r(|z|)) is in the range [0, 1]. Hence, the quantity log_2(log_r(|z|)) = log_2(log(|z|)/log(r)) = log_2(log(|z|^2)/log(r^2)) provides a fractional part that we can simply add to the integer iteration count to make it continuous and eliminate the banding. We subtract from the iteration count instead of adding to it, because larger values of z have smaller iteration counts. + return static_cast(iter) - std::log2(std::log(std::norm(z)) / std::log(r * r)); } /** - * @brief Benchmark multithreaded performance. + * @brief A helper struct to store the RGB values of a pixel. */ -void check_performance() +struct [[nodiscard]] color { + color() = default; + + template + color(const T r_, const T g_, const T b_) : r(static_cast(r_)), g(static_cast(g_)), b(static_cast(b_)) + { + } + + std::uint8_t r = 0; + std::uint8_t g = 0; + std::uint8_t b = 0; +}; + +/** + * @brief Interpolate between two colors. + * + * @param first The first color. + * @param second The second color. + * @param t The interpolation point, in the range [0, 1] where 0 corresponds to the first color, 1 corresponds to the second color, and any other value combines the two colors. + * @return The interpolated color. + */ +color interpolate_colors(const color& first, const color& second, const double t) +{ + return {first.r + (t * (second.r - first.r)), first.g + (t * (second.g - first.g)), first.b + (t * (second.b - first.b))}; +} + +/** + * @brief Convert the escape time of a point into a color. + * + * @param iterations The fractional number of iterations before the point escapes the Mandelbrot set. + * @return The color. + */ +color iter_to_color(const double iterations) +{ + // Define a nice color pallette for the image. + static const std::vector palette = {{66, 30, 15}, {25, 7, 26}, {9, 1, 47}, {4, 4, 73}, {0, 7, 100}, {12, 44, 138}, {24, 82, 177}, {57, 125, 209}, {134, 181, 229}, {211, 236, 248}, {241, 233, 191}, {248, 201, 95}, {255, 170, 0}, {204, 128, 0}, {153, 87, 0}, {106, 52, 3}}; + + // Points that are in the set (or at least, suspected to be in the set because they did not diverge after the maximum number of iterations) will be black. + if (iterations == max_iter) + return {0, 0, 0}; + + // Get the integer and fractional parts of the number of iterations. + double int_part = 0; + const double frac_part = std::modf(iterations, &int_part); + + // Choose two adjacent colors from the palette based on the integer part. We cycle through the palette, so the same colors will repeat many times (`max_iter` is much larger than the number of colors). + const color color1 = palette[static_cast(int_part) % palette.size()]; + const color color2 = palette[(static_cast(int_part) + 1) % palette.size()]; + // Use the fractional part to interpolate smoothly between the two colors. + return interpolate_colors(color1, color2, frac_part); +} + +/** + * @brief Calculate the colors of a range of pixels in an image, enumerated as a range of indices in a 1-dimensional array containing the flattened matrix in row-major order. + * + * @param image The matrix storing the image. + * @param start The first index to calculate. + * @param end The index after the last index to calculate. + * @param jump How many pixels to jump over each iteration, to allow for splitting the work between different runs of the same test. + * @param offset How many pixels to shift the calculation by. + */ +void calculate_mandelbrot(image_matrix& image, const std::size_t start, const std::size_t end, const std::size_t jump, const std::size_t offset) +{ + // Define the ranges of real and imaginary values to consider for the Mandelbrot set. The aspect ratio should be exactly 1:1 (width:height) to prevent stretching, since the benchmark always outputs square images for simplicity. + constexpr double re_min = -2.01; + constexpr double re_max = 0.51; + constexpr double im_min = -1.26; + constexpr double im_max = 1.26; + + // Get the width and height of the image. + const std::size_t width = image.get_width(); + const std::size_t height = image.get_height(); + + for (std::size_t i = start + offset; i < end; i += jump) + { + // Convert the pixel index to the corresponding x and y coordinates. + const std::size_t x = i % width; + const std::size_t y = i / width; + // Convert the pixel coordinates, integers (x, y) such that x is in [0, width-1] and y is in [0, height-1], to a complex number c such that Re(c) is in [re_min, re_max] and Im(c) is in [im_min, im_max]}. (Note: We also need to invert the y axis because the y value increases downwards in the image, but the imaginary part increases upwards in the complex plane. However, to avoid doing any extra calculations, we do that later when we save the image.) + const std::complex c = {(static_cast(x) / static_cast(width) * (re_max - re_min)) + re_min, (static_cast(y) / static_cast(height) * (im_max - im_min)) + im_min}; + // Calculate the pixel's escape time and convert it to a color. + image[i] = iter_to_color(mandelbrot_escape(c)); + } +} + +// A macro to unpack a 16-bit integer into 2 bytes. +#define UNPACK_2_BYTES(value) static_cast(value), static_cast((value) >> 8) +// A macro to unpack a 32-bit integer into 4 bytes. +#define UNPACK_4_BYTES(value) static_cast(value), static_cast((value) >> 8), static_cast((value) >> 16), static_cast((value) >> 24) + +/** + * @brief Save an image to a BMP file. + * + * @param image The matrix containing the pixels. + * @param filename The output file name. + * @return `true` if the file was saved successfully, `false` otherwise. + */ +void save_bmp(const image_matrix& image, const std::string& filename) +{ + // Create the file. + std::ofstream file(filename, std::ios::binary); + if (!file.is_open()) + { + sync_out.println("Error: Could not create the file ", filename, '.'); + return; + } + + sync_out.print("Saving image to a BMP file: ["); + + // Calculate the size of the BMP file in bytes. + const std::uint32_t width = static_cast(image.get_width()); + const std::uint32_t height = static_cast(image.get_height()); + const std::uint32_t total_pixels = width * height; + constexpr std::uint32_t file_header_size = 14; + constexpr std::uint32_t info_header_size = 40; + constexpr std::uint32_t bytes_per_pixel = 3; + constexpr std::uint32_t bits_per_pixel = bytes_per_pixel * 8; + const std::uint32_t file_size = file_header_size + info_header_size + (bytes_per_pixel * total_pixels); + + // The file header of the BMP file: 2 bytes for the "BM" signature, 4 bytes for the file size, 4 bytes reserved, 4 bytes for the start offset of the pixel array. Note that all integers are stored in little-endian format (least-significant byte first), hence the bit shifts (from the macro UNPACK_4_BYTES). We specify the values explicitly to avoid issues with padding. + const std::uint8_t bmp_file_header[file_header_size] = {'B', 'M', UNPACK_4_BYTES(file_size), UNPACK_4_BYTES(0), UNPACK_4_BYTES(file_header_size + info_header_size)}; + + // The information header of the BMP file: 4 bytes for the header size, 4 bytes for the image width, 4 bytes for the image height, 2 bytes for the number of color planes, 2 bytes for the number of bits per pixel, 4 bytes for the compression method (0 = no compression), 4 bytes for the image size (can be 0 if no compression), 4 bytes for the horizontal pixels per meter, 4 bytes for the vertical pixels per meter, 4 bytes for the number of colors (0 = default), 4 bytes for the number of "important colors" (generally ignored). + const std::uint8_t bmp_info_header[info_header_size] = {UNPACK_4_BYTES(info_header_size), UNPACK_4_BYTES(width), UNPACK_4_BYTES(height), UNPACK_2_BYTES(1), UNPACK_2_BYTES(bits_per_pixel), UNPACK_4_BYTES(0), UNPACK_4_BYTES(0), UNPACK_4_BYTES(0), UNPACK_4_BYTES(0), UNPACK_4_BYTES(0), UNPACK_4_BYTES(0)}; + + // Write the headers. + file.write(reinterpret_cast(bmp_file_header), file_header_size); + file.write(reinterpret_cast(bmp_info_header), info_header_size); + + // Create padding bytes for later use. + const std::uint8_t padding_bytes[3] = {0, 0, 0}; + const std::streamsize num_padding_bytes = (4 - ((width * bytes_per_pixel) % 4)) % 4; + + // Write the pixels. Note that they are stored "bottom-up", starting in the lower left corner, going from left to right and then row by row. However, we need to invert the y axis anyway, because the y value increases downwards in the image, but the imaginary part increases upwards in the complex plane. Therefore, we just use the normal y values when saving the image. + for (std::size_t y = 0; y < height; ++y) + { + for (std::size_t x = 0; x < width; ++x) + { + const color col = image(x, y); + // BMP format stores the colors in BGR order. + file.write(reinterpret_cast(&col.b), 1); + file.write(reinterpret_cast(&col.g), 1); + file.write(reinterpret_cast(&col.r), 1); + } + if (num_padding_bytes != 0) + { + // BMP format requires that each row is a multiple of 4 bytes long, so we add padding if necessary. + file.write(reinterpret_cast(padding_bytes), num_padding_bytes); + } + if (y % (height / 10) == 0) + sync_out.print('.'); + } + + file.close(); + sync_out.println("]\nMandelbrot image saved successfully as ", filename, '.'); +} + +/** + * @brief A utility class to measure execution time for benchmarking purposes. + */ +class [[nodiscard]] timer +{ +public: + /** + * @brief Get the number of milliseconds that have elapsed since the object was constructed or since `start()` was last called, but keep the timer ticking. + * + * @return The number of milliseconds. + */ + [[nodiscard]] std::chrono::milliseconds::rep current_ms() const + { + return (std::chrono::duration_cast(std::chrono::steady_clock::now() - start_time)).count(); + } + + /** + * @brief Start (or restart) measuring time. Note that the timer starts ticking as soon as the object is created, so this is only necessary if we want to restart the clock later. + */ + void start() + { + start_time = std::chrono::steady_clock::now(); + } + + /** + * @brief Stop measuring time and store the elapsed time since the object was constructed or since `start()` was last called. + */ + void stop() + { + elapsed_time = std::chrono::steady_clock::now() - start_time; + } + + /** + * @brief Get the number of milliseconds stored when `stop()` was last called. + * + * @return The number of milliseconds. + */ + [[nodiscard]] std::chrono::milliseconds::rep ms() const + { + return (std::chrono::duration_cast(elapsed_time)).count(); + } + +private: + /** + * @brief The time point when measuring started. + */ + std::chrono::time_point start_time = std::chrono::steady_clock::now(); + + /** + * @brief The duration that has elapsed between `start()` and `stop()`. + */ + std::chrono::duration elapsed_time = std::chrono::duration::zero(); +}; // class timer + +/** + * @brief Benchmark multithreaded performance by calculating the Mandelbrot set. + * + * @param benchmark Whether to perform the full benchmarks. + * @param plot Whether to perform quick benchmarks by just plotting the image once. + * @param save Whether to save the image as a BMP file. + */ +void check_performance(const bool benchmark, const bool plot, const bool save) +{ + print_header("Preparing benchmarks:"); + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS + // Try to give the process the highest possible priority, so that other processes do not interfere with the benchmarks. + if (!BS::set_os_process_priority(BS::os_process_priority::realtime)) + if (!BS::set_os_process_priority(BS::os_process_priority::high)) + BS::set_os_process_priority(BS::os_process_priority::above_normal); + + const std::string process_priority = os_process_priority_name(BS::get_os_process_priority()); + sync_out.println("Process priority set to: ", process_priority, "."); + if (process_priority != "realtime") + sync_out.println("Note: Please run as admin/root to enable a higher process priority."); + + try_os_thread_priority(); + const std::string thread_priority = os_thread_priority_name(BS::this_thread::get_os_thread_priority()); + sync_out.println("Thread priority set to: ", thread_priority, "."); + if (thread_priority != "realtime") + sync_out.println("Note: Please run as admin/root to enable a higher thread priority."); + + // Initialize a thread pool with the default number of threads, and ensure that the threads have the highest possible priority, so that other processes do not interfere with the benchmarks. + BS::thread_pool pool(try_os_thread_priority); +#else + // If native extensions are disabled, just initialize a thread pool with the default number of threads. BS::thread_pool pool; - - // Set the formatting of floating point numbers. - dual_print(std::fixed, std::setprecision(1)); - - // Initialize a timer object to measure execution time. - BS::timer tmr; +#endif // Store the number of available hardware threads for easy access. - const BS::concurrency_t thread_count = pool.get_thread_count(); - dual_println("Using ", thread_count, " threads."); + const std::size_t thread_count = pool.get_thread_count(); + sync_out.println("Using ", thread_count, " threads."); - // The target execution time, in milliseconds, of the multithreaded test with the number of blocks equal to the number of threads. The total time spent on that test will be approximately equal to repeat * target_ms. + // Set the formatting of floating point numbers. + sync_out.print(std::fixed, std::setprecision(1)); + + // Initialize a timer object to measure execution time. + timer tmr; + + // The target execution time, in milliseconds, of the multithreaded test with the number of blocks equal to the number of threads. The total time spent on that test will be approximately equal to `repeat * target_ms`. constexpr std::chrono::milliseconds::rep target_ms = 50; - // Test how many vectors we need to generate, and of what size, to roughly achieve the target execution time. - dual_println("Determining the number of elements to generate in order to achieve an approximate mean execution time of ", target_ms, " ms with ", thread_count, " tasks..."); - size_t vector_size = thread_count; - std::vector vector; - const std::function loop = [&vector](const size_t start, const size_t end) + // Find the Mandelbrot image size that will roughly achieve the target execution time. + sync_out.println("Determining the Mandelbrot image size needed to achieve an approximate mean execution time of ", target_ms, " ms with ", thread_count, " tasks..."); + std::size_t image_size = thread_count; + image_matrix image; + std::size_t jump = 1; + std::size_t offset = 0; + + // Define the loop function. + const auto loop = [&image, &jump, &offset](const std::size_t start, const std::size_t end) { - for (size_t i = start; i < end; ++i) - vector[i] = generate_element(i); + calculate_mandelbrot(image, start, end, jump, offset); }; + + // Increase the image size gradually until the target execution time is reached. do { - vector_size *= 2; - vector = std::vector(vector_size); + image_size *= 2; + image = image_matrix(image_size, image_size); tmr.start(); - pool.detach_blocks(0, vector_size, loop); + pool.detach_blocks(0, image_size * image_size, loop); pool.wait(); tmr.stop(); } while (tmr.ms() < target_ms); - vector_size = thread_count * static_cast(std::llround(static_cast(vector_size) * static_cast(target_ms) / static_cast(tmr.ms()) / thread_count)); - // Define vectors to store statistics. - std::vector different_n_timings; - std::vector same_n_timings; + // Scale the image size to fit the target execution time more precisely, keeping in mind that the time complexity is O(image_size^2). + image_size = static_cast(std::llround(static_cast(image_size) * std::sqrt(static_cast(target_ms) / static_cast(tmr.ms())))); + sync_out.println("Result: ", image_size, 'x', image_size, " pixels."); - // The maximum number of overall tests. - constexpr size_t max_tests = 20; - - // The maximum number, in milliseconds, that a single test can last. - constexpr std::chrono::milliseconds::rep max_time_ms = 5000; - - // The minimum number of repeats for each test. At least this many repeats will be performed even if `max_time_ms` is exceeded. - constexpr size_t min_repeats = 5; - - // The maximum number of times to repeat each run of the test in order to collect reliable statistics. - constexpr size_t max_repeats = 30; - dual_println("Each test will be repeated up to ", max_repeats, " times to collect reliable statistics."); - - // Perform the test. - std::vector try_tasks; - BS::concurrency_t num_tasks = 0; - double last_timing = std::numeric_limits::max(); - dual_println("Generating ", vector_size, " elements:"); - while (try_tasks.size() <= max_tests) + if (benchmark) { - try_tasks.push_back(num_tasks); - dual_print('['); - std::chrono::milliseconds::rep total_time = 0; - for (size_t i = 0; i < max_repeats; ++i) + print_header("Performing full benchmarks:"); + // Define vectors to store statistics. + std::vector different_n_timings; + std::vector same_n_timings; + + // The number of times to repeat each run of the test in order to collect reliable statistics. + constexpr std::size_t num_repeats = 30; + + // Since we are repeating the same test multiple times, we might as well use different parts of the complex plane in each repetition. However, we have to spread the calculations evenly to avoid biasing the results, as some regions have much higher escape times than others. So we calculate the whole image, but at an offset from 0 to `num_repeats`. + jump = num_repeats; + const std::size_t benchmark_image_size = static_cast(std::floor(static_cast(image_size) * std::sqrt(num_repeats))); + sync_out.println("Generating a ", benchmark_image_size, 'x', benchmark_image_size, " plot of the Mandelbrot set..."); + sync_out.println("Each test will be repeated ", num_repeats, " times to collect reliable statistics."); + + // Perform the test. + std::vector try_tasks; + std::size_t num_tasks = 0; + double last_timing = std::numeric_limits::max(); + constexpr int width_tasks = 4; + while (true) { - vector = std::vector(vector_size); - tmr.start(); - if (num_tasks > 0) + image = image_matrix(benchmark_image_size, benchmark_image_size); + try_tasks.push_back(num_tasks); + if (num_tasks == 0) + sync_out.print(std::setw(width_tasks), 1, " task: "); + else + sync_out.print(std::setw(width_tasks), num_tasks, " tasks: "); + sync_out.print('['); + for (std::size_t i = 0; i < num_repeats; ++i) { - pool.detach_blocks(0, vector_size, loop, num_tasks); - pool.wait(); + // Measure execution time for this test. + tmr.start(); + if (num_tasks > 0) + { + pool.detach_blocks(0, benchmark_image_size * benchmark_image_size, loop, num_tasks); + pool.wait(); + } + else + { + loop(0, benchmark_image_size * benchmark_image_size); + } + tmr.stop(); + // Save the measurement for later analysis. + same_n_timings.push_back(tmr.ms()); + // Print a dot to inform the user that we've made progress. + sync_out.print('.'); + // Increase the offset so we calculate a different part of the image in each repetition of the test. + offset = (offset + 1) % num_repeats; + } + sync_out.println(']', (num_tasks == 0) ? " (single-threaded)" : ""); + // Analyze, print, and save the mean and standard deviation of all the tests with the same number of tasks. + const mean_sd stats = analyze(same_n_timings); + const std::chrono::milliseconds::rep total_time = std::reduce(same_n_timings.begin(), same_n_timings.end()); + const double pixels_per_ms = static_cast(benchmark_image_size * benchmark_image_size) / static_cast(total_time); + same_n_timings.clear(); + print_timing(stats, pixels_per_ms); + different_n_timings.push_back(stats.mean); + if (num_tasks == 0) + { + num_tasks = std::max(thread_count / 4, 2); } else { - loop(0, vector_size); + if ((num_tasks > thread_count) && (stats.mean > last_timing)) + break; + last_timing = stats.mean; + num_tasks *= 2; } - tmr.stop(); - same_n_timings.push_back(tmr.ms()); - dual_print('.'); - total_time += tmr.ms(); - if (total_time > max_time_ms && same_n_timings.size() > min_repeats) - break; - } - dual_println(']'); - const std::pair mean_sd = analyze(same_n_timings); - same_n_timings.clear(); - print_timing(num_tasks, mean_sd); - different_n_timings.push_back(mean_sd.first); - if (num_tasks == 0) - { - num_tasks = 2; - } - else - { - if ((num_tasks > thread_count) && (mean_sd.first > last_timing)) - break; - last_timing = mean_sd.first; - num_tasks *= 2; } + print_speedup(different_n_timings, try_tasks); } - print_speedup(different_n_timings, try_tasks); + + if (plot) + { + print_header("Performing quick benchmarks:"); + // Just plot whatever we can in 5 seconds. Feel free to increase this to get higher resolution images. + constexpr std::chrono::milliseconds::rep total_ms = 5000; + const std::size_t plot_image_size = static_cast(std::floor(static_cast(image_size) * std::sqrt(static_cast(total_ms) / static_cast(target_ms)))); + image = image_matrix(plot_image_size, plot_image_size); + sync_out.print("Generating a ", plot_image_size, 'x', plot_image_size, " plot of the Mandelbrot set with ", thread_count, " tasks: ["); + pool.detach_blocks(0, plot_image_size * plot_image_size, + [&image](const std::size_t start, const std::size_t end) + { + calculate_mandelbrot(image, start, end, 1, 0); + sync_out.print('.'); + }); + pool.wait(); + tmr.stop(); + sync_out.println("]\nDone in ", tmr.ms(), " ms (", static_cast(plot_image_size * plot_image_size) / static_cast(tmr.ms()), " pixels/ms)."); + } + + if (save) + save_bmp(image, "BS_thread_pool_benchmark_mandelbrot.bmp"); + + print_header("Thread pool performance test completed!", '+'); } -// =============================================== -// Functions related to the command line interface -// =============================================== +// ================================== +// The main function and related code +// ================================== /** * @brief Show basic information about the program. */ void show_intro() { - dual_println("BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library"); - dual_println("(c) 2024 Barak Shoshany (baraksh@gmail.com) (https://baraksh.com)"); - dual_println("GitHub: https://github.com/bshoshany/thread-pool"); - dual_println(); + sync_out.println(R"( +██████ ███████ ████████ ██ ██ ██████ ███████ █████ ██████ ██████ ██████ ██████ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ███████ ██████ █████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██████ ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██████ ██████ ███████ +)"); - dual_println("Thread pool library version is ", BS_THREAD_POOL_VERSION_MAJOR, '.', BS_THREAD_POOL_VERSION_MINOR, '.', BS_THREAD_POOL_VERSION_PATCH, '.'); - 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(); + sync_out.println("BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17/C++20/C++23 thread pool library"); + sync_out.println("(c) 2024 Barak Shoshany (baraksh@gmail.com) (https://baraksh.com/)"); + sync_out.println("GitHub: https://github.com/bshoshany/thread-pool"); + sync_out.println(); - dual_print("Exception handling is "); -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING - dual_println("enabled."); -#else - dual_println("disabled."); -#endif + sync_out.println("Thread pool library version is v", BS::thread_pool_version, '.'); + sync_out.println("Thread pool library imported using: ", BS::thread_pool_module ? "import BS.thread_pool (" : "#include \"BS_thread_pool.hpp\" (no ", "C++20 modules)."); + sync_out.println(); + sync_out.println("C++ Standard Library imported using:"); + sync_out.println("* Thread pool library: ", BS::thread_pool_import_std ? "import std (" : "#include <...> (no ", "C++23 std module)."); + sync_out.println("* Test program: ", using_import_std ? "import std (" : "#include <...> (no ", "C++23 std module)."); + sync_out.println(); - dual_print("Native handles are "); -#ifdef BS_THREAD_POOL_ENABLE_NATIVE_HANDLES - dual_println("enabled."); -#else - dual_println("disabled."); -#endif + sync_out.println("Native extensions are ", BS::thread_pool_native_extensions ? "enabled" : "disabled", '.'); - dual_print("Pausing is "); -#ifdef BS_THREAD_POOL_ENABLE_PAUSE - dual_println("enabled."); -#else - dual_println("disabled."); -#endif + sync_out.println(); - dual_print("Priority is "); -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - dual_println("enabled."); -#else - dual_println("disabled."); -#endif + sync_out.println("Detected OS: ", detect_os(), '.'); + sync_out.println("Detected compiler: ", detect_compiler(), '.'); + sync_out.println("Detected standard library: ", detect_lib(), '.'); + sync_out.println("Detected C++ standard: ", detect_cpp_standard(), '.'); + sync_out.println("Detected features:"); + print_features(); - dual_print("Wait deadlock checks are "); -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - dual_println("enabled."); -#else - dual_println("disabled."); -#endif - - dual_println(); - - dual_println("Detected OS: ", detect_os(), '.'); - dual_println("Detected compiler: ", detect_compiler(), '.'); - dual_println(); - - dual_println("Important: Please do not run any other applications, especially multithreaded applications, in parallel with this test!"); + sync_out.println("Hardware concurrency is ", std::thread::hardware_concurrency(), '.'); + sync_out.println("Important: Please do not run any other applications, especially multithreaded applications, in parallel with this test!"); } /** - * @brief Show the available command line options. + * @brief Get a string representing the current time. + * + * @return The string. */ -void show_help() +std::string get_time() { - dual_println(); - dual_println("Available options:"); - dual_println(" help Show this help message and exit."); - dual_println(" log Create a log file."); - dual_println(" tests Perform standard tests."); - dual_println(" deadlock Perform long deadlock tests."); - dual_println(" benchmarks Perform benchmarks."); - dual_println("If no options are entered, the default is:"); - dual_println(" ", enable_log ? "log " : "", enable_tests ? "tests " : "", enable_long_deadlock_tests ? "deadlock " : "", enable_benchmarks ? "benchmarks " : ""); +#ifdef __cpp_lib_format + // Things are much easier with C++20 `std::format`. + return std::format("{:%Y-%m-%d_%H.%M.%S}", std::chrono::time_point_cast(std::chrono::system_clock::now())); +#else + std::string time_string = "YYYY-MM-DD_HH.MM.SS"; + std::tm local_tm = {}; + const std::time_t epoch = std::time(nullptr); + #if defined(_MSC_VER) && !defined(__cpp_lib_modules) + // If MSVC is detected, use `localtime_s()` to avoid warning C4996. (This doesn't work if we used `import std`, so we check that to be on the safe side, although in that case `std::format` should be available anyway). + if (localtime_s(&local_tm, &epoch) != 0) + return ""; + #elif defined(__linux__) || defined(__APPLE__) + // On Linux or macOS, use `localtime_r()` to avoid clang-tidy warning `concurrency-mt-unsafe`. + if (localtime_r(&epoch, &local_tm) == nullptr) + return ""; + #else + local_tm = *std::localtime(&epoch); + #endif + const std::size_t bytes = std::strftime(time_string.data(), time_string.length() + 1, "%Y-%m-%d_%H.%M.%S", &local_tm); + if (bytes != time_string.length()) + return ""; + return time_string; +#endif } /** - * @brief A class to parse command line arguments. + * @brief A class to parse command line arguments. All arguments are assumed to be on/off and default to off. */ class [[nodiscard]] arg_parser { @@ -2344,33 +3392,58 @@ public: * @param argc The number of arguments. * @param argv An array containing the arguments. */ - arg_parser(int argc, char* argv[]) : args(argv, argv + argc){}; + arg_parser(int argc, char* argv[]) : args(argv + 1, argv + argc), executable(argv[0]) {}; /** - * @brief Check if a specific command line argument has been passed to the program. + * @brief Check if a specific command line argument has been passed to the program. If no arguments were passed, use the default value instead. * * @param arg The argument to check for. * @return `true` if the argument exists, `false` otherwise. */ - [[nodiscard]] bool exists(const std::string_view arg) const + [[nodiscard]] bool operator[](const std::string_view arg) { - for (const std::string_view str : args) - { - if (str == arg) - return true; - } - return false; + if (size() > 0) + return (args.count(arg) == 1); + return allowed[arg].def; } /** - * @brief Access a command line argument at a specific index. + * @brief Add an argument to the list of allowed arguments. * - * @param index The index to access. - * @return The argument. + * @param arg The argument. + * @param desc The description of the argument. + * @param def The default value of the argument. */ - [[nodiscard]] const std::string_view operator[](const size_t index) const + void add_argument(const std::string_view arg, const std::string_view desc, const bool def) { - return args[index]; + allowed[arg] = {desc, def}; + } + + /** + * @brief Get the name of the executable. + * + * @return The name of the executable. + */ + std::string_view get_executable() + { + return executable; + } + + void show_help() const + { + int width = 1; + for (const auto& [arg, opt] : allowed) + width = std::max(width, static_cast(arg.size())); + sync_out.println("\nAvailable options (all are on/off and default to off):"); + for (const auto& [arg, opt] : allowed) + sync_out.println(" ", std::left, std::setw(width), arg, " ", opt.desc); + sync_out.print("If no options are entered, the default is:\n "); + for (const auto& [arg, opt] : allowed) + { + if (opt.def) + sync_out.print(arg, " "); + } + sync_out.println(); } /** @@ -2378,183 +3451,285 @@ public: * * @return The number of arguments. */ - [[nodiscard]] size_t size() const + [[nodiscard]] std::size_t size() const { return args.size(); } -private: /** - * @brief A vector containing string views of the command line arguments. + * @brief Verify that the command line arguments belong to the list of allowed arguments. + * + * @return `true` if all arguments are allowed, `false` otherwise. */ - std::vector args; -}; // class arg_parser - -// ================= -// The main function -// ================= - -int main(int argc, char* argv[]) -{ - // Parse the command line arguments. - const arg_parser args(argc, argv); - if (args.size() > 1) + [[nodiscard]] bool verify() const { - if (args.exists("help")) - { - show_intro(); - show_help(); - std::exit(0); - } - enable_tests = args.exists("tests"); - enable_long_deadlock_tests = args.exists("deadlock"); - enable_benchmarks = args.exists("benchmarks"); - enable_log = args.exists("log"); - if (!enable_tests && !enable_long_deadlock_tests && !enable_benchmarks) - { - show_intro(); - dual_println("Error: No tests requested! Aborting."); - std::exit(0); - } + return std::all_of(args.begin(), args.end(), + [this](const std::string_view arg) + { + return allowed.count(arg) == 1; + }); } - if (enable_log) +private: + struct arg_spec { - // Extract the name of the executable file, or use a default value if it is not found. - const size_t last_slash = args[0].find_last_of("/\\") + 1; - std::string exe_file(args[0].substr(last_slash, args[0].find('.', last_slash) - last_slash)); - if (exe_file.empty()) - exe_file = "BS_thread_pool_test"; - // Create a log file using the name of the executable, followed by the current date and time. - const std::string log_filename = exe_file + "-" + get_time() + ".log"; - log_file.open(log_filename); - if (log_file.is_open()) + std::string_view desc; + bool def = false; + }; + + /** + * @brief A set containing string views of the command line arguments. + */ + std::set args; + + /** + * @brief A map containing the allowed arguments and their descriptions. + */ + std::map allowed; + + /** + * @brief A string view containing the name of the executable. + */ + std::string_view executable; +}; // class arg_parser + +int main(int argc, char* argv[]) // NOLINT(bugprone-exception-escape) +{ +#ifdef __cpp_exceptions + try + { +#endif + // If the file default_args.txt exists, read the default arguments from it (space separated in a single line). Otherwise, use the built-in defaults. This is useful when debugging. + std::map defaults; + std::ifstream default_args_file("default_args.txt"); + if (default_args_file.is_open()) { - dual_println("Generating log file: ", log_filename, ".\n"); + std::string line; + std::getline(default_args_file, line); + std::istringstream iss(line); + std::string arg; + while (iss >> arg) + defaults[arg] = true; + default_args_file.close(); } else { - enable_log = false; - dual_println("Could not create log file: ", log_filename, ". Proceeding without it.\n"); + defaults = {{"help", false}, {"stdout", true}, {"log", true}, {"tests", true}, {"deadlock", false}, {"benchmarks", true}, {"plot", false}, {"save", false}}; } - } - show_intro(); + // Parse the command line arguments. + arg_parser args(argc, argv); + args.add_argument("help", "Show this help message and exit.", defaults["help"]); + args.add_argument("stdout", "Print to the standard output.", defaults["stdout"]); + args.add_argument("log", "Print to a log file.", defaults["log"]); + args.add_argument("tests", "Perform standard tests.", defaults["tests"]); + args.add_argument("deadlock", "Perform long deadlock tests.", defaults["deadlock"]); + args.add_argument("benchmarks", "Perform full Mandelbrot plot benchmarks.", defaults["benchmarks"]); + args.add_argument("plot", "Perform quick Mandelbrot plot benchmarks.", defaults["plot"]); + args.add_argument("save", "Save the Mandelbrot plot to a file.", defaults["save"]); - if (enable_tests) - { - print_header("Checking the constructor:"); - check_constructor(); - - print_header("Checking reset():"); - check_reset(); - - print_header("Checking detach_task() and submit_task():"); - check_task("detach_task()"); - check_task("submit_task()"); - - print_header("Checking submission of member functions as tasks:"); - check_member_function(); - check_member_function_within_object(); - - print_header("Checking wait(), wait_for(), and wait_until():"); - check_wait(); - check_wait_blocks(); - check_wait_for(); - check_wait_until(); - check_wait_multiple_deadlock(); -#ifdef BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK - check_wait_self_deadlock(); -#else - print_header("NOTE: Wait deadlock checks disabled, skipping the corresponding test."); -#endif - - print_header("Checking detach_loop() and submit_loop():"); - check_loop(); - - print_header("Checking detach_blocks() and submit_blocks():"); - check_blocks(); - - print_header("Checking detach_sequence() and submit_sequence():"); - check_sequence(); - - print_header("Checking task monitoring:"); - check_task_monitoring(); - -#ifdef BS_THREAD_POOL_ENABLE_PAUSE - print_header("Checking pausing:"); - check_pausing(); -#else - print_header("NOTE: Pausing disabled, skipping the corresponding test."); -#endif - - print_header("Checking purge():"); - check_purge(); - -#ifndef BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING - print_header("Checking exception handling:"); - check_exceptions_submit(); - check_exceptions_multi_future(); -#else - print_header("NOTE: Exception handling disabled, skipping the corresponding test."); -#endif - - print_header("Checking parallelized vector operations:"); - check_vectors(); - -#ifdef BS_THREAD_POOL_ENABLE_PRIORITY - print_header("Checking task priority:"); - check_priority(); -#else - print_header("NOTE: Task priority disabled, skipping the corresponding test."); -#endif - - print_header("Checking thread initialization functions and get_index():"); - check_init(); - - print_header("Checking get_pool():"); - check_get_pool(); - } - - if (enable_long_deadlock_tests) - { - print_header("Checking for deadlocks:"); - dual_println("Checking for destruction deadlocks..."); - check_deadlock( - [] - { - const BS::thread_pool temp_pool; - }); - dual_println("Checking for reset deadlocks..."); - BS::thread_pool temp_pool; - check_deadlock( - [&temp_pool] - { - temp_pool.reset(); - }); - } - - if (tests_failed == 0) - { - if (enable_tests) - print_header("SUCCESS: Passed all " + std::to_string(tests_succeeded) + " checks!", '+'); - if (enable_benchmarks) + if (args.size() > 0) { - print_header("Performing benchmarks:"); - check_performance(); - print_header("Thread pool performance test completed!", '+'); + if (args["help"] || !args.verify()) + { + show_intro(); + args.show_help(); + return 0; + } + if (!args["stdout"] && !args["log"]) + { + show_intro(); + args.show_help(); + sync_out.println("\nERROR: No output stream specified! Please enter one or more of: log, stdout. Aborting."); + return 0; + } + if (!args["benchmarks"] && !args["deadlock"] && !args["plot"] && !args["tests"]) + { + show_intro(); + args.show_help(); + sync_out.println("\nERROR: No tests or benchmarks requested! Please enter one or more of: benchmarks, deadlock, plot, tests. Aborting."); + return 0; + } } - std::exit(0); - } - else - { - print_header("FAILURE: Passed " + std::to_string(tests_succeeded) + " checks, but failed " + std::to_string(tests_failed) + "!", '+'); - dual_println("\nPlease submit a bug report at https://github.com/bshoshany/thread-pool/issues including the exact specifications of your system (OS, CPU, compiler, etc.) and the generated log file."); -#if defined(__APPLE__) && !defined(BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING) - // macOS does not implement `std::quick_exit` for some reason. `std::exit()` cannot be used here, as it might get stuck if a deadlock occurs. - std::terminate(); + + if (!args["stdout"]) + sync_out.remove_stream(std::cout); + + // A stream object used to access the log file. + std::ofstream log_file; + + if (args["log"]) + { + // Extract the name of the executable file, or use a default value if it is not available. + const std::string_view executable = args.get_executable(); + const std::size_t last_slash = executable.find_last_of("/\\") + 1; + std::string exe_file(executable.substr(last_slash, executable.find('.', last_slash) - last_slash)); + if (exe_file.empty()) + exe_file = "BS_thread_pool_test"; + // Create a log file using the name of the executable, followed by the current date and time. + const std::string log_filename = exe_file + "-" + get_time() + ".log"; + log_file.open(log_filename); + if (log_file.is_open()) + { + sync_out.print("Generating log file: ", log_filename, ".\n"); + sync_out.add_stream(log_file); + } + else + { + sync_out.println("ERROR: Could not create a log file."); + return 1; + } + } + + show_intro(); + + if (args["tests"]) + { + print_header("Checking the constructor:"); + check_constructor(); + + print_header("Checking reset():"); + check_reset(); + + print_header("Checking detach_task() and submit_task():"); + check_task("detach_task()"); + check_task("submit_task()"); + + print_header("Checking submission of member functions as tasks:"); + check_member_function(); + check_member_function_within_object(); + + print_header("Checking submission of different callable types:"); + check_callables(); + + print_header("Checking wait(), wait_for(), and wait_until():"); + check_wait(); + check_wait_blocks(); + check_wait_for(); + check_wait_until(); + check_wait_multiple_deadlock(); +#ifdef __cpp_exceptions + check_wait_self_deadlock(); + + print_header("Checking exception handling:"); + check_exceptions_submit(); + check_exceptions_multi_future(); #else - std::quick_exit(static_cast(tests_failed)); + print_header("NOTE: Exceptions are disabled, skipping wait deadlock check and exception handling tests."); #endif + + print_header("Checking detach_loop() and submit_loop():"); + check_loop(); + + print_header("Checking detach_blocks() and submit_blocks():"); + check_blocks(); + + print_header("Checking detach_sequence() and submit_sequence():"); + check_sequence(); + + print_header("Checking task monitoring:"); + check_task_monitoring(); + + print_header("Checking pausing:"); + check_pausing(); + + print_header("Checking purge():"); + check_purge(); + + print_header("Checking parallelized vector operations:"); + check_vectors(); + + print_header("Checking task priority:"); + check_priority(); + + print_header("Checking thread initialization/cleanup functions and BS::this_thread:"); + check_init(); + check_cleanup(); + check_get_pool(); + + print_header("Checking that parallelized tasks do not get copied:"); + check_copy_all(); + + print_header("Checking that shared pointers are correctly shared:"); + check_shared_ptr_all(); + + print_header("Checking that tasks are destructed immediately after running:"); + check_task_destruct(); + + print_header("Checking BS::common_index_type:"); + check_common_index_type(); + +#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS + print_header("Checking native extensions:"); + #ifndef _WIN32 + if ((args["benchmarks"] || args["plot"]) && !BS::set_os_process_priority(BS::os_process_priority::realtime)) + { + sync_out.println("NOTE: Skipping process/thread priority checks since the test is running on Linux/macOS without root privileges and benchmarks are enabled. On Linux/macOS, if priorities are decreased, they cannot be increased back to normal without root privileges, so the process will be stuck on the lowest priority, and the benchmarks will be unreliable.\n"); + } + else + #endif + { + // Note: We have to check thread priorities first, because the check for process priorities lowers the priority of the process to the lowest level, and on Linux, if not running as root, we can only lower the priority, not raise it, so the process gets stuck on the lowest priority. Since the thread priorities cannot be set to higher than the process priorities, this means the thread priorities will also be stuck on the lowest priority, and the test will fail. + check_os_thread_priorities(); + sync_out.println(); + check_os_process_priorities(); + sync_out.println(); + } + check_os_thread_names(); + sync_out.println(); + #if defined(_WIN32) || defined(__linux__) + check_os_thread_affinity(); + sync_out.println(); + check_os_process_affinity(); + #else + sync_out.println("NOTE: macOS does not support affinity, skipping the corresponding test."); + #endif +#else + print_header("NOTE: Native extensions disabled, skipping the corresponding test."); +#endif + } + + if (args["deadlock"]) + { + print_header("Checking for deadlocks:"); + sync_out.println("Checking for destruction deadlocks..."); + check_deadlock( + [] + { + BS::thread_pool temp_pool; + temp_pool.detach_task([] {}); + }); + sync_out.println("Checking for reset deadlocks..."); + BS::thread_pool temp_pool; + check_deadlock( + [&temp_pool] + { + temp_pool.reset(); + }); + } + + if (test_results::tests_failed > 0) + { + print_header("FAILURE: Passed " + std::to_string(test_results::tests_succeeded) + " checks, but failed " + std::to_string(test_results::tests_failed) + "!", '+'); + sync_out.println("\nPlease submit a bug report at https://github.com/bshoshany/thread-pool/issues including the exact specifications of your system (OS, CPU, compiler, etc.) and the generated log file."); + log_file.close(); + return static_cast(test_results::tests_failed); + } + + if (args["tests"]) + print_header("SUCCESS: Passed all " + std::to_string(test_results::tests_succeeded) + " checks!", '+'); + + if (args["benchmarks"] || args["plot"]) + check_performance(args["benchmarks"], args["plot"], args["save"]); + + log_file.close(); + return 0; +#ifdef __cpp_exceptions } + catch (const std::exception& e) + { + sync_out.println("ERROR: Tests failed due to exception: ", e.what()); + return 1; + } +#endif } diff --git a/tests/BS_thread_pool_test.ps1 b/tests/BS_thread_pool_test.ps1 deleted file mode 100644 index 97ce66a..0000000 --- a/tests/BS_thread_pool_test.ps1 +++ /dev/null @@ -1,232 +0,0 @@ -#!/usr/bin/env pwsh - -<# -.SYNOPSIS -BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library by Barak Shoshany (baraksh@gmail.com) (https://baraksh.com) v4.1.0 (2024-03-22) -.DESCRIPTION -This script compiles and runs the bundled test program with different compilers. -.NOTES -Copyright (c) 2024 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.1016/j.softx.2024.101687, SoftwareX 26 (2024) 101687, arXiv:2105.00613 -.LINK -https://github.com/bshoshany/thread-pool -#> - -Set-StrictMode -Version Latest - -$Host.UI.RawUI.WindowTitle = 'BS::thread_pool Test Script' - -$TitleColor = 'Green' -$TextColor = 'Yellow' -$ErrorColor = 'Red' -$CommandColor = 'Blue' - -Function Write-Title([String] $Title) -{ - If ($Title.Length -gt 0) - { - $Separator = '=' * $Title.Length - Write-Host - Write-Host $Separator -ForegroundColor $TitleColor - Write-Host $Title -ForegroundColor $TitleColor - Write-Host $Separator -ForegroundColor $TitleColor - Write-Host - } -} - -Function Write-Text([String] $Text) -{ - Write-Host $Text -ForegroundColor $TextColor -} - -Function Write-Error([String] $Text) -{ - Write-Host $Text -ForegroundColor $ErrorColor -} - -Function Write-Command([String] $Text) -{ - Write-Host $Text -ForegroundColor $CommandColor -} - -Function Exit-Script([Int] $Code) -{ - Set-Location $StartingLocation - Exit $Code -} - -Write-Title '=== BS::thread_pool test script ===' - -$StartingLocation = Get-Location - -Set-Location $PSScriptRoot - -$SourceFile = Join-Path $PSScriptRoot 'BS_thread_pool_test.cpp' -If (Test-Path -Path $SourceFile) -{ - Write-Text "Found source file $SourceFile." -} -Else -{ - Write-Error "Source file $SourceFile not found, aborting script!" - Exit-Script 1 -} - -$IncludeFile = Join-Path (Split-Path -Parent $PSScriptRoot) 'include' 'BS_thread_pool.hpp' -If (Test-Path -Path $IncludeFile) -{ - Write-Text "Found main include file $IncludeFile." -} -Else -{ - Write-Error "Main include file $IncludeFile not found, aborting script!" - Exit-Script 1 -} - -$IncludeFile = Join-Path (Split-Path -Parent $PSScriptRoot) 'include' 'BS_thread_pool_utils.hpp' -If (Test-Path -Path $IncludeFile) -{ - Write-Text "Found utilities include file $IncludeFile." -} -Else -{ - Write-Error "Utilities include file $IncludeFile not found, aborting script!" - Exit-Script 1 -} - -$BuildFolder = Join-Path (Split-Path -Parent $PSScriptRoot) 'build' -If (Test-Path -Path $BuildFolder) -{ - Write-Text "Cleaning up build folder $BuildFolder..." - Remove-Item (Join-Path $BuildFolder '*') -} -Else -{ - Write-Text "Creating build folder $BuildFolder..." - $Null = New-Item $BuildFolder -ItemType 'Directory' -} -Write-Text 'Done.' - -Write-Title 'Compiling...' - -If ($IsWindows) -{ - $Extension = '.exe' - $PThread = '' -} -Else -{ - $Extension = '' - $PThread = ' -pthread' -} - -$ExePrefix = 'BS_thread_pool_test_' - -$Executables = @() - -Function Build-ClangGCC([String] $Compiler, [String] $ExeSuffix, [String] $ExtraFlags = '') -{ - $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 $ExtraFlags" - Write-Command $Command - Invoke-Expression $Command - If ($LASTEXITCODE) - { - Write-Error "Failed to compile, aborting script! (Exit code: $LASTEXITCODE)" - Exit-Script $LASTEXITCODE - } - Else - { - Write-Text "Successfully compiled to $FullExe." - $Script:Executables += $FullExe - } -} - -If (Get-Command 'clang++' -ErrorAction SilentlyContinue) -{ - Write-Text 'Compiling with Clang...' - Build-ClangGCC 'clang++' 'clang' - Build-ClangGCC 'clang++' 'clang_light' '-DBS_THREAD_POOL_LIGHT_TEST' -} -Else -{ - Write-Error 'Clang not found, skipping it!' -} - -Write-Host - -If (Get-Command 'g++' -ErrorAction SilentlyContinue) -{ - Write-Text 'Compiling with GCC...' - Build-ClangGCC 'g++' 'gcc' '-Wuseless-cast' - Build-ClangGCC 'g++' 'gcc_light' '-Wuseless-cast -DBS_THREAD_POOL_LIGHT_TEST' -} -Else -{ - Write-Error 'GCC not found, skipping it!' -} - -Write-Host - -Function Build-MSVC([String] $ExeSuffix, [String] $ExtraFlags = '') -{ - $MSVCName = "$ExePrefix$ExeSuffix" - $FullExe = Join-Path $BuildFolder "$MSVCName$Extension" - $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 $ExtraFlags" - Write-Command $Env:BS_THREAD_POOL_MSVC_COMMAND - pwsh -Command { - $CurrentDir = Get-Location - & 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' - Set-Location $CurrentDir - Invoke-Expression $Env:BS_THREAD_POOL_MSVC_COMMAND - Exit $LASTEXITCODE - } - $Env:BS_THREAD_POOL_MSVC_COMMAND = $Null - If ($LASTEXITCODE) - { - Write-Error "Failed to compile, aborting script! (Exit code: $LASTEXITCODE)" - Exit-Script $LASTEXITCODE - } - Else - { - Remove-Item $FullObj - Write-Text "Successfully compiled to $FullExe." - $Script:Executables += $FullExe - } -} - -If ($IsWindows) -{ - $MSVCEnv = 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' - If (Test-Path $MSVCEnv) - { - Write-Text 'Compiling with MSVC...' - Build-MSVC 'msvc' - Build-MSVC 'msvc_light' '/DBS_THREAD_POOL_LIGHT_TEST' - } - Else - { - Write-Error 'MSVC not found, skipping it!' - } -} - -Set-Location $BuildFolder - -Write-Title 'Starting tests. Outputs can be found in the generated log files.' - -ForEach ($Executable in $Executables) -{ - Write-Text "Executing: $Executable" - Invoke-Expression $Executable | Out-Null - If ($LASTEXITCODE) - { - Write-Error "Test Failed, aborting script! (Exit code: $LASTEXITCODE)" - Exit-Script $LASTEXITCODE - } - Write-Text '-> Test finished successfully!' - Write-Host -} - -Write-Title '=== All tests completed successfully! ===' - -Exit-Script 0