mirror of
https://github.com/bshoshany/thread-pool.git
synced 2026-07-21 19:13:00 +04:00
Updated to v3.5.0
This commit is contained in:
+190
@@ -0,0 +1,190 @@
|
||||
---
|
||||
Language: Cpp
|
||||
AccessModifierOffset: -4
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignArrayOfStructures: None
|
||||
AlignConsecutiveMacros: None
|
||||
AlignConsecutiveAssignments: None
|
||||
AlignConsecutiveBitFields: None
|
||||
AlignConsecutiveDeclarations: None
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: Align
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
AttributeMacros:
|
||||
- __capability
|
||||
BinPackArguments: true
|
||||
BinPackParameters: true
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterControlStatement: Never
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: false
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeConceptDeclarations: true
|
||||
BreakBeforeBraces: Allman
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakInheritanceList: BeforeColon
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 10000
|
||||
CommentPragmas: "^ IWYU pragma:"
|
||||
QualifierAlignment: Leave
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
EmptyLineAfterAccessModifier: Never
|
||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
PackConstructorInitializers: BinPack
|
||||
BasedOnStyle: ""
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||
AllowAllConstructorInitializersOnNextLine: true
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IfMacros:
|
||||
- KJ_IF_MAYBE
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories:
|
||||
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||
Priority: 3
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: ".*"
|
||||
Priority: 1
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
IncludeIsMainRegex: "(Test)?$"
|
||||
IncludeIsMainSourceRegex: ""
|
||||
IndentAccessModifiers: false
|
||||
IndentCaseLabels: false
|
||||
IndentCaseBlocks: false
|
||||
IndentGotoLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentExternBlock: AfterExternBlock
|
||||
IndentRequires: false
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: false
|
||||
InsertTrailingCommas: None
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
LambdaBodyIndentation: Signature
|
||||
MacroBlockBegin: ""
|
||||
MacroBlockEnd: ""
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Auto
|
||||
ObjCBlockIndentWidth: 4
|
||||
ObjCBreakBeforeNestedBlockParam: true
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 19
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakOpenParenthesis: 0
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 60
|
||||
PenaltyIndentedWhitespace: 0
|
||||
PointerAlignment: Left
|
||||
PPIndentWidth: -1
|
||||
ReferenceAlignment: Pointer
|
||||
ReflowComments: true
|
||||
RemoveBracesLLVM: false
|
||||
SeparateDefinitionBlocks: Leave
|
||||
ShortNamespaceLines: 1
|
||||
SortIncludes: CaseSensitive
|
||||
SortJavaStaticImport: Before
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: true
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterForeachMacros: true
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterIfMacros: true
|
||||
AfterOverloadedOperator: false
|
||||
BeforeNonEmptyParentheses: false
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyBlock: false
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInAngles: Never
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
SpaceBeforeSquareBrackets: false
|
||||
BitFieldColonSpacing: Both
|
||||
Standard: Latest
|
||||
StatementAttributeLikeMacros:
|
||||
- Q_EMIT
|
||||
StatementMacros:
|
||||
- Q_UNUSED
|
||||
- QT_REQUIRE_VERSION
|
||||
TabWidth: 4
|
||||
UseCRLF: false
|
||||
UseTab: Never
|
||||
WhitespaceSensitiveMacros:
|
||||
- STRINGIZE
|
||||
- PP_STRINGIZE
|
||||
- BOOST_PP_STRINGIZE
|
||||
- NS_SWIFT_NAME
|
||||
- CF_SWIFT_NAME
|
||||
---
|
||||
@@ -6,9 +6,15 @@
|
||||
|
||||
What does your pull request fix or add to the library?
|
||||
|
||||
**Style**
|
||||
|
||||
Have you formatted your code using the `.clang-format` file attached to this project?
|
||||
|
||||
**Testing**
|
||||
|
||||
Have you tested the new code using the provided automated test program and/or performed any other tests to ensure that it works correctly? If so, please provide information about the test system(s):
|
||||
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?
|
||||
|
||||
If so, please provide information about the test system(s):
|
||||
|
||||
* CPU model, architecture, # of cores and threads:
|
||||
* Operating system:
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
build
|
||||
@@ -1,657 +0,0 @@
|
||||
/**
|
||||
* @file BS_thread_pool_light_test.cpp
|
||||
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||
* @version 3.4.0
|
||||
* @date 2023-05-12
|
||||
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. If you use this library in published research, please cite it as follows: Barak Shoshany, "A C++17 Thread Pool for High-Performance Scientific Computing", doi:10.5281/zenodo.4742687, arXiv:2105.00613 (May 2021)
|
||||
*
|
||||
* @brief BS::thread_pool_light: a fast, lightweight, and easy-to-use C++17 thread pool library. This program tests all aspects of the light version of the main library, but is not needed in order to use the library.
|
||||
*/
|
||||
|
||||
#include <algorithm> // std::min, std::sort, std::unique
|
||||
#include <atomic> // std::atomic
|
||||
#include <chrono> // std::chrono
|
||||
#include <cmath> // std::abs
|
||||
#include <condition_variable> // std::condition_variable
|
||||
#include <cstdlib> // std::quick_exit
|
||||
#include <exception> // std::exception
|
||||
#include <future> // std::future
|
||||
#include <iostream> // std::cout
|
||||
#include <memory> // std::make_unique, std::unique_ptr
|
||||
#include <mutex> // std::mutex, std::scoped_lock, std::unique_lock
|
||||
#include <random> // std::mt19937_64, std::random_device, std::uniform_int_distribution
|
||||
#include <stdexcept> // std::runtime_error
|
||||
#include <string> // std::string, std::to_string
|
||||
#include <thread> // std::this_thread, std::thread
|
||||
#include <utility> // std::forward
|
||||
#include <vector> // std::vector
|
||||
|
||||
// Include the header file for the thread pool library.
|
||||
#include "BS_thread_pool_light.hpp"
|
||||
|
||||
// ================
|
||||
// Global variables
|
||||
// ================
|
||||
|
||||
// A global thread pool object to be used throughout the test.
|
||||
BS::thread_pool_light pool;
|
||||
|
||||
// A global random_device object to be used to seed some random number generators.
|
||||
std::random_device rd;
|
||||
|
||||
// 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;
|
||||
|
||||
// ================
|
||||
// Helper functions
|
||||
// ================
|
||||
|
||||
/**
|
||||
* @brief Print any number of items into std::cout.
|
||||
*
|
||||
* @tparam T The types of the items.
|
||||
* @param items The items to print.
|
||||
*/
|
||||
template <typename... T>
|
||||
void print(T&&... items)
|
||||
{
|
||||
(std::cout << ... << std::forward<T>(items));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print any number of items into std::cout, followed by a newline character.
|
||||
*
|
||||
* @tparam T The types of the items.
|
||||
* @param items The items to print.
|
||||
*/
|
||||
template <typename... T>
|
||||
void println(T&&... items)
|
||||
{
|
||||
print(std::forward<T>(items)..., '\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a stylized header.
|
||||
*
|
||||
* @param text The text of the header. Will appear between two lines.
|
||||
* @param symbol The symbol to use for the lines. Default is '='.
|
||||
*/
|
||||
void print_header(const std::string& text, const char symbol = '=')
|
||||
{
|
||||
println();
|
||||
println(std::string(text.length(), symbol));
|
||||
println(text);
|
||||
println(std::string(text.length(), symbol));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a condition is met, report the result, and keep count of the total number of successes and failures.
|
||||
*
|
||||
* @param condition The condition to check.
|
||||
*/
|
||||
void check(const bool condition)
|
||||
{
|
||||
if (condition)
|
||||
{
|
||||
println("-> PASSED!");
|
||||
++tests_succeeded;
|
||||
}
|
||||
else
|
||||
{
|
||||
println("-> FAILED!");
|
||||
++tests_failed;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the expected result has been obtained, report the result, and keep count of the total number of successes and failures.
|
||||
*
|
||||
* @param condition The condition to check.
|
||||
*/
|
||||
template <typename T1, typename T2>
|
||||
void check(const T1 expected, const T2 obtained)
|
||||
{
|
||||
print("Expected: ", expected, ", obtained: ", obtained);
|
||||
if (expected == obtained)
|
||||
{
|
||||
println(" -> PASSED!");
|
||||
++tests_succeeded;
|
||||
}
|
||||
else
|
||||
{
|
||||
println(" -> FAILED!");
|
||||
++tests_failed;
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================
|
||||
// Functions to verify the number of threads
|
||||
// =========================================
|
||||
|
||||
/**
|
||||
* @brief Count the number of unique threads in the pool. Submits a number of tasks equal to twice the thread count into the pool. Each task stores the ID of the thread running it, and then waits until released by the main thread. This ensures that each thread in the pool runs at least one task. The number of unique thread IDs is then counted from the stored IDs.
|
||||
*/
|
||||
BS::concurrency_t count_unique_threads()
|
||||
{
|
||||
std::condition_variable ID_cv, total_cv;
|
||||
std::mutex ID_mutex, total_mutex;
|
||||
{
|
||||
const BS::concurrency_t num_tasks = pool.get_thread_count() * 2;
|
||||
std::vector<std::thread::id> thread_IDs(num_tasks);
|
||||
std::unique_lock<std::mutex> total_lock(total_mutex);
|
||||
BS::concurrency_t total_count = 0;
|
||||
bool ID_release = false;
|
||||
pool.wait_for_tasks();
|
||||
for (std::thread::id& id : thread_IDs)
|
||||
pool.push_task(
|
||||
[&total_count, &id, &ID_release, &ID_cv, &total_cv, &ID_mutex, &total_mutex]
|
||||
{
|
||||
id = std::this_thread::get_id();
|
||||
{
|
||||
const std::scoped_lock total_lock_local(total_mutex);
|
||||
++total_count;
|
||||
}
|
||||
total_cv.notify_one();
|
||||
std::unique_lock<std::mutex> ID_lock_local(ID_mutex);
|
||||
ID_cv.wait(ID_lock_local, [&ID_release] { return ID_release; });
|
||||
});
|
||||
total_cv.wait(total_lock, [&total_count] { return total_count == pool.get_thread_count(); });
|
||||
{
|
||||
const std::scoped_lock ID_lock(ID_mutex);
|
||||
ID_release = true;
|
||||
}
|
||||
ID_cv.notify_all();
|
||||
total_cv.wait(total_lock, [&total_count, &num_tasks] { return total_count == num_tasks; });
|
||||
pool.wait_for_tasks();
|
||||
std::sort(thread_IDs.begin(), thread_IDs.end());
|
||||
return static_cast<BS::concurrency_t>(std::unique(thread_IDs.begin(), thread_IDs.end()) - thread_IDs.begin());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that the constructor works.
|
||||
*/
|
||||
void check_constructor()
|
||||
{
|
||||
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());
|
||||
println("Checking that the manually counted number of unique thread IDs is equal to the reported number of threads...");
|
||||
check(pool.get_thread_count(), count_unique_threads());
|
||||
}
|
||||
|
||||
// =======================================
|
||||
// Functions to verify submission of tasks
|
||||
// =======================================
|
||||
|
||||
/**
|
||||
* @brief Check that push_task() works.
|
||||
*/
|
||||
void check_push_task()
|
||||
{
|
||||
println("Checking that push_task() works for a function with no arguments or return value...");
|
||||
{
|
||||
bool flag = false;
|
||||
pool.push_task([&flag] { flag = true; });
|
||||
pool.wait_for_tasks();
|
||||
check(flag);
|
||||
}
|
||||
println("Checking that push_task() works for a function with one argument and no return value...");
|
||||
{
|
||||
bool flag = false;
|
||||
pool.push_task([](bool* flag_) { *flag_ = true; }, &flag);
|
||||
pool.wait_for_tasks();
|
||||
check(flag);
|
||||
}
|
||||
println("Checking that push_task() works for a function with two arguments and no return value...");
|
||||
{
|
||||
bool flag1 = false;
|
||||
bool flag2 = false;
|
||||
pool.push_task([](bool* flag1_, bool* flag2_) { *flag1_ = *flag2_ = true; }, &flag1, &flag2);
|
||||
pool.wait_for_tasks();
|
||||
check(flag1 && flag2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that submit() works.
|
||||
*/
|
||||
void check_submit()
|
||||
{
|
||||
println("Checking that submit() works for a function with no arguments or return value...");
|
||||
{
|
||||
bool flag = false;
|
||||
pool.submit([&flag] { flag = true; }).wait();
|
||||
check(flag);
|
||||
}
|
||||
println("Checking that submit() works for a function with one argument and no return value...");
|
||||
{
|
||||
bool flag = false;
|
||||
pool.submit([](bool* flag_) { *flag_ = true; }, &flag).wait();
|
||||
check(flag);
|
||||
}
|
||||
println("Checking that submit() works for a function with two arguments and no return value...");
|
||||
{
|
||||
bool flag1 = false;
|
||||
bool flag2 = false;
|
||||
pool.submit([](bool* flag1_, bool* flag2_) { *flag1_ = *flag2_ = true; }, &flag1, &flag2).wait();
|
||||
check(flag1 && flag2);
|
||||
}
|
||||
println("Checking that submit() works for a function with no arguments and a return value...");
|
||||
{
|
||||
bool flag = false;
|
||||
std::future<int> flag_future = pool.submit(
|
||||
[&flag]
|
||||
{
|
||||
flag = true;
|
||||
return 42;
|
||||
});
|
||||
check(flag_future.get() == 42 && flag);
|
||||
}
|
||||
println("Checking that submit() works for a function with one argument and a return value...");
|
||||
{
|
||||
bool flag = false;
|
||||
std::future<int> flag_future = pool.submit(
|
||||
[](bool* flag_)
|
||||
{
|
||||
*flag_ = true;
|
||||
return 42;
|
||||
},
|
||||
&flag);
|
||||
check(flag_future.get() == 42 && flag);
|
||||
}
|
||||
println("Checking that submit() works for a function with two arguments and a return value...");
|
||||
{
|
||||
bool flag1 = false;
|
||||
bool flag2 = false;
|
||||
std::future<int> flag_future = pool.submit(
|
||||
[](bool* flag1_, bool* flag2_)
|
||||
{
|
||||
*flag1_ = *flag2_ = true;
|
||||
return 42;
|
||||
},
|
||||
&flag1, &flag2);
|
||||
check(flag_future.get() == 42 && flag1 && flag2);
|
||||
}
|
||||
}
|
||||
|
||||
class flag_class
|
||||
{
|
||||
public:
|
||||
void set_flag_no_args()
|
||||
{
|
||||
flag = true;
|
||||
}
|
||||
|
||||
void set_flag_one_arg(const bool arg)
|
||||
{
|
||||
flag = arg;
|
||||
}
|
||||
|
||||
int set_flag_no_args_return()
|
||||
{
|
||||
flag = true;
|
||||
return 42;
|
||||
}
|
||||
|
||||
int set_flag_one_arg_return(const bool arg)
|
||||
{
|
||||
flag = arg;
|
||||
return 42;
|
||||
}
|
||||
|
||||
bool get_flag() const
|
||||
{
|
||||
return flag;
|
||||
}
|
||||
|
||||
void push_test_flag_no_args()
|
||||
{
|
||||
pool.push_task(&flag_class::set_flag_no_args, this);
|
||||
pool.wait_for_tasks();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void push_test_flag_one_arg()
|
||||
{
|
||||
pool.push_task(&flag_class::set_flag_one_arg, this, true);
|
||||
pool.wait_for_tasks();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_no_args()
|
||||
{
|
||||
pool.submit(&flag_class::set_flag_no_args, this).wait();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_one_arg()
|
||||
{
|
||||
pool.submit(&flag_class::set_flag_one_arg, this, true).wait();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_no_args_return()
|
||||
{
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_no_args_return, this);
|
||||
check(flag_future.get() == 42 && get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_one_arg_return()
|
||||
{
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_one_arg_return, this, true);
|
||||
check(flag_future.get() == 42 && get_flag());
|
||||
}
|
||||
|
||||
private:
|
||||
bool flag = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Check that submitting member functions works.
|
||||
*/
|
||||
void check_member_function()
|
||||
{
|
||||
println("Checking that push_task() works for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.push_task(&flag_class::set_flag_no_args, &flag);
|
||||
pool.wait_for_tasks();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
println("Checking that push_task() works for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.push_task(&flag_class::set_flag_one_arg, &flag, true);
|
||||
pool.wait_for_tasks();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
println("Checking that submit() works for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.submit(&flag_class::set_flag_no_args, &flag).wait();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
println("Checking that submit() works for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.submit(&flag_class::set_flag_one_arg, &flag, true).wait();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
println("Checking that submit() works for a member function with no arguments and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_no_args_return, &flag);
|
||||
check(flag_future.get() == 42 && flag.get_flag());
|
||||
}
|
||||
println("Checking that submit() works for a member function with one argument and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_one_arg_return, &flag, true);
|
||||
check(flag_future.get() == 42 && flag.get_flag());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that submitting member functions within an object works.
|
||||
*/
|
||||
void check_member_function_within_object()
|
||||
{
|
||||
println("Checking that push_task() works within an object for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag.push_test_flag_no_args();
|
||||
}
|
||||
println("Checking that push_task() works within an object for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag.push_test_flag_one_arg();
|
||||
}
|
||||
println("Checking that submit() works within an object for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag.submit_test_flag_no_args();
|
||||
}
|
||||
println("Checking that submit() works within an object for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag.submit_test_flag_one_arg();
|
||||
}
|
||||
println("Checking that submit() works within an object for a member function with no arguments and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag.submit_test_flag_no_args_return();
|
||||
}
|
||||
println("Checking that submit() works within an object for a member function with one argument and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag.submit_test_flag_one_arg_return();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that wait_for_tasks() works.
|
||||
*/
|
||||
void check_wait_for_tasks()
|
||||
{
|
||||
const BS::concurrency_t n = pool.get_thread_count() * 10;
|
||||
std::unique_ptr<std::atomic<bool>[]> flags = std::make_unique<std::atomic<bool>[]>(n);
|
||||
for (BS::concurrency_t i = 0; i < n; ++i)
|
||||
pool.push_task(
|
||||
[&flags, i]
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
flags[i] = true;
|
||||
});
|
||||
println("Waiting for tasks...");
|
||||
pool.wait_for_tasks();
|
||||
bool all_flags = true;
|
||||
for (BS::concurrency_t i = 0; i < n; ++i)
|
||||
all_flags = all_flags && flags[i];
|
||||
check(all_flags);
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// Functions to verify loop parallelization
|
||||
// ========================================
|
||||
|
||||
/**
|
||||
* @brief Check that push_loop() works for a specific range of indices split over a specific number of tasks, with no return value.
|
||||
*
|
||||
* @param random_start The first index in the loop.
|
||||
* @param random_end The last index in the loop plus 1.
|
||||
* @param num_tasks The number of tasks.
|
||||
*/
|
||||
template <typename T>
|
||||
void check_push_loop_no_return(const int64_t random_start, T random_end, const BS::concurrency_t num_tasks)
|
||||
{
|
||||
if (random_start == random_end)
|
||||
++random_end;
|
||||
println("Verifying that push_loop() from ", random_start, " to ", random_end, " with ", num_tasks, num_tasks == 1 ? " task" : " tasks", " modifies all indices...");
|
||||
const size_t num_indices = static_cast<size_t>(std::abs(random_end - random_start));
|
||||
const int64_t offset = std::min<int64_t>(random_start, static_cast<int64_t>(random_end));
|
||||
std::unique_ptr<std::atomic<bool>[]> flags = std::make_unique<std::atomic<bool>[]>(num_indices);
|
||||
const auto loop = [&flags, offset](const int64_t start, const int64_t end)
|
||||
{
|
||||
for (int64_t i = start; i < end; ++i)
|
||||
flags[static_cast<size_t>(i - offset)] = true;
|
||||
};
|
||||
if (random_start == 0)
|
||||
pool.push_loop(random_end, loop, num_tasks);
|
||||
else
|
||||
pool.push_loop(random_start, random_end, loop, num_tasks);
|
||||
pool.wait_for_tasks();
|
||||
bool all_flags = true;
|
||||
for (size_t i = 0; i < num_indices; ++i)
|
||||
all_flags = all_flags && flags[i];
|
||||
check(all_flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that push_loop() works using several different random values for the range of indices and number of tasks.
|
||||
*/
|
||||
void check_push_loop()
|
||||
{
|
||||
std::mt19937_64 mt(rd());
|
||||
std::uniform_int_distribution<int64_t> index_dist(-1000000, 1000000);
|
||||
std::uniform_int_distribution<BS::concurrency_t> task_dist(1, pool.get_thread_count());
|
||||
constexpr uint64_t n = 10;
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
check_push_loop_no_return(index_dist(mt), index_dist(mt), task_dist(mt));
|
||||
println("Verifying that push_loop() with identical start and end indices does nothing...");
|
||||
bool flag = true;
|
||||
const int64_t index = index_dist(mt);
|
||||
pool.push_loop(index, index, [&flag](const int64_t, const int64_t) { flag = false; });
|
||||
pool.wait_for_tasks();
|
||||
check(flag);
|
||||
println("Trying push_loop() with start and end indices of different types:");
|
||||
const int64_t start = index_dist(mt);
|
||||
const uint32_t end = static_cast<uint32_t>(std::abs(index_dist(mt)));
|
||||
check_push_loop_no_return(start, end, task_dist(mt));
|
||||
println("Trying the overload for push_loop() for the case where the first index is equal to 0:");
|
||||
check_push_loop_no_return(0, index_dist(mt), task_dist(mt));
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// Functions to verify exception handling
|
||||
// ======================================
|
||||
|
||||
/**
|
||||
* @brief Check that exception handling works.
|
||||
*/
|
||||
void check_exceptions()
|
||||
{
|
||||
println("Checking that exceptions are forwarded correctly by submit()...");
|
||||
bool caught = false;
|
||||
auto throws = []
|
||||
{
|
||||
println("Throwing exception...");
|
||||
throw std::runtime_error("Exception thrown!");
|
||||
};
|
||||
std::future<void> my_future = pool.submit(throws);
|
||||
try
|
||||
{
|
||||
my_future.get();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
if (e.what() == std::string("Exception thrown!"))
|
||||
caught = true;
|
||||
}
|
||||
check(caught);
|
||||
}
|
||||
|
||||
// =====================================
|
||||
// Functions to verify vector operations
|
||||
// =====================================
|
||||
|
||||
/**
|
||||
* @brief Check that parallelized vector operations work as expected by calculating the sum of two randomized vectors of a specific size in two ways, single-threaded and multithreaded, and comparing the results.
|
||||
*/
|
||||
void check_vector_of_size(const size_t vector_size, const BS::concurrency_t num_tasks)
|
||||
{
|
||||
std::vector<int64_t> vector_1(vector_size);
|
||||
std::vector<int64_t> vector_2(vector_size);
|
||||
std::mt19937_64 mt(rd());
|
||||
std::uniform_int_distribution<int64_t> vector_dist(-1000000, 1000000);
|
||||
for (size_t i = 0; i < vector_size; ++i)
|
||||
{
|
||||
vector_1[i] = vector_dist(mt);
|
||||
vector_2[i] = vector_dist(mt);
|
||||
}
|
||||
println("Adding two vectors with ", vector_size, " elements using ", num_tasks, " tasks...");
|
||||
std::vector<int64_t> sum_single(vector_size);
|
||||
for (size_t i = 0; i < vector_size; ++i)
|
||||
sum_single[i] = vector_1[i] + vector_2[i];
|
||||
std::vector<int64_t> sum_multi(vector_size);
|
||||
pool.push_loop(
|
||||
0, vector_size,
|
||||
[&sum_multi, &vector_1, &vector_2](const size_t start, const size_t end)
|
||||
{
|
||||
for (size_t i = start; i < end; ++i)
|
||||
sum_multi[i] = vector_1[i] + vector_2[i];
|
||||
},
|
||||
num_tasks);
|
||||
pool.wait_for_tasks();
|
||||
bool vectors_equal = true;
|
||||
for (size_t i = 0; i < vector_size; ++i)
|
||||
vectors_equal = vectors_equal && (sum_single[i] == sum_multi[i]);
|
||||
check(vectors_equal);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that parallelized vector operations work as expected by calculating the sum of two randomized vectors in two ways, single-threaded and multithreaded, and comparing the results.
|
||||
*/
|
||||
void check_vectors()
|
||||
{
|
||||
std::mt19937_64 mt(rd());
|
||||
std::uniform_int_distribution<size_t> size_dist(0, 1000000);
|
||||
std::uniform_int_distribution<BS::concurrency_t> task_dist(1, pool.get_thread_count());
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
check_vector_of_size(size_dist(mt), task_dist(mt));
|
||||
}
|
||||
|
||||
// ==================
|
||||
// Main test function
|
||||
// ==================
|
||||
|
||||
/**
|
||||
* @brief Test that various aspects of the library are working as expected.
|
||||
*/
|
||||
void do_tests()
|
||||
{
|
||||
print_header("Checking that the constructor works:");
|
||||
check_constructor();
|
||||
|
||||
print_header("Checking that push_task() works:");
|
||||
check_push_task();
|
||||
|
||||
print_header("Checking that submit() works:");
|
||||
check_submit();
|
||||
|
||||
print_header("Checking that submitting member functions works:");
|
||||
check_member_function();
|
||||
|
||||
print_header("Checking that submitting member functions from within an object works:");
|
||||
check_member_function_within_object();
|
||||
|
||||
print_header("Checking that wait_for_tasks() works...");
|
||||
check_wait_for_tasks();
|
||||
|
||||
print_header("Checking that push_loop() works:");
|
||||
check_push_loop();
|
||||
|
||||
print_header("Checking that exception handling works:");
|
||||
check_exceptions();
|
||||
|
||||
print_header("Testing that vector operations produce the expected results:");
|
||||
check_vectors();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
println("BS::thread_pool_light: a fast, lightweight, and easy-to-use C++17 thread pool library");
|
||||
println("(c) 2023 Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)");
|
||||
println("GitHub: https://github.com/bshoshany/thread-pool\n");
|
||||
|
||||
println("Thread pool library version is ", BS_THREAD_POOL_LIGHT_VERSION, ".");
|
||||
println("Hardware concurrency is ", std::thread::hardware_concurrency(), ".");
|
||||
|
||||
println("Important: Please do not run any other applications, especially multithreaded applications, in parallel with this test!");
|
||||
|
||||
do_tests();
|
||||
|
||||
if (tests_failed == 0)
|
||||
{
|
||||
print_header("SUCCESS: Passed all " + std::to_string(tests_succeeded) + " checks!", '+');
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
print_header("FAILURE: Passed " + std::to_string(tests_succeeded) + " checks, but failed " + std::to_string(tests_failed) + "!", '+');
|
||||
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.");
|
||||
std::quick_exit(static_cast<int>(tests_failed));
|
||||
}
|
||||
}
|
||||
+31
-10
@@ -1,13 +1,3 @@
|
||||
[](https://doi.org/10.5281/zenodo.4742687)
|
||||
[](https://arxiv.org/abs/2105.00613)
|
||||
[](https://github.com/bshoshany/thread-pool/blob/master/LICENSE.txt)
|
||||

|
||||

|
||||

|
||||
[](https://github.com/bshoshany/thread-pool)
|
||||
[](https://twitter.com/BarakShoshany)
|
||||
[](https://vscode.dev/github/bshoshany/thread-pool)
|
||||
|
||||
# `BS::thread_pool`: a fast, lightweight, and easy-to-use C++17 thread pool library
|
||||
|
||||
By Barak Shoshany\
|
||||
@@ -16,6 +6,7 @@ Website: <https://baraksh.com/>\
|
||||
GitHub: <https://github.com/bshoshany>
|
||||
|
||||
* [Version history](#version-history)
|
||||
* [v3.5.0 (2023-05-25)](#v350-2023-05-25)
|
||||
* [v3.4.0 (2023-05-12)](#v340-2023-05-12)
|
||||
* [v3.3.0 (2022-08-03)](#v330-2022-08-03)
|
||||
* [v3.2.0 (2022-07-28)](#v320-2022-07-28)
|
||||
@@ -35,6 +26,36 @@ GitHub: <https://github.com/bshoshany>
|
||||
|
||||
## Version history
|
||||
|
||||
### v3.5.0 (2023-05-25)
|
||||
|
||||
* `BS_thread_pool.hpp` and `BS_thread_pool_light.hpp`:
|
||||
* Added a new member function, `purge()`, to the full (non-light) thread pool. This function purges 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 removed and will never be executed by the threads. Please note that there is no way to restore the purged tasks.
|
||||
* Fix a bug which caused `wait_for_tasks()` to only block the first thread that called it. Now it blocks every thread that calls it, which is the expected behavior. In addition, all related deadlock have now been completely resolved. This also applies to the variants `wait_for_tasks_duration()` and `wait_for_tasks_until()` in the non-light version. See [#110](https://github.com/bshoshany/thread-pool/pull/110).
|
||||
* Note: You should never call `wait_for_tasks()` from within a thread of the same thread pool, as that will cause it to wait forever! This fix is relevant for situations when `wait_for_tasks()` is called from an auxiliary `std::thread` or a separate thread pool.
|
||||
* `push_task()` and `submit()` now avoid creating unnecessary copies of the function object. This should improve performance, especially if large objects are involved. See [#90](https://github.com/bshoshany/thread-pool/pull/90).
|
||||
* Optimized the way condition variables are used by the thread pool class. Shared variables are now modified while owning the mutex, but condition variables are notified after the mutex is released, if possible. See [#84](https://github.com/bshoshany/thread-pool/pull/84).
|
||||
* Instead of a variable `tasks_total` to keep track of the total number of tasks (queued + running), the thread pool class now uses a variable `tasks_running` to keep track only of the number of running tasks, with the number of tasks in the queue obtained via `tasks.size()`. This makes more sense in terms of the internal logic of the class.
|
||||
* All atomic variables have been converted to non-atomic. They are now all governed by `tasks_mutex`, so they do not need to be atomic. This eliminates redundant locking, and may improve performance a bit.
|
||||
* `running` has been renamed to `workers_running` and `task_done_cv` has been renamed to `tasks_done_cv`.
|
||||
* The worker now only notifies thi condition variable `tasks_done_cv` if all the tasks are done, not just a single task. Checking if the tasks are done is cheaper than notifying the condition variable, so since the worker no longer notifies the condition variable every single time it finishes a task, this should improve performance a bit if `wait_for_tasks()` is used.
|
||||
* `BS_thread_pool_test.cpp`:
|
||||
* Combined the tests for the full and light versions into one program. The file `BS_thread_pool_light_test.cpp` has been removed.
|
||||
* The tests for the light version are now much more comprehensive. The only features that are not tested in the light version are those that do not exist in it.
|
||||
* Added a test for the new `purge()` member function.
|
||||
* Added a test to ensure that `push_task()` and `submit()` do not create unnecessary copies of the function object.
|
||||
* Added a test to ensure that `push_task()` and `submit()` correctly accept arguments passed by value, reference, and constant reference.
|
||||
* Added a test to ensure that `wait_for_tasks()` blocks all external threads that call it.
|
||||
* `_CRT_SECURE_NO_WARNINGS` is now set only if it has not already been defined, to prevent errors in MSVC projects which already have it set as part of the default build settings. See [#72](https://github.com/bshoshany/thread-pool/pull/72).
|
||||
* `README.md`:
|
||||
* Added documentation for the new `purge()` member function.
|
||||
* Added an explanation for how to pass arguments by reference or constant reference when submitting functions to the queue, using the wrappers `std::ref()` and `std::cref()` respectively. See [#83](https://github.com/bshoshany/thread-pool/issues/83).
|
||||
* Added a link to [my lecture notes](https://baraksh.com/CSE701/notes.php) for a course taught at McMaster University, for the benefit of beginner C++ programmers who wish to learn some of the advanced techniques and programming practices used in developing this library.
|
||||
* Removed the sample test results, since the complete log file (including the deadlock tests) is now over 500 lines long.
|
||||
* Other:
|
||||
* A `.clang-format` file with the project's formatting conventions is now included in the GitHub repository. The pull request template now asks to format any new code using this file, so that it is consistent with the rest of the library.
|
||||
* A PowerShell script, `BS_thread_pool_test.ps1`, is now provided in the GitHub repository 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, compile the test program using each available compiler, and then run each compiled test program 5 times and report on any errors. The pull request template now recommends using this script for testing.
|
||||
* Since the root folder has become a bit crowded, the header files `BS_thread_pool.hpp` and `BS_thread_pool_light.hpp` have been moved to the `include` subfolder, and the test file `BS_thread_pool_test.cpp` has been moved to the `tests` subfolder, which also contains the new test script `BS_thread_pool_test.ps1`.
|
||||
|
||||
### v3.4.0 (2023-05-12)
|
||||
|
||||
* `BS_thread_pool.hpp` and `BS_thread_pool_light.hpp`:
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||

|
||||

|
||||
[](https://github.com/bshoshany/thread-pool)
|
||||
[](https://twitter.com/BarakShoshany)
|
||||
[](https://vscode.dev/github/bshoshany/thread-pool)
|
||||
|
||||
# `BS::thread_pool`: a fast, lightweight, and easy-to-use C++17 thread pool library
|
||||
@@ -15,7 +14,7 @@ Email: <baraksh@gmail.com>\
|
||||
Website: <https://baraksh.com/>\
|
||||
GitHub: <https://github.com/bshoshany>
|
||||
|
||||
This is the complete documentation for v3.4.0 of the library, released on 2023-05-12.
|
||||
This is the complete documentation for v3.5.0 of the library, released on 2023-05-25.
|
||||
|
||||
* [Introduction](#introduction)
|
||||
* [Motivation](#motivation)
|
||||
@@ -34,6 +33,7 @@ This is the complete documentation for v3.4.0 of the library, released on 2023-0
|
||||
* [Manually waiting for all tasks to complete](#manually-waiting-for-all-tasks-to-complete)
|
||||
* [Waiting with a timeout](#waiting-with-a-timeout)
|
||||
* [Submitting class member functions to the queue](#submitting-class-member-functions-to-the-queue)
|
||||
* [Passing task arguments by reference](#passing-task-arguments-by-reference)
|
||||
* [Parallelizing loops](#parallelizing-loops)
|
||||
* [Automatic parallelization of loops](#automatic-parallelization-of-loops)
|
||||
* [Loops with return values](#loops-with-return-values)
|
||||
@@ -45,6 +45,7 @@ This is the complete documentation for v3.4.0 of the library, released on 2023-0
|
||||
* [Other features](#other-features)
|
||||
* [Monitoring the tasks](#monitoring-the-tasks)
|
||||
* [Pausing the workers](#pausing-the-workers)
|
||||
* [Purging tasks](#purging-tasks)
|
||||
* [Exception handling](#exception-handling)
|
||||
* [Testing the package](#testing-the-package)
|
||||
* [Automated tests](#automated-tests)
|
||||
@@ -55,6 +56,7 @@ This is the complete documentation for v3.4.0 of the library, released on 2023-0
|
||||
* [Acknowledgements](#acknowledgements)
|
||||
* [Starring the repository](#starring-the-repository)
|
||||
* [Copyright and citing](#copyright-and-citing)
|
||||
* [Learning more about C++](#learning-more-about-c)
|
||||
|
||||
## Introduction
|
||||
|
||||
@@ -85,25 +87,28 @@ Other, more advanced multithreading libraries may offer more features and/or hig
|
||||
* 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 ~340 lines of code, excluding comments and blank lines.
|
||||
* A stand-alone "light version" of the C++ thread pool is also available in the `BS_thread_pool_light.hpp` header file, with only ~170 lines of code.
|
||||
* Only 247 lines of code, excluding comments, blank lines, and lines containing only a single brace.
|
||||
* A stand-alone "light version" of the C++ thread pool is also available in the `BS_thread_pool_light.hpp` header file, with only 115 lines of code.
|
||||
* **Easy to use:**
|
||||
* Very simple operation, using a handful of member functions.
|
||||
* Every task submitted to the queue using the `submit()` 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.
|
||||
* Optionally, tasks may also be submitted using the `push_task()` member function without generating a future, sacrificing convenience for even greater performance.
|
||||
* Loops can be automatically parallelized into any number of parallel tasks using the `parallelize_loop()` member function, which returns a `BS::multi_future` (see below) that can be used to track the execution of all parallel tasks at once.
|
||||
* If futures are not needed, tasks may be submitted using `push_task()`, and loops can be parallelized using `push_loop()` - sacrificing convenience for even greater performance.
|
||||
* 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 package.
|
||||
* **Helper classes:**
|
||||
* Automatically parallelize a loop into any number of parallel tasks using the `parallelize_loop()` member function, and track its execution using the `BS::multi_future` helper class.
|
||||
* Track the execution of multiple futures at once using the `BS::multi_future` helper class.
|
||||
* Synchronize output to a stream from multiple threads in parallel using the `BS::synced_stream` helper class.
|
||||
* Easily measure execution time for benchmarking purposes using the `BS::timer` helper class.
|
||||
* **Additional features:**
|
||||
* Easily wait for all tasks in the queue to complete using the `wait_for_tasks()` member function.
|
||||
* Easily wait for all tasks in the queue to complete using the `wait_for_tasks()`, `wait_for_tasks_duration()`, and `wait_for_tasks_until()` member functions.
|
||||
* 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.
|
||||
* 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.
|
||||
* Catch exceptions thrown by the submitted tasks.
|
||||
* 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()` or `parallelize_loop()` from the main thread through their futures.
|
||||
* 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.
|
||||
* 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).
|
||||
|
||||
### Compiling and compatibility
|
||||
@@ -111,12 +116,12 @@ Other, more advanced multithreading libraries may offer more features and/or hig
|
||||
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:
|
||||
|
||||
* Windows 11 build 22621.1702:
|
||||
* [Clang](https://clang.llvm.org/) v16.0.3
|
||||
* [Clang](https://clang.llvm.org/) v16.0.4
|
||||
* [GCC](https://gcc.gnu.org/) v13.1.0 ([WinLibs build](https://winlibs.com/))
|
||||
* [MSVC](https://docs.microsoft.com/en-us/cpp/) v19.35.32217.1
|
||||
* Ubuntu 22.10:
|
||||
* [Clang](https://clang.llvm.org/) v15.0.7
|
||||
* [GCC](https://gcc.gnu.org/) v12.2.0
|
||||
* [MSVC](https://docs.microsoft.com/en-us/cpp/) v19.36.32532
|
||||
* Ubuntu 23.04:
|
||||
* [Clang](https://clang.llvm.org/) v16.0.0
|
||||
* [GCC](https://gcc.gnu.org/) v13.0.1
|
||||
|
||||
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/) v12.2.0.
|
||||
|
||||
@@ -167,7 +172,7 @@ If you are using the [Conan](https://conan.io/) C/C++ package manager, please re
|
||||
|
||||
### Including the library
|
||||
|
||||
If you are not using a C++ library manager (such as vcpkg), simply download the [latest release](https://github.com/bshoshany/thread-pool/releases) from the GitHub repository, place the single header file `BS_thread_pool.hpp` in the desired folder, and include it in your program:
|
||||
If you are not using a C++ library manager (such as vcpkg), simply download the [latest release](https://github.com/bshoshany/thread-pool/releases) from the GitHub repository, place the single header file `BS_thread_pool.hpp` from the `include` folder of the repository in the desired folder, and include it in your program:
|
||||
|
||||
```cpp
|
||||
#include "BS_thread_pool.hpp"
|
||||
@@ -214,7 +219,7 @@ std::cout << "Thread pool library version is " << BS_THREAD_POOL_VERSION << ".\n
|
||||
Sample output:
|
||||
|
||||
```none
|
||||
Thread pool library version is v3.4.0 (2023-05-12).
|
||||
Thread pool library version is v3.5.0 (2023-05-25).
|
||||
```
|
||||
|
||||
This can be used, for example, to allow the same code to work with several incompatible versions of the library.
|
||||
@@ -337,7 +342,7 @@ pool.push_task(task, arg);
|
||||
pool.push_task(task, arg1, arg2);
|
||||
```
|
||||
|
||||
**Warning!** Since `push_task()` does not return a future, there is no built-in way for the user to know when the task finishes executing. You must use either `wait_for_tasks()` (see below), or some other method such as condition variables, to ensure that the task finishes executing before trying to use anything that depends on its output. Otherwise, bad things will happen!
|
||||
**Warning:** Since `push_task()` does not return a future, there is no built-in way for the user to know when the task finishes executing. You must use either `wait_for_tasks()` (see below), or some other method such as condition variables, to ensure that the task finishes executing before trying to use anything that depends on its output. Otherwise, bad things will happen!
|
||||
|
||||
### Manually waiting for all tasks to complete
|
||||
|
||||
@@ -373,6 +378,8 @@ after the `for` loop will ensure - as efficiently as possible - that all tasks h
|
||||
|
||||
Note, however, that `wait_for_tasks()` will wait for **all** the tasks in the queue, including those that are unrelated to the `for` loop. Using [`parallelize_loop()`](#parallelizing-loops) would make much more sense in this particular case, as it will allow waiting only for the tasks related to the loop.
|
||||
|
||||
**Warning:** Never call `wait_for_tasks()` from within a thread of the same thread pool, for example `pool.push_task([] { pool.wait_for_tasks(); })`, as that will cause it to wait forever!
|
||||
|
||||
### Waiting 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. This can be achieved using two member functions:
|
||||
@@ -387,7 +394,6 @@ Here is an example:
|
||||
|
||||
int main()
|
||||
{
|
||||
BS::synced_stream sync_out;
|
||||
BS::thread_pool pool;
|
||||
std::atomic<bool> done = false;
|
||||
pool.push_task(
|
||||
@@ -400,11 +406,11 @@ int main()
|
||||
{
|
||||
pool.wait_for_tasks_duration(std::chrono::milliseconds(200));
|
||||
if (!done)
|
||||
sync_out.println("Sorry, task is not done yet.");
|
||||
std::cout << "Sorry, task is not done yet.\n";
|
||||
else
|
||||
break;
|
||||
}
|
||||
sync_out.println("Task done!");
|
||||
std::cout << "Task done!\n";
|
||||
}
|
||||
```
|
||||
|
||||
@@ -516,6 +522,39 @@ int main()
|
||||
}
|
||||
```
|
||||
|
||||
### Passing task arguments by 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.
|
||||
|
||||
When submitting a task using `push_task()` or `submit()`, the task's arguments are always passed by value by default. To pass arguments to the task by reference, you must wrap them with `std::ref()`. Similarly, to pass arguments by constant reference, you must wrap them with `std::cref()`. Here is an example:
|
||||
|
||||
```cpp
|
||||
#include "BS_thread_pool.hpp"
|
||||
|
||||
BS::thread_pool pool;
|
||||
|
||||
void increment(int& x)
|
||||
{
|
||||
++x;
|
||||
}
|
||||
|
||||
void print(const int& x)
|
||||
{
|
||||
std::cout << x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
pool.submit(increment, std::ref(n)).wait();
|
||||
pool.submit(print, std::cref(n)).wait();
|
||||
}
|
||||
```
|
||||
|
||||
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()`. To pass `n` by reference, we wrapped it inside `std::ref()`. Note that the program will not compile otherwise, since `increment()` only accepts arguments by reference.
|
||||
|
||||
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. To pass `n` by constant reference, we wrapped it inside `std::cref()`.
|
||||
|
||||
## Parallelizing loops
|
||||
|
||||
### Automatic parallelization of loops
|
||||
@@ -674,7 +713,7 @@ int main()
|
||||
|
||||
As with `parallelize_loop()`, the first argument can be omitted if the start index is 0, and the last argument can be omitted if the number of blocks should be equal to the number of threads.
|
||||
|
||||
**Warning!** Since `push_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_for_tasks()`](#manually-waiting-for-all-tasks-to-complete), 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 `push_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_for_tasks()`](#manually-waiting-for-all-tasks-to-complete), 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!
|
||||
|
||||
## Helper classes
|
||||
|
||||
@@ -1080,7 +1119,49 @@ All tasks completed.
|
||||
|
||||
The first `wait_for_tasks()`, which was called while the pool was not paused, waited for all 8 tasks, both running and queued. The second `wait_for_tasks()`, 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_for_tasks()`, which was called after unpausing the pool, waited for the remaining 8 tasks, both running and queued.
|
||||
|
||||
**Warning**: If the thread pool is destroyed while paused, any tasks still in the queue will never be executed!
|
||||
**Warning:** If the thread pool is destroyed while paused, any tasks still in the queue will never be executed!
|
||||
|
||||
### Purging tasks
|
||||
|
||||
Consider a situation where the user cancels a multi-threaded 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.
|
||||
|
||||
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::synced_stream sync_out;
|
||||
BS::thread_pool pool(4);
|
||||
|
||||
int main()
|
||||
{
|
||||
for (size_t i = 0; i < 8; ++i)
|
||||
{
|
||||
pool.push_task(
|
||||
[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_for_tasks();
|
||||
}
|
||||
```
|
||||
|
||||
The program submit 8 tasks to the queue. Each task waits 100 milliseconds and then prints a message. The thread pool has 4 threads, so it will execute the first 4 tasks in parallel, and then the remaining 4. We wait 50 milliseconds, to ensure that the first 4 tasks have all started running. Then we call `purge()` to purge the remaining 4 tasks. As a result, these tasks never get executed. However, since the first 4 tasks are still running when `purge()` is called, they will finish uninterrupted; `purge()` only discards tasks that have not yet started running. The output of the program therefore only contains the messages from the first 4 tasks:
|
||||
|
||||
```none
|
||||
Task 0 done.
|
||||
Task 1 done.
|
||||
Task 2 done.
|
||||
Task 3 done.
|
||||
```
|
||||
|
||||
### Exception handling
|
||||
|
||||
@@ -1159,316 +1240,26 @@ When using `BS::multi_future` to handle multiple futures at once, exception hand
|
||||
|
||||
## Testing the package
|
||||
|
||||
The included file `BS_thread_pool_test.cpp` will perform automated tests of all aspects of the package, and perform simple benchmarks. The output will be printed both to `std::cout` and to a file named `BS_thread_pool_test-yyyy-mm-dd_hh.mm.ss.log` based on the current date and time. In addition, the code is thoroughly documented, and is meant to serve as an extensive example of how to properly use the package.
|
||||
### 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 package. The output will be printed both to `std::cout` and to a file named `BS_thread_pool_test-yyyy-mm-dd_hh.mm.ss.log` based on the current date and time. In addition, the code is thoroughly documented, and is meant to serve as an extensive example of how to properly use the package.
|
||||
|
||||
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.
|
||||
|
||||
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, compile the test program using each available compiler, and then run each compiled test program 5 times and report on any errors.
|
||||
|
||||
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.
|
||||
|
||||
### Automated tests
|
||||
|
||||
A sample output of a successful run of the automated tests is as follows:
|
||||
|
||||
```none
|
||||
BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library
|
||||
(c) 2023 Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||
GitHub: https://github.com/bshoshany/thread-pool
|
||||
|
||||
Thread pool library version is v3.4.0 (2023-05-12).
|
||||
Hardware concurrency is 32.
|
||||
Generating log file: BS_thread_pool_test-2023-05-12_12.48.13.log.
|
||||
|
||||
Important: Please do not run any other applications, especially multithreaded applications, in parallel with this test!
|
||||
|
||||
====================================
|
||||
Checking that the constructor works:
|
||||
====================================
|
||||
Checking that the thread pool reports a number of threads equal to the hardware concurrency...
|
||||
Expected: 32, obtained: 32 -> PASSED!
|
||||
Checking that the manually counted number of unique thread IDs is equal to the reported number of threads...
|
||||
Expected: 32, obtained: 32 -> PASSED!
|
||||
|
||||
============================
|
||||
Checking that reset() works:
|
||||
============================
|
||||
Checking that after reset() the thread pool reports a number of threads equal to half the hardware concurrency...
|
||||
Expected: 16, obtained: 16 -> PASSED!
|
||||
Checking that after reset() the manually counted number of unique thread IDs is equal to the reported number of threads...
|
||||
Expected: 16, obtained: 16 -> PASSED!
|
||||
Checking that after a second reset() the thread pool reports a number of threads equal to the hardware concurrency...
|
||||
Expected: 32, obtained: 32 -> PASSED!
|
||||
Checking that after a second reset() the manually counted number of unique thread IDs is equal to the reported number of threads...
|
||||
Expected: 32, obtained: 32 -> PASSED!
|
||||
|
||||
================================
|
||||
Checking that push_task() works:
|
||||
================================
|
||||
Checking that push_task() works for a function with no arguments or return value...
|
||||
-> PASSED!
|
||||
Checking that push_task() works for a function with one argument and no return value...
|
||||
-> PASSED!
|
||||
Checking that push_task() works for a function with two arguments and no return value...
|
||||
-> PASSED!
|
||||
|
||||
=============================
|
||||
Checking that submit() works:
|
||||
=============================
|
||||
Checking that submit() works for a function with no arguments or return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a function with one argument and no return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a function with two arguments and no return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a function with no arguments and a return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a function with one argument and a return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a function with two arguments and a return value...
|
||||
-> PASSED!
|
||||
|
||||
================================================
|
||||
Checking that submitting member functions works:
|
||||
================================================
|
||||
Checking that push_task() works for a member function with no arguments or return value...
|
||||
-> PASSED!
|
||||
Checking that push_task() works for a member function with one argument and no return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a member function with no arguments or return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a member function with one argument and no return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a member function with no arguments and a return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works for a member function with one argument and a return value...
|
||||
-> PASSED!
|
||||
|
||||
======================================================================
|
||||
Checking that submitting member functions from within an object works:
|
||||
======================================================================
|
||||
Checking that push_task() works within an object for a member function with no arguments or return value...
|
||||
-> PASSED!
|
||||
Checking that push_task() works within an object for a member function with one argument and no return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works within an object for a member function with no arguments or return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works within an object for a member function with one argument and no return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works within an object for a member function with no arguments and a return value...
|
||||
-> PASSED!
|
||||
Checking that submit() works within an object for a member function with one argument and a return value...
|
||||
-> PASSED!
|
||||
|
||||
=======================================
|
||||
Checking that wait_for_tasks() works...
|
||||
=======================================
|
||||
Waiting for tasks...
|
||||
-> PASSED!
|
||||
Checking for deadlocks when waiting for tasks...
|
||||
All waiting tasks successfully finished!
|
||||
-> PASSED!
|
||||
Checking that wait_for_tasks_duration() works...
|
||||
Task submitted. Waiting for 10ms...
|
||||
-> PASSED!
|
||||
Waiting for 500ms...
|
||||
-> PASSED!
|
||||
Checking that wait_for_tasks_until() works...
|
||||
Task submitted. Waiting until 10ms from submission time...
|
||||
-> PASSED!
|
||||
Waiting until 500ms from submission time...
|
||||
-> PASSED!
|
||||
|
||||
======================================================
|
||||
Checking that push_loop() and parallelize_loop() work:
|
||||
======================================================
|
||||
Verifying that push_loop() from 117855 to 168463 with 25 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from -788069 to -860364 with 21 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 545486 to 553538 with 15 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 987439 to 166022 with 29 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 843125 to 395220 with 19 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 75069 to 552964 with 3 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 986466 to -642521 with 20 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 994906 to -386703 with 7 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from -574578 to 327232 with 22 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that push_loop() from 632264 to 644863 with 12 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from 450897 to -789636 with 16 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -986029 to -900579 with 24 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -405930 to 299022 with 24 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -545956 to 219212 with 13 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from 462224 to 865745 with 26 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -311718 to 644762 with 28 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -615396 to -267130 with 2 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -510089 to 363393 with 26 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -846318 to -18573 with 11 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from -680422 to -342474 with 23 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from 383147 to -130987 with 10 tasks correctly sums all indices...
|
||||
Expected: 129643515306, obtained: 129643515306 -> PASSED!
|
||||
Verifying that parallelize_loop() from 827793 to 219417 with 18 tasks correctly sums all indices...
|
||||
Expected: 637096822584, obtained: 637096822584 -> PASSED!
|
||||
Verifying that parallelize_loop() from -441740 to -531834 with 12 tasks correctly sums all indices...
|
||||
Expected: -87713266050, obtained: -87713266050 -> PASSED!
|
||||
Verifying that parallelize_loop() from 980942 to 18492 with 2 tasks correctly sums all indices...
|
||||
Expected: 961904290850, obtained: 961904290850 -> PASSED!
|
||||
Verifying that parallelize_loop() from 351318 to 433401 with 8 tasks correctly sums all indices...
|
||||
Expected: 64412007594, obtained: 64412007594 -> PASSED!
|
||||
Verifying that parallelize_loop() from 184385 to 382813 with 30 tasks correctly sums all indices...
|
||||
Expected: 112547766316, obtained: 112547766316 -> PASSED!
|
||||
Verifying that parallelize_loop() from -498480 to 323830 with 10 tasks correctly sums all indices...
|
||||
Expected: -143617263810, obtained: -143617263810 -> PASSED!
|
||||
Verifying that parallelize_loop() from -119493 to 109088 with 25 tasks correctly sums all indices...
|
||||
Expected: -2378613886, obtained: -2378613886 -> PASSED!
|
||||
Verifying that parallelize_loop() from 776258 to 340877 with 6 tasks correctly sums all indices...
|
||||
Expected: 486378918054, obtained: 486378918054 -> PASSED!
|
||||
Verifying that parallelize_loop() from 160863 to 750589 with 3 tasks correctly sums all indices...
|
||||
Expected: 537506352426, obtained: 537506352426 -> PASSED!
|
||||
Verifying that parallelize_loop() with identical start and end indices does nothing...
|
||||
-> PASSED!
|
||||
Trying parallelize_loop() with start and end indices of different types:
|
||||
Verifying that parallelize_loop() from 838162 to 345683 with 13 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Trying the overloads for push_loop() and parallelize_loop() for the case where the first index is equal to 0:
|
||||
Verifying that push_loop() from 0 to 15216 with 30 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from 0 to 239084 with 5 tasks modifies all indices...
|
||||
-> PASSED!
|
||||
Verifying that parallelize_loop() from 0 to -824186 with 18 tasks correctly sums all indices...
|
||||
Expected: -679283386782, obtained: -679283386782 -> PASSED!
|
||||
|
||||
====================================
|
||||
Checking that task monitoring works:
|
||||
====================================
|
||||
Resetting pool to 4 threads.
|
||||
Submitting 12 tasks.
|
||||
After submission, should have: 12 tasks total, 4 tasks running, 8 tasks queued...
|
||||
Result: 12 tasks total, 4 tasks running, 8 tasks queued -> PASSED!
|
||||
Task 3 released.
|
||||
Task 1 released.
|
||||
Task 2 released.
|
||||
Task 0 released.
|
||||
After releasing 4 tasks, should have: 8 tasks total, 4 tasks running, 4 tasks queued...
|
||||
Result: 8 tasks total, 4 tasks running, 4 tasks queued -> PASSED!
|
||||
Task 4 released.
|
||||
Task 6 released.
|
||||
Task 5 released.
|
||||
Task 7 released.
|
||||
After releasing 4 more tasks, should have: 4 tasks total, 4 tasks running, 0 tasks queued...
|
||||
Result: 4 tasks total, 4 tasks running, 0 tasks queued -> PASSED!
|
||||
Task 8 released.
|
||||
Task 11 released.
|
||||
Task 9 released.
|
||||
Task 10 released.
|
||||
After releasing the final 4 tasks, should have: 0 tasks total, 0 tasks running, 0 tasks queued...
|
||||
Result: 0 tasks total, 0 tasks running, 0 tasks queued -> PASSED!
|
||||
Resetting pool to 32 threads.
|
||||
|
||||
============================
|
||||
Checking that pausing works:
|
||||
============================
|
||||
Resetting pool to 4 threads.
|
||||
Checking that the pool correctly reports that it is not paused.
|
||||
-> PASSED!
|
||||
Pausing pool.
|
||||
Checking that the pool correctly reports that it is paused.
|
||||
-> PASSED!
|
||||
Submitting 12 tasks, each one waiting for 200ms.
|
||||
Immediately after submission, should have: 12 tasks total, 0 tasks running, 12 tasks queued...
|
||||
Result: 12 tasks total, 0 tasks running, 12 tasks queued -> PASSED!
|
||||
300ms later, should still have: 12 tasks total, 0 tasks running, 12 tasks queued...
|
||||
Result: 12 tasks total, 0 tasks running, 12 tasks queued -> PASSED!
|
||||
Unpausing pool.
|
||||
Checking that the pool correctly reports that it is not paused.
|
||||
-> PASSED!
|
||||
Task 3 done.
|
||||
Task 2 done.
|
||||
Task 1 done.
|
||||
Task 0 done.
|
||||
300ms later, should have: 8 tasks total, 4 tasks running, 4 tasks queued...
|
||||
Result: 8 tasks total, 4 tasks running, 4 tasks queued -> PASSED!
|
||||
Pausing pool and using wait_for_tasks() to wait for the running tasks.
|
||||
Task 6 done.
|
||||
Task 4 done.
|
||||
Task 7 done.
|
||||
Task 5 done.
|
||||
After waiting, should have: 4 tasks total, 0 tasks running, 4 tasks queued...
|
||||
Result: 4 tasks total, 0 tasks running, 4 tasks queued -> PASSED!
|
||||
200ms later, should still have: 4 tasks total, 0 tasks running, 4 tasks queued...
|
||||
Result: 4 tasks total, 0 tasks running, 4 tasks queued -> PASSED!
|
||||
Unpausing pool and using wait_for_tasks() to wait for all tasks.
|
||||
Task 11 done.
|
||||
Task 9 done.
|
||||
Task 10 done.
|
||||
Task 8 done.
|
||||
After waiting, should have: 0 tasks total, 0 tasks running, 0 tasks queued...
|
||||
Result: 0 tasks total, 0 tasks running, 0 tasks queued -> PASSED!
|
||||
Resetting pool to 32 threads.
|
||||
|
||||
=======================================
|
||||
Checking that exception handling works:
|
||||
=======================================
|
||||
Checking that exceptions are forwarded correctly by submit()...
|
||||
Throwing exception...
|
||||
-> PASSED!
|
||||
Checking that exceptions are forwarded correctly by BS::multi_future...
|
||||
Throwing exception...
|
||||
Throwing exception...
|
||||
-> PASSED!
|
||||
|
||||
============================================================
|
||||
Testing that vector operations produce the expected results:
|
||||
============================================================
|
||||
Adding two vectors with 365392 elements using 9 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 797060 elements using 7 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 159148 elements using 19 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 432461 elements using 3 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 907909 elements using 30 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 854259 elements using 3 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 238088 elements using 2 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 559647 elements using 32 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 473570 elements using 25 tasks...
|
||||
-> PASSED!
|
||||
Adding two vectors with 124722 elements using 9 tasks...
|
||||
-> PASSED!
|
||||
|
||||
++++++++++++++++++++++++++++++
|
||||
SUCCESS: Passed all 93 checks!
|
||||
++++++++++++++++++++++++++++++
|
||||
```
|
||||
|
||||
### Performance tests
|
||||
|
||||
If all checks passed, `BS_thread_pool_test.cpp` will perform simple benchmarks by filling a specific number of vectors of fixed size with values. The program decides how many vectors to use by testing how many are needed to reach a target duration in the single-threaded test. This ensures that the test takes approximately the same amount of time on different systems, and is thus more consistent and portable.
|
||||
If all checks passed, `BS_thread_pool_test.cpp` performs simple benchmarks by filling a specific number of vectors of fixed size with values. The program decides how many vectors to use, and of what size, by testing how many are needed to reach a certain target duration in a single-threaded computation. This ensures that the test takes approximately the same amount of time on all systems, and is thus more consistent and portable.
|
||||
|
||||
Once the required number of vectors has been determined, the program will test the performance of several multi-threaded tests, dividing the total number of vectors into different numbers of tasks, compare them to the performance of the single-threaded test, and indicate the maximum speedup obtained.
|
||||
Once the appropriate number and size of vectors has been determined, the program allocates the vectors and fills them 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.
|
||||
|
||||
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 run times of the tests are compared, and the maximum speedup obtained is calculated.
|
||||
|
||||
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 v12.2.0 with the `-O3` and `-march=native` flags. The output was as follows:
|
||||
|
||||
@@ -1479,27 +1270,25 @@ Performing benchmarks:
|
||||
Using 80 threads.
|
||||
Each test will be repeated 20 times to collect reliable statistics.
|
||||
Determining the number and size of vectors to generate in order to achieve an approximate mean execution time of 50 ms with 80 tasks...
|
||||
Generating 4000 vectors with 5120 elements each:
|
||||
Single-threaded, mean execution time was 2211.9 ms with standard deviation 39.1 ms.
|
||||
With 20 tasks, mean execution time was 128.8 ms with standard deviation 12.8 ms.
|
||||
With 40 tasks, mean execution time was 71.6 ms with standard deviation 1.1 ms.
|
||||
With 80 tasks, mean execution time was 44.4 ms with standard deviation 5.0 ms.
|
||||
With 160 tasks, mean execution time was 47.0 ms with standard deviation 6.4 ms.
|
||||
With 320 tasks, mean execution time was 132.6 ms with standard deviation 2.2 ms.
|
||||
Maximum speedup obtained by multithreading vs. single-threading: 49.8x, using 80 tasks.
|
||||
Generating 3840 vectors with 5120 elements each:
|
||||
Single-threaded, mean execution time was 2122.5 ms with standard deviation 18.1 ms.
|
||||
With 20 tasks, mean execution time was 119.8 ms with standard deviation 13.3 ms.
|
||||
With 40 tasks, mean execution time was 68.9 ms with standard deviation 0.3 ms.
|
||||
With 80 tasks, mean execution time was 45.5 ms with standard deviation 6.2 ms.
|
||||
With 160 tasks, mean execution time was 39.3 ms with standard deviation 3.3 ms.
|
||||
With 320 tasks, mean execution time was 40.4 ms with standard deviation 2.4 ms.
|
||||
Maximum speedup obtained by multithreading vs. single-threading: 53.9x, using 160 tasks.
|
||||
|
||||
+++++++++++++++++++++++++++++++++++++++
|
||||
Thread pool performance test completed!
|
||||
+++++++++++++++++++++++++++++++++++++++
|
||||
```
|
||||
|
||||
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 49.8x in our performance test is very close to this estimate.
|
||||
|
||||
In addition, this test demonstrates that splitting the loop into a number of tasks to be equal to the number of hardware threads usually yields optimal results, since all the tasks can be run in parallel. When we used less than 80 tasks, not all of the CPU cores were taken advantage of, and when we used more than 80 tasks, they had to run in more than one batch instead of all at once, introducing additional overhead.
|
||||
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 53.9x in our performance test exceeds this estimate.
|
||||
|
||||
## The light version of the package
|
||||
|
||||
This package started out as a very lightweight C++ thread pool, but over time has expanded to include many additional features and helper classes. Therefore, I have decided to bundle a light version of the thread pool in a separate and stand-alone header file, `BS_thread_pool_light.hpp`, which is about half the size of the full package.
|
||||
This package started out as a very lightweight C++ thread pool, but over time has expanded to include many additional features and helper classes. Therefore, I have decided to bundle a light version of the thread pool in a separate and stand-alone header file, `BS_thread_pool_light.hpp`.
|
||||
|
||||
This file does not contain any of the helper classes, only a new `BS::thread_pool_light` class, which is a minimal thread pool with only the 5 most basic member functions:
|
||||
|
||||
@@ -1509,11 +1298,9 @@ This file does not contain any of the helper classes, only a new `BS::thread_poo
|
||||
* `submit()`
|
||||
* `wait_for_tasks()`
|
||||
|
||||
A separate test program `BS_thread_pool_light_test.cpp` tests only the features of the lightweight `BS::thread_pool_light` class. In the spirit of minimalism, it does not generate a log file and does not do any benchmarks.
|
||||
Note that each header file is 100% stand-alone. If you wish to use the full package, you only need `BS_thread_pool.hpp`, and if you wish to use the light version, you only need `BS_thread_pool_light.hpp`. Only a single header file needs to be included in your project. However, if you wish to use both the light and non-light thread pool classes in the same project, you can include both header files.
|
||||
|
||||
To be perfectly clear, each header file is 100% stand-alone. If you wish to use the full package, you only need `BS_thread_pool.hpp`, and if you wish to use the light version, you only need `BS_thread_pool_light.hpp`. Only a single header file needs to be included in your project. However, if you wish to use both the light and non-light thread pool classes in the same project, you can include both header files.
|
||||
|
||||
If needed, the current version of the light thread pool can be obtained using the macro `BS_THREAD_POOL_LIGHT_VERSION`.
|
||||
The test program `BS_thread_pool_test.cpp` tests both the full and the light versions of the package. If needed, the current version of the light thread pool can be obtained using the macro `BS_THREAD_POOL_LIGHT_VERSION`.
|
||||
|
||||
## About the project
|
||||
|
||||
@@ -1560,3 +1347,7 @@ You can use the following BibTeX entry:
|
||||
```
|
||||
|
||||
Please note that the [companion paper on arXiv](https://arxiv.org/abs/2105.00613) is updated infrequently. The paper is intended to facilitate discovery of the package by scientists who may find it useful for scientific computing purposes and to allow citing the package in scientific research, but most users should read the `README.md` file on [the GitHub repository](https://github.com/bshoshany/thread-pool) instead, as it is guaranteed to always be up to date.
|
||||
|
||||
### Learning more about C++
|
||||
|
||||
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.
|
||||
|
||||
@@ -3,16 +3,15 @@
|
||||
/**
|
||||
* @file BS_thread_pool.hpp
|
||||
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||
* @version 3.4.0
|
||||
* @date 2023-05-12
|
||||
* @version 3.5.0
|
||||
* @date 2023-05-25
|
||||
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. If you use this library in published research, please cite it as follows: Barak Shoshany, "A C++17 Thread Pool for High-Performance Scientific Computing", doi:10.5281/zenodo.4742687, arXiv:2105.00613 (May 2021)
|
||||
*
|
||||
* @brief BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This header file contains the entire library, including the main BS::thread_pool class and the helper classes BS::multi_future, BS::blocks, BS:synced_stream, and BS::timer.
|
||||
*/
|
||||
|
||||
#define BS_THREAD_POOL_VERSION "v3.4.0 (2023-05-12)"
|
||||
#define BS_THREAD_POOL_VERSION "v3.5.0 (2023-05-25)"
|
||||
|
||||
#include <atomic> // std::atomic
|
||||
#include <chrono> // std::chrono
|
||||
#include <condition_variable> // std::condition_variable
|
||||
#include <exception> // std::current_exception
|
||||
@@ -286,17 +285,18 @@ public:
|
||||
[[nodiscard]] size_t get_tasks_running() const
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
return tasks_total - tasks.size();
|
||||
return tasks_running;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get 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().
|
||||
* @brief 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().
|
||||
*
|
||||
* @return The total number of tasks.
|
||||
*/
|
||||
[[nodiscard]] size_t get_tasks_total() const
|
||||
{
|
||||
return tasks_total;
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
return tasks_running + tasks.size();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,6 +316,7 @@ public:
|
||||
*/
|
||||
[[nodiscard]] bool is_paused() const
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
return paused;
|
||||
}
|
||||
|
||||
@@ -372,9 +373,20 @@ public:
|
||||
*/
|
||||
void pause()
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
paused = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
void purge()
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
while (!tasks.empty())
|
||||
tasks.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. Does not return a multi_future, so the user must use wait_for_tasks() or some other method to ensure that the loop finishes executing, otherwise bad things will happen.
|
||||
*
|
||||
@@ -424,11 +436,9 @@ public:
|
||||
template <typename F, typename... A>
|
||||
void push_task(F&& task, A&&... args)
|
||||
{
|
||||
std::function<void()> task_function = std::bind(std::forward<F>(task), std::forward<A>(args)...);
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
tasks.push(task_function);
|
||||
++tasks_total;
|
||||
tasks.push(std::bind(std::forward<F>(task), std::forward<A>(args)...)); // cppcheck-suppress ignoredReturnValue
|
||||
}
|
||||
task_available_cv.notify_one();
|
||||
}
|
||||
@@ -440,8 +450,10 @@ public:
|
||||
*/
|
||||
void reset(const concurrency_t thread_count_ = 0)
|
||||
{
|
||||
std::unique_lock tasks_lock(tasks_mutex);
|
||||
const bool was_paused = paused;
|
||||
paused = true;
|
||||
tasks_lock.unlock();
|
||||
wait_for_tasks();
|
||||
destroy_threads();
|
||||
thread_count = determine_thread_count(thread_count_);
|
||||
@@ -463,10 +475,9 @@ public:
|
||||
template <typename F, typename... A, typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>
|
||||
[[nodiscard]] std::future<R> submit(F&& task, A&&... args)
|
||||
{
|
||||
std::function<R()> task_function = std::bind(std::forward<F>(task), std::forward<A>(args)...);
|
||||
std::shared_ptr<std::promise<R>> task_promise = std::make_shared<std::promise<R>>();
|
||||
push_task(
|
||||
[task_function, task_promise]
|
||||
[task_function = std::bind(std::forward<F>(task), std::forward<A>(args)...), task_promise]
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -499,6 +510,7 @@ public:
|
||||
*/
|
||||
void unpause()
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
paused = false;
|
||||
}
|
||||
|
||||
@@ -507,13 +519,10 @@ public:
|
||||
*/
|
||||
void wait_for_tasks()
|
||||
{
|
||||
if (!waiting)
|
||||
{
|
||||
waiting = true;
|
||||
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
|
||||
task_done_cv.wait(tasks_lock, [this] { return (tasks_total == (paused ? tasks.size() : 0)); });
|
||||
waiting = false;
|
||||
}
|
||||
std::unique_lock tasks_lock(tasks_mutex);
|
||||
waiting = true;
|
||||
tasks_done_cv.wait(tasks_lock, [this] { return !tasks_running && (paused || tasks.empty()); });
|
||||
waiting = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -522,20 +531,16 @@ public:
|
||||
* @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.
|
||||
* @param duration The time duration to wait.
|
||||
* @return true if finished waiting before the duration expired, false if timed out or the pool is already waiting. In other words, returns false if and only if tasks are still running.
|
||||
* @return true if all tasks finished running, false if the duration expired but some tasks are still running.
|
||||
*/
|
||||
template <typename R, typename P>
|
||||
bool wait_for_tasks_duration(const std::chrono::duration<R, P>& duration)
|
||||
{
|
||||
if (!waiting)
|
||||
{
|
||||
waiting = true;
|
||||
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
|
||||
const bool status = task_done_cv.wait_for(tasks_lock, duration, [this] { return (tasks_total == (paused ? tasks.size() : 0)); });
|
||||
waiting = false;
|
||||
return status;
|
||||
}
|
||||
return false;
|
||||
std::unique_lock tasks_lock(tasks_mutex);
|
||||
waiting = true;
|
||||
const bool status = tasks_done_cv.wait_for(tasks_lock, duration, [this] { return !tasks_running && (paused || tasks.empty()); });
|
||||
waiting = false;
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -544,20 +549,16 @@ public:
|
||||
* @tparam C The type of the clock used to measure time.
|
||||
* @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 finished waiting before the time point was reached, false if timed out or the pool is already waiting. In other words, returns false if and only if tasks are still running.
|
||||
* @return true if all tasks finished running, false if the time point was reached but some tasks are still running.
|
||||
*/
|
||||
template <typename C, typename D>
|
||||
bool wait_for_tasks_until(const std::chrono::time_point<C, D>& timeout_time)
|
||||
{
|
||||
if (!waiting)
|
||||
{
|
||||
waiting = true;
|
||||
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
|
||||
const bool status = task_done_cv.wait_until(tasks_lock, timeout_time, [this] { return (tasks_total == (paused ? tasks.size() : 0)); });
|
||||
waiting = false;
|
||||
return status;
|
||||
}
|
||||
return false;
|
||||
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 && (paused || tasks.empty()); });
|
||||
waiting = false;
|
||||
return status;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -570,7 +571,10 @@ private:
|
||||
*/
|
||||
void create_threads()
|
||||
{
|
||||
running = true;
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
workers_running = true;
|
||||
}
|
||||
for (concurrency_t i = 0; i < thread_count; ++i)
|
||||
{
|
||||
threads[i] = std::thread(&thread_pool::worker, this);
|
||||
@@ -582,11 +586,11 @@ private:
|
||||
*/
|
||||
void destroy_threads()
|
||||
{
|
||||
running = false;
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
task_available_cv.notify_all();
|
||||
workers_running = false;
|
||||
}
|
||||
task_available_cv.notify_all();
|
||||
for (concurrency_t i = 0; i < thread_count; ++i)
|
||||
{
|
||||
threads[i].join();
|
||||
@@ -599,7 +603,7 @@ private:
|
||||
* @param thread_count_ 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 a non-positive number 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]] concurrency_t determine_thread_count(const concurrency_t thread_count_)
|
||||
[[nodiscard]] concurrency_t determine_thread_count(const concurrency_t thread_count_) const
|
||||
{
|
||||
if (thread_count_ > 0)
|
||||
return thread_count_;
|
||||
@@ -617,22 +621,24 @@ private:
|
||||
*/
|
||||
void worker()
|
||||
{
|
||||
while (running)
|
||||
std::function<void()> task;
|
||||
while (true)
|
||||
{
|
||||
std::function<void()> task;
|
||||
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
|
||||
task_available_cv.wait(tasks_lock, [this] { return !tasks.empty() || !running; });
|
||||
if (running && !paused)
|
||||
{
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
tasks_lock.unlock();
|
||||
task();
|
||||
tasks_lock.lock();
|
||||
--tasks_total;
|
||||
if (waiting)
|
||||
task_done_cv.notify_one();
|
||||
}
|
||||
std::unique_lock tasks_lock(tasks_mutex);
|
||||
task_available_cv.wait(tasks_lock, [this] { return !tasks.empty() || !workers_running; });
|
||||
if (!workers_running)
|
||||
break;
|
||||
if (paused)
|
||||
continue;
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
++tasks_running;
|
||||
tasks_lock.unlock();
|
||||
task();
|
||||
tasks_lock.lock();
|
||||
--tasks_running;
|
||||
if (waiting && !tasks_running && (paused || tasks.empty()))
|
||||
tasks_done_cv.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -641,24 +647,19 @@ private:
|
||||
// ============
|
||||
|
||||
/**
|
||||
* @brief An atomic variable 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 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.
|
||||
*/
|
||||
std::atomic<bool> paused = false;
|
||||
bool paused = false;
|
||||
|
||||
/**
|
||||
* @brief An atomic variable indicating to the workers to keep running. When set to false, the workers permanently stop working.
|
||||
*/
|
||||
std::atomic<bool> running = false;
|
||||
|
||||
/**
|
||||
* @brief A condition variable used to notify worker() that a new task has become available.
|
||||
* @brief A condition variable to notify worker() that a new task has become available.
|
||||
*/
|
||||
std::condition_variable task_available_cv = {};
|
||||
|
||||
/**
|
||||
* @brief A condition variable used to notify wait_for_tasks() that a tasks is done.
|
||||
* @brief A condition variable to notify wait_for_tasks() that the tasks are done.
|
||||
*/
|
||||
std::condition_variable task_done_cv = {};
|
||||
std::condition_variable tasks_done_cv = {};
|
||||
|
||||
/**
|
||||
* @brief A queue of tasks to be executed by the threads.
|
||||
@@ -666,9 +667,9 @@ private:
|
||||
std::queue<std::function<void()>> tasks = {};
|
||||
|
||||
/**
|
||||
* @brief An atomic variable to keep track of the total number of unfinished tasks - either still in the queue, or running in a thread.
|
||||
* @brief A counter for the total number of currently running tasks.
|
||||
*/
|
||||
std::atomic<size_t> tasks_total = 0;
|
||||
size_t tasks_running = 0;
|
||||
|
||||
/**
|
||||
* @brief A mutex to synchronize access to the task queue by different threads.
|
||||
@@ -686,9 +687,14 @@ private:
|
||||
std::unique_ptr<std::thread[]> threads = nullptr;
|
||||
|
||||
/**
|
||||
* @brief An atomic variable indicating that wait_for_tasks() is active and expects to be notified whenever a task is done.
|
||||
* @brief A flag indicating that wait_for_tasks() is active and expects to be notified whenever a task is done.
|
||||
*/
|
||||
std::atomic<bool> waiting = false;
|
||||
bool waiting = false;
|
||||
|
||||
/**
|
||||
* @brief A flag indicating to the workers to keep running. When set to false, the workers terminate permanently.
|
||||
*/
|
||||
bool workers_running = false;
|
||||
};
|
||||
|
||||
// End class thread_pool //
|
||||
@@ -3,16 +3,15 @@
|
||||
/**
|
||||
* @file BS_thread_pool_light.hpp
|
||||
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||
* @version 3.4.0
|
||||
* @date 2023-05-12
|
||||
* @version 3.5.0
|
||||
* @date 2023-05-25
|
||||
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. If you use this library in published research, please cite it as follows: Barak Shoshany, "A C++17 Thread Pool for High-Performance Scientific Computing", doi:10.5281/zenodo.4742687, arXiv:2105.00613 (May 2021)
|
||||
*
|
||||
* @brief BS::thread_pool_light: a fast, lightweight, and easy-to-use C++17 thread pool library. This header file contains a light version of the main library, for use when advanced features are not needed.
|
||||
*/
|
||||
|
||||
#define BS_THREAD_POOL_LIGHT_VERSION "v3.4.0 (2023-05-12)"
|
||||
#define BS_THREAD_POOL_LIGHT_VERSION "v3.5.0 (2023-05-25)"
|
||||
|
||||
#include <atomic> // std::atomic
|
||||
#include <condition_variable> // std::condition_variable
|
||||
#include <exception> // std::current_exception
|
||||
#include <functional> // std::bind, std::function, std::invoke
|
||||
@@ -135,11 +134,9 @@ public:
|
||||
template <typename F, typename... A>
|
||||
void push_task(F&& task, A&&... args)
|
||||
{
|
||||
std::function<void()> task_function = std::bind(std::forward<F>(task), std::forward<A>(args)...);
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
tasks.push(task_function);
|
||||
++tasks_total;
|
||||
tasks.push(std::bind(std::forward<F>(task), std::forward<A>(args)...)); // cppcheck-suppress ignoredReturnValue
|
||||
}
|
||||
task_available_cv.notify_one();
|
||||
}
|
||||
@@ -157,10 +154,9 @@ public:
|
||||
template <typename F, typename... A, typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>
|
||||
[[nodiscard]] std::future<R> submit(F&& task, A&&... args)
|
||||
{
|
||||
std::function<R()> task_function = std::bind(std::forward<F>(task), std::forward<A>(args)...);
|
||||
std::shared_ptr<std::promise<R>> task_promise = std::make_shared<std::promise<R>>();
|
||||
push_task(
|
||||
[task_function, task_promise]
|
||||
[task_function = std::bind(std::forward<F>(task), std::forward<A>(args)...), task_promise]
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -189,17 +185,14 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @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. Note: To wait for just one specific task, use submit() instead, and call the wait() member function of the generated future.
|
||||
* @brief Wait for tasks to be completed, both those that are currently running in the threads and those that are still waiting in the queue. Note: To wait for just one specific task, use submit() instead, and call the wait() member function of the generated future.
|
||||
*/
|
||||
void wait_for_tasks()
|
||||
{
|
||||
if (!waiting)
|
||||
{
|
||||
waiting = true;
|
||||
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
|
||||
task_done_cv.wait(tasks_lock, [this] { return (tasks_total == 0); });
|
||||
waiting = false;
|
||||
}
|
||||
std::unique_lock tasks_lock(tasks_mutex);
|
||||
waiting = true;
|
||||
tasks_done_cv.wait(tasks_lock, [this] { return !tasks_running && tasks.empty(); });
|
||||
waiting = false;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -212,7 +205,10 @@ private:
|
||||
*/
|
||||
void create_threads()
|
||||
{
|
||||
running = true;
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
workers_running = true;
|
||||
}
|
||||
for (concurrency_t i = 0; i < thread_count; ++i)
|
||||
{
|
||||
threads[i] = std::thread(&thread_pool_light::worker, this);
|
||||
@@ -224,11 +220,11 @@ private:
|
||||
*/
|
||||
void destroy_threads()
|
||||
{
|
||||
running = false;
|
||||
{
|
||||
const std::scoped_lock tasks_lock(tasks_mutex);
|
||||
task_available_cv.notify_all();
|
||||
workers_running = false;
|
||||
}
|
||||
task_available_cv.notify_all();
|
||||
for (concurrency_t i = 0; i < thread_count; ++i)
|
||||
{
|
||||
threads[i].join();
|
||||
@@ -241,7 +237,7 @@ private:
|
||||
* @param thread_count_ The parameter passed to the constructor. 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 a non-positive number 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]] concurrency_t determine_thread_count(const concurrency_t thread_count_)
|
||||
[[nodiscard]] concurrency_t determine_thread_count(const concurrency_t thread_count_) const
|
||||
{
|
||||
if (thread_count_ > 0)
|
||||
return thread_count_;
|
||||
@@ -259,22 +255,22 @@ private:
|
||||
*/
|
||||
void worker()
|
||||
{
|
||||
while (running)
|
||||
std::function<void()> task;
|
||||
while (true)
|
||||
{
|
||||
std::function<void()> task;
|
||||
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
|
||||
task_available_cv.wait(tasks_lock, [this] { return !tasks.empty() || !running; });
|
||||
if (running)
|
||||
{
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
tasks_lock.unlock();
|
||||
task();
|
||||
tasks_lock.lock();
|
||||
--tasks_total;
|
||||
if (waiting)
|
||||
task_done_cv.notify_one();
|
||||
}
|
||||
std::unique_lock tasks_lock(tasks_mutex);
|
||||
task_available_cv.wait(tasks_lock, [this] { return !tasks.empty() || !workers_running; });
|
||||
if (!workers_running)
|
||||
break;
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
++tasks_running;
|
||||
tasks_lock.unlock();
|
||||
task();
|
||||
tasks_lock.lock();
|
||||
--tasks_running;
|
||||
if (waiting && !tasks_running && tasks.empty())
|
||||
tasks_done_cv.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,19 +279,14 @@ private:
|
||||
// ============
|
||||
|
||||
/**
|
||||
* @brief An atomic variable indicating to the workers to keep running. When set to false, the workers permanently stop working.
|
||||
*/
|
||||
std::atomic<bool> running = false;
|
||||
|
||||
/**
|
||||
* @brief A condition variable used to notify worker() that a new task has become available.
|
||||
* @brief A condition variable to notify worker() that a new task has become available.
|
||||
*/
|
||||
std::condition_variable task_available_cv = {};
|
||||
|
||||
/**
|
||||
* @brief A condition variable used to notify wait_for_tasks() that a tasks is done.
|
||||
* @brief A condition variable to notify wait_for_tasks() that the tasks are done.
|
||||
*/
|
||||
std::condition_variable task_done_cv = {};
|
||||
std::condition_variable tasks_done_cv = {};
|
||||
|
||||
/**
|
||||
* @brief A queue of tasks to be executed by the threads.
|
||||
@@ -303,9 +294,9 @@ private:
|
||||
std::queue<std::function<void()>> tasks = {};
|
||||
|
||||
/**
|
||||
* @brief An atomic variable to keep track of the total number of unfinished tasks - either still in the queue, or running in a thread.
|
||||
* @brief A counter for the total number of currently running tasks.
|
||||
*/
|
||||
std::atomic<size_t> tasks_total = 0;
|
||||
size_t tasks_running = 0;
|
||||
|
||||
/**
|
||||
* @brief A mutex to synchronize access to the task queue by different threads.
|
||||
@@ -323,9 +314,14 @@ private:
|
||||
std::unique_ptr<std::thread[]> threads = nullptr;
|
||||
|
||||
/**
|
||||
* @brief An atomic variable indicating that wait_for_tasks() is active and expects to be notified whenever a task is done.
|
||||
* @brief A flag indicating that wait_for_tasks() is active and expects to be notified whenever a task is done.
|
||||
*/
|
||||
std::atomic<bool> waiting = false;
|
||||
bool waiting = false;
|
||||
|
||||
/**
|
||||
* @brief A flag indicating to the workers to keep running. When set to false, the workers terminate permanently.
|
||||
*/
|
||||
bool workers_running = false;
|
||||
};
|
||||
|
||||
} // namespace BS
|
||||
@@ -1,15 +1,15 @@
|
||||
/**
|
||||
* @file BS_thread_pool_test.cpp
|
||||
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||
* @version 3.4.0
|
||||
* @date 2023-05-12
|
||||
* @version 3.5.0
|
||||
* @date 2023-05-25
|
||||
* @copyright Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license. If you found this project useful, please consider starring it on GitHub! If you use this library in software of any kind, please provide a link to the GitHub repository https://github.com/bshoshany/thread-pool in the source code and documentation. If you use this library in published research, please cite it as follows: Barak Shoshany, "A C++17 Thread Pool for High-Performance Scientific Computing", doi:10.5281/zenodo.4742687, arXiv:2105.00613 (May 2021)
|
||||
*
|
||||
* @brief BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library. This program tests all aspects of the library, but is not needed in order to use the library.
|
||||
*/
|
||||
|
||||
// Get rid of annoying MSVC warning.
|
||||
#ifdef _MSC_VER
|
||||
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <ctime> // std::localtime, std::strftime, std::time, std::time_t
|
||||
#include <exception> // std::exception
|
||||
#include <fstream> // std::ofstream
|
||||
#include <functional> // std::cref, std::ref
|
||||
#include <future> // std::future
|
||||
#include <iomanip> // std::setprecision, std::setw
|
||||
#include <ios> // std::fixed
|
||||
@@ -32,11 +33,13 @@
|
||||
#include <stdexcept> // std::runtime_error
|
||||
#include <string> // std::string, std::to_string
|
||||
#include <thread> // std::this_thread, std::thread
|
||||
#include <type_traits> // std::is_same_v
|
||||
#include <utility> // std::forward, std::pair
|
||||
#include <vector> // std::begin, std::end, std::vector
|
||||
|
||||
// Include the header file for the thread pool library.
|
||||
#include "BS_thread_pool.hpp"
|
||||
// Include the header files for the thread pool library.
|
||||
#include "../include/BS_thread_pool.hpp"
|
||||
#include "../include/BS_thread_pool_light.hpp"
|
||||
|
||||
// ================
|
||||
// Global variables
|
||||
@@ -63,8 +66,9 @@ std::ofstream log_file;
|
||||
// A global synced_stream object which prints to the log file.
|
||||
BS::synced_stream sync_file(log_file);
|
||||
|
||||
// A global thread pool object to be used throughout the test.
|
||||
BS::thread_pool pool;
|
||||
// Global thread pool objects to be used throughout the test.
|
||||
BS::thread_pool pool_full;
|
||||
BS::thread_pool_light pool_light;
|
||||
|
||||
// A global random_device object to be used to seed some random number generators.
|
||||
std::random_device rd;
|
||||
@@ -178,8 +182,12 @@ void check(const T1 expected, const T2 obtained)
|
||||
|
||||
/**
|
||||
* @brief Count the number of unique threads in the pool. Submits a number of tasks equal to twice the thread count into the pool. Each task stores the ID of the thread running it, and then waits until released by the main thread. This ensures that each thread in the pool runs at least one task. The number of unique thread IDs is then counted from the stored IDs.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
BS::concurrency_t count_unique_threads()
|
||||
template <typename P>
|
||||
BS::concurrency_t count_unique_threads(P& pool)
|
||||
{
|
||||
std::condition_variable ID_cv, total_cv;
|
||||
std::mutex ID_mutex, total_mutex;
|
||||
@@ -203,7 +211,7 @@ BS::concurrency_t count_unique_threads()
|
||||
std::unique_lock<std::mutex> ID_lock_local(ID_mutex);
|
||||
ID_cv.wait(ID_lock_local, [&ID_release] { return ID_release; });
|
||||
});
|
||||
total_cv.wait(total_lock, [&total_count] { return total_count == pool.get_thread_count(); });
|
||||
total_cv.wait(total_lock, [&total_count, &pool] { return total_count == pool.get_thread_count(); });
|
||||
{
|
||||
const std::scoped_lock ID_lock(ID_mutex);
|
||||
ID_release = true;
|
||||
@@ -218,30 +226,36 @@ BS::concurrency_t count_unique_threads()
|
||||
|
||||
/**
|
||||
* @brief Check that the constructor works.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_constructor()
|
||||
template <typename P>
|
||||
void check_constructor(P& pool)
|
||||
{
|
||||
dual_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...");
|
||||
check(pool.get_thread_count(), count_unique_threads());
|
||||
check(pool.get_thread_count(), count_unique_threads(pool));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that reset() works.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_reset()
|
||||
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 half 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...");
|
||||
check(pool.get_thread_count(), count_unique_threads());
|
||||
check(pool.get_thread_count(), count_unique_threads(pool));
|
||||
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...");
|
||||
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...");
|
||||
check(pool.get_thread_count(), count_unique_threads());
|
||||
check(pool.get_thread_count(), count_unique_threads(pool));
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -294,33 +308,37 @@ void check_deadlock(const F&& task)
|
||||
check(passed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that the destructor does not create deadlocks.
|
||||
*/
|
||||
void check_destructor_deadlock()
|
||||
{
|
||||
dual_println("Checking for destruction deadlocks...");
|
||||
check_deadlock([] { BS::thread_pool temp_pool; });
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that reset() does not create deadlocks.
|
||||
*/
|
||||
void check_reset_deadlock()
|
||||
{
|
||||
dual_println("Checking for reset deadlocks...");
|
||||
BS::thread_pool temp_pool;
|
||||
check_deadlock([&temp_pool] { temp_pool.reset(); });
|
||||
}
|
||||
|
||||
// =======================================
|
||||
// Functions to verify submission of tasks
|
||||
// =======================================
|
||||
|
||||
/**
|
||||
* @brief Check that push_task() works.
|
||||
* @brief A class to detect when a copy or move constructor has been invoked.
|
||||
*/
|
||||
void check_push_task()
|
||||
class detect_copy_move
|
||||
{
|
||||
public:
|
||||
detect_copy_move() = default;
|
||||
detect_copy_move(const detect_copy_move&)
|
||||
{
|
||||
copied = true;
|
||||
}
|
||||
detect_copy_move(detect_copy_move&&)
|
||||
{
|
||||
moved = true;
|
||||
}
|
||||
bool copied = false;
|
||||
bool moved = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Check that push_task() works.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
template <typename P>
|
||||
void check_push_task(P& pool)
|
||||
{
|
||||
dual_println("Checking that push_task() works for a function with no arguments or return value...");
|
||||
{
|
||||
@@ -344,12 +362,60 @@ void check_push_task()
|
||||
pool.wait_for_tasks();
|
||||
check(flag1 && flag2);
|
||||
}
|
||||
dual_println("Checking that push_task() does not create unnecessary copies of the function object...");
|
||||
{
|
||||
bool copied = false;
|
||||
bool moved = false;
|
||||
pool.push_task(
|
||||
[detect = detect_copy_move(), &copied, &moved]
|
||||
{
|
||||
copied = detect.copied;
|
||||
moved = detect.moved;
|
||||
});
|
||||
pool.wait_for_tasks();
|
||||
check(!copied && moved);
|
||||
}
|
||||
dual_println("Checking that push_task() correctly accepts arguments passed by value, reference, and constant reference...");
|
||||
{
|
||||
int64_t pass_me_by_value = 0;
|
||||
pool.push_task(
|
||||
[](int64_t passed_by_value)
|
||||
{
|
||||
if (++passed_by_value)
|
||||
std::this_thread::yield();
|
||||
},
|
||||
pass_me_by_value);
|
||||
pool.wait_for_tasks();
|
||||
check(pass_me_by_value == 0);
|
||||
int64_t pass_me_by_ref = 0;
|
||||
pool.push_task([](int64_t& passed_by_ref) { ++passed_by_ref; }, std::ref(pass_me_by_ref));
|
||||
pool.wait_for_tasks();
|
||||
check(pass_me_by_ref == 1);
|
||||
int64_t pass_me_by_cref = 0;
|
||||
std::atomic<bool> release = false;
|
||||
pool.push_task(
|
||||
[&release](const int64_t& passed_by_cref)
|
||||
{
|
||||
while (!release)
|
||||
std::this_thread::yield();
|
||||
check(passed_by_cref == 1);
|
||||
},
|
||||
std::cref(pass_me_by_cref));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
++pass_me_by_cref;
|
||||
release = true;
|
||||
pool.wait_for_tasks();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that submit() works.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_submit()
|
||||
template <typename P>
|
||||
void check_submit(P& pool)
|
||||
{
|
||||
dual_println("Checking that submit() works for a function with no arguments or return value...");
|
||||
{
|
||||
@@ -406,11 +472,55 @@ void check_submit()
|
||||
&flag1, &flag2);
|
||||
check(flag_future.get() == 42 && flag1 && flag2);
|
||||
}
|
||||
dual_println("Checking that submit() does not create unnecessary copies of the function object...");
|
||||
{
|
||||
std::future<std::pair<bool, bool>> fut = pool.submit([detect = detect_copy_move()] { return std::pair(detect.copied, detect.moved); });
|
||||
std::pair<bool, bool> result = fut.get();
|
||||
check(!result.first && result.second);
|
||||
}
|
||||
dual_println("Checking that submit() correctly accepts arguments passed by value, reference, and constant reference...");
|
||||
{
|
||||
int64_t pass_me_by_value = 0;
|
||||
pool.submit(
|
||||
[](int64_t passed_by_value)
|
||||
{
|
||||
if (++passed_by_value)
|
||||
std::this_thread::yield();
|
||||
},
|
||||
pass_me_by_value)
|
||||
.wait();
|
||||
check(pass_me_by_value == 0);
|
||||
int64_t pass_me_by_ref = 0;
|
||||
pool.submit([](int64_t& passed_by_ref) { ++passed_by_ref; }, std::ref(pass_me_by_ref)).wait();
|
||||
check(pass_me_by_ref == 1);
|
||||
int64_t pass_me_by_cref = 0;
|
||||
std::atomic<bool> release = false;
|
||||
std::future<void> fut = pool.submit(
|
||||
[&release](const int64_t& passed_by_cref)
|
||||
{
|
||||
while (!release)
|
||||
std::this_thread::yield();
|
||||
check(passed_by_cref == 1);
|
||||
},
|
||||
std::cref(pass_me_by_cref));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
++pass_me_by_cref;
|
||||
release = true;
|
||||
fut.wait();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief A class to facilitate checking that member functions of different types have been successfully submitted.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
*/
|
||||
template <typename P>
|
||||
class flag_class
|
||||
{
|
||||
public:
|
||||
flag_class(P& pool_) : pool(pool_) {}
|
||||
|
||||
void set_flag_no_args()
|
||||
{
|
||||
flag = true;
|
||||
@@ -433,139 +543,152 @@ public:
|
||||
return 42;
|
||||
}
|
||||
|
||||
bool get_flag() const
|
||||
[[nodiscard]] bool get_flag() const
|
||||
{
|
||||
return flag;
|
||||
}
|
||||
|
||||
void push_test_flag_no_args()
|
||||
{
|
||||
pool.push_task(&flag_class::set_flag_no_args, this);
|
||||
pool.push_task(&flag_class<P>::set_flag_no_args, this);
|
||||
pool.wait_for_tasks();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void push_test_flag_one_arg()
|
||||
{
|
||||
pool.push_task(&flag_class::set_flag_one_arg, this, true);
|
||||
pool.push_task(&flag_class<P>::set_flag_one_arg, this, true);
|
||||
pool.wait_for_tasks();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_no_args()
|
||||
{
|
||||
pool.submit(&flag_class::set_flag_no_args, this).wait();
|
||||
pool.submit(&flag_class<P>::set_flag_no_args, this).wait();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_one_arg()
|
||||
{
|
||||
pool.submit(&flag_class::set_flag_one_arg, this, true).wait();
|
||||
pool.submit(&flag_class<P>::set_flag_one_arg, this, true).wait();
|
||||
check(get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_no_args_return()
|
||||
{
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_no_args_return, this);
|
||||
std::future<int> flag_future = pool.submit(&flag_class<P>::set_flag_no_args_return, this);
|
||||
check(flag_future.get() == 42 && get_flag());
|
||||
}
|
||||
|
||||
void submit_test_flag_one_arg_return()
|
||||
{
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_one_arg_return, this, true);
|
||||
std::future<int> flag_future = pool.submit(&flag_class<P>::set_flag_one_arg_return, this, true);
|
||||
check(flag_future.get() == 42 && get_flag());
|
||||
}
|
||||
|
||||
private:
|
||||
bool flag = false;
|
||||
P& pool;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Check that submitting member functions works.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_member_function()
|
||||
template <typename P>
|
||||
void check_member_function(P& pool)
|
||||
{
|
||||
dual_println("Checking that push_task() works for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.push_task(&flag_class::set_flag_no_args, &flag);
|
||||
flag_class<P> flag(pool);
|
||||
pool.push_task(&flag_class<P>::set_flag_no_args, &flag);
|
||||
pool.wait_for_tasks();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
dual_println("Checking that push_task() works for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.push_task(&flag_class::set_flag_one_arg, &flag, true);
|
||||
flag_class<P> flag(pool);
|
||||
pool.push_task(&flag_class<P>::set_flag_one_arg, &flag, true);
|
||||
pool.wait_for_tasks();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
dual_println("Checking that submit() works for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.submit(&flag_class::set_flag_no_args, &flag).wait();
|
||||
flag_class<P> flag(pool);
|
||||
pool.submit(&flag_class<P>::set_flag_no_args, &flag).wait();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
dual_println("Checking that submit() works for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
pool.submit(&flag_class::set_flag_one_arg, &flag, true).wait();
|
||||
flag_class<P> flag(pool);
|
||||
pool.submit(&flag_class<P>::set_flag_one_arg, &flag, true).wait();
|
||||
check(flag.get_flag());
|
||||
}
|
||||
dual_println("Checking that submit() works for a member function with no arguments and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_no_args_return, &flag);
|
||||
flag_class<P> flag(pool);
|
||||
std::future<int> flag_future = pool.submit(&flag_class<P>::set_flag_no_args_return, &flag);
|
||||
check(flag_future.get() == 42 && flag.get_flag());
|
||||
}
|
||||
dual_println("Checking that submit() works for a member function with one argument and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
std::future<int> flag_future = pool.submit(&flag_class::set_flag_one_arg_return, &flag, true);
|
||||
flag_class<P> flag(pool);
|
||||
std::future<int> flag_future = pool.submit(&flag_class<P>::set_flag_one_arg_return, &flag, true);
|
||||
check(flag_future.get() == 42 && flag.get_flag());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that submitting member functions within an object works.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_member_function_within_object()
|
||||
template <typename P>
|
||||
void check_member_function_within_object(P& pool)
|
||||
{
|
||||
dual_println("Checking that push_task() works within an object for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag_class<P> flag(pool);
|
||||
flag.push_test_flag_no_args();
|
||||
}
|
||||
dual_println("Checking that push_task() works within an object for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag_class<P> flag(pool);
|
||||
flag.push_test_flag_one_arg();
|
||||
}
|
||||
dual_println("Checking that submit() works within an object for a member function with no arguments or return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag_class<P> flag(pool);
|
||||
flag.submit_test_flag_no_args();
|
||||
}
|
||||
dual_println("Checking that submit() works within an object for a member function with one argument and no return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag_class<P> flag(pool);
|
||||
flag.submit_test_flag_one_arg();
|
||||
}
|
||||
dual_println("Checking that submit() works within an object for a member function with no arguments and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag_class<P> flag(pool);
|
||||
flag.submit_test_flag_no_args_return();
|
||||
}
|
||||
dual_println("Checking that submit() works within an object for a member function with one argument and a return value...");
|
||||
{
|
||||
flag_class flag;
|
||||
flag_class<P> flag(pool);
|
||||
flag.submit_test_flag_one_arg_return();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that wait_for_tasks() works.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_wait_for_tasks()
|
||||
template <typename P>
|
||||
void check_wait_for_tasks(P& pool)
|
||||
{
|
||||
const BS::concurrency_t n = pool.get_thread_count() * 10;
|
||||
std::unique_ptr<std::atomic<bool>[]> flags = std::make_unique<std::atomic<bool>[]>(n);
|
||||
@@ -585,12 +708,60 @@ void check_wait_for_tasks()
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that calling wait_for_tasks() more than once doesn't create a deadlock. Uses a mechanism similar to check_deadlock().
|
||||
* @brief Check that wait_for_tasks() correctly blocks all external threads that call it.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
template <typename P>
|
||||
void check_wait_for_tasks_blocks(P& pool)
|
||||
{
|
||||
pool.wait_for_tasks();
|
||||
dual_println("Checking that wait_for_tasks() correctly blocks all external threads that call it...");
|
||||
std::atomic<bool> release = false;
|
||||
pool.push_task(
|
||||
[&release]
|
||||
{
|
||||
dual_println("Task submitted to pool 1 and waiting to be released...");
|
||||
while (!release)
|
||||
std::this_thread::yield();
|
||||
dual_println("Task released.");
|
||||
});
|
||||
constexpr BS::concurrency_t num_waiting_tasks = 4;
|
||||
P temp_pool(num_waiting_tasks);
|
||||
std::unique_ptr<std::atomic<bool>[]> flags = std::make_unique<std::atomic<bool>[]>(num_waiting_tasks);
|
||||
auto waiting_task = [&flags, &pool](const BS::concurrency_t i)
|
||||
{
|
||||
dual_println("Task ", i, " submitted to pool 2 and waiting for pool 1's task to finish...");
|
||||
pool.wait_for_tasks();
|
||||
dual_println("Task ", i, " finished waiting.");
|
||||
flags[i] = true;
|
||||
};
|
||||
for (BS::concurrency_t i = 0; i < num_waiting_tasks; ++i)
|
||||
temp_pool.push_task(waiting_task, i);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
bool any_flag_set = false;
|
||||
for (BS::concurrency_t i = 0; i < num_waiting_tasks; ++i)
|
||||
any_flag_set = any_flag_set || flags[i];
|
||||
check(!any_flag_set);
|
||||
release = true;
|
||||
temp_pool.wait_for_tasks();
|
||||
bool all_flags_set = true;
|
||||
for (size_t i = 0; i < num_waiting_tasks; ++i)
|
||||
all_flags_set = all_flags_set && flags[i];
|
||||
check(all_flags_set);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that calling wait_for_tasks() more than once doesn't create a deadlock. Uses a mechanism similar to check_deadlock().
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
*/
|
||||
template <typename P>
|
||||
void check_wait_for_tasks_deadlock()
|
||||
{
|
||||
dual_println("Checking for deadlocks when waiting for tasks...");
|
||||
BS::thread_pool temp_pool(1);
|
||||
P temp_pool(1);
|
||||
temp_pool.push_task([] { std::this_thread::sleep_for(std::chrono::milliseconds(500)); });
|
||||
constexpr uint32_t n_waiting_tasks = 1000;
|
||||
std::atomic<uint32_t> count = 0;
|
||||
@@ -630,8 +801,10 @@ void check_wait_for_tasks_deadlock()
|
||||
|
||||
/**
|
||||
* @brief Check that wait_for_tasks_duration() works.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_wait_for_tasks_duration()
|
||||
void check_wait_for_tasks_duration(BS::thread_pool& pool)
|
||||
{
|
||||
dual_println("Checking that wait_for_tasks_duration() works...");
|
||||
pool.reset();
|
||||
@@ -652,8 +825,10 @@ void check_wait_for_tasks_duration()
|
||||
|
||||
/**
|
||||
* @brief Check that wait_for_tasks_until() works.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_wait_for_tasks_until()
|
||||
void check_wait_for_tasks_until(BS::thread_pool& pool)
|
||||
{
|
||||
dual_println("Checking that wait_for_tasks_until() works...");
|
||||
pool.reset();
|
||||
@@ -680,13 +855,16 @@ void check_wait_for_tasks_until()
|
||||
/**
|
||||
* @brief Check that push_loop() or parallelize_loop() work for a specific range of indices split over a specific number of tasks, with no return value.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @tparam T The type of the last index in the loop plus 1.
|
||||
* @param pool The thread pool to check.
|
||||
* @param random_start The first index in the loop.
|
||||
* @param random_end The last index in the loop plus 1.
|
||||
* @param num_tasks The number of tasks.
|
||||
* @param use_push Whether to check push_loop() instead of parallelize_loop().
|
||||
*/
|
||||
template <typename T>
|
||||
void check_parallelize_loop_no_return(const int64_t random_start, T random_end, const BS::concurrency_t num_tasks, const bool use_push = false)
|
||||
template <typename P, typename T>
|
||||
void check_parallelize_loop_no_return(P& pool, const int64_t random_start, T random_end, const BS::concurrency_t num_tasks, const bool use_push = false)
|
||||
{
|
||||
if (random_start == random_end)
|
||||
++random_end;
|
||||
@@ -707,7 +885,7 @@ void check_parallelize_loop_no_return(const int64_t random_start, T random_end,
|
||||
pool.push_loop(random_start, random_end, loop, num_tasks);
|
||||
pool.wait_for_tasks();
|
||||
}
|
||||
else
|
||||
else if constexpr (std::is_same_v<P, BS::thread_pool>)
|
||||
{
|
||||
if (random_start == 0)
|
||||
pool.parallelize_loop(random_end, loop, num_tasks).wait();
|
||||
@@ -723,11 +901,12 @@ void check_parallelize_loop_no_return(const int64_t random_start, T random_end,
|
||||
/**
|
||||
* @brief Check that parallelize_loop() works for a specific range of indices split over a specific number of tasks, with a return value.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
* @param random_start The first index in the loop.
|
||||
* @param random_end The last index in the loop plus 1.
|
||||
* @param num_tasks The number of tasks.
|
||||
*/
|
||||
void check_parallelize_loop_return(const int64_t random_start, int64_t random_end, const BS::concurrency_t num_tasks)
|
||||
void check_parallelize_loop_return(BS::thread_pool& pool, const int64_t random_start, int64_t random_end, const BS::concurrency_t num_tasks)
|
||||
{
|
||||
if (random_start == random_end)
|
||||
++random_end;
|
||||
@@ -748,32 +927,50 @@ void check_parallelize_loop_return(const int64_t random_start, int64_t random_en
|
||||
|
||||
/**
|
||||
* @brief Check that push_loop() and parallelize_loop() work using several different random values for the range of indices and number of tasks.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_push_loop()
|
||||
template <typename P>
|
||||
void check_loop(P& pool)
|
||||
{
|
||||
std::mt19937_64 mt(rd());
|
||||
std::uniform_int_distribution<int64_t> index_dist(-1000000, 1000000);
|
||||
std::uniform_int_distribution<BS::concurrency_t> task_dist(1, pool.get_thread_count());
|
||||
constexpr uint64_t n = 10;
|
||||
if constexpr (std::is_same_v<P, BS::thread_pool>)
|
||||
{
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
check_parallelize_loop_no_return(pool, index_dist(mt), index_dist(mt), task_dist(mt));
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
check_parallelize_loop_return(pool, index_dist(mt), index_dist(mt), task_dist(mt));
|
||||
dual_println("Verifying that parallelize_loop() with identical start and end indices does nothing...");
|
||||
bool flag = true;
|
||||
const int64_t index = index_dist(mt);
|
||||
pool.parallelize_loop(index, index, [&flag](const int64_t, const int64_t) { flag = false; }).wait();
|
||||
check(flag);
|
||||
dual_println("Trying parallelize_loop() with start and end indices of different types:");
|
||||
const int64_t start = index_dist(mt);
|
||||
const uint32_t end = static_cast<uint32_t>(std::abs(index_dist(mt)));
|
||||
check_parallelize_loop_no_return(pool, start, end, task_dist(mt));
|
||||
dual_println("Trying the overload of parallelize_loop() for the case where the first index is equal to 0:");
|
||||
check_parallelize_loop_no_return(pool, 0, index_dist(mt), task_dist(mt));
|
||||
check_parallelize_loop_return(pool, 0, index_dist(mt), task_dist(mt));
|
||||
}
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
check_parallelize_loop_no_return(index_dist(mt), index_dist(mt), task_dist(mt), true);
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
check_parallelize_loop_no_return(index_dist(mt), index_dist(mt), task_dist(mt));
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
check_parallelize_loop_return(index_dist(mt), index_dist(mt), task_dist(mt));
|
||||
dual_println("Verifying that parallelize_loop() with identical start and end indices does nothing...");
|
||||
check_parallelize_loop_no_return(pool, index_dist(mt), index_dist(mt), task_dist(mt), true);
|
||||
dual_println("Verifying that push_loop() with identical start and end indices does nothing...");
|
||||
bool flag = true;
|
||||
const int64_t index = index_dist(mt);
|
||||
pool.parallelize_loop(index, index, [&flag](const int64_t, const int64_t) { flag = false; }).wait();
|
||||
pool.push_loop(index, index, [&flag](const int64_t, const int64_t) { flag = false; });
|
||||
pool.wait_for_tasks();
|
||||
check(flag);
|
||||
dual_println("Trying parallelize_loop() with start and end indices of different types:");
|
||||
dual_println("Trying push_loop() with start and end indices of different types:");
|
||||
const int64_t start = index_dist(mt);
|
||||
const uint32_t end = static_cast<uint32_t>(std::abs(index_dist(mt)));
|
||||
check_parallelize_loop_no_return(start, end, task_dist(mt));
|
||||
dual_println("Trying the overloads for push_loop() and parallelize_loop() for the case where the first index is equal to 0:");
|
||||
check_parallelize_loop_no_return(0, index_dist(mt), task_dist(mt), true);
|
||||
check_parallelize_loop_no_return(0, index_dist(mt), task_dist(mt));
|
||||
check_parallelize_loop_return(0, index_dist(mt), task_dist(mt));
|
||||
check_parallelize_loop_no_return(pool, start, end, task_dist(mt), true);
|
||||
dual_println("Trying the overload of push_loop() for the case where the first index is equal to 0:");
|
||||
check_parallelize_loop_no_return(pool, 0, index_dist(mt), task_dist(mt), true);
|
||||
}
|
||||
|
||||
// ===============================================
|
||||
@@ -782,8 +979,10 @@ void check_push_loop()
|
||||
|
||||
/**
|
||||
* @brief Check that task monitoring works.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_task_monitoring()
|
||||
void check_task_monitoring(BS::thread_pool& pool)
|
||||
{
|
||||
BS::concurrency_t n = std::min<BS::concurrency_t>(std::thread::hardware_concurrency(), 4);
|
||||
dual_println("Resetting pool to ", n, " threads.");
|
||||
@@ -832,8 +1031,10 @@ void check_task_monitoring()
|
||||
|
||||
/**
|
||||
* @brief Check that pausing works.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_pausing()
|
||||
void check_pausing(BS::thread_pool& pool)
|
||||
{
|
||||
BS::concurrency_t n = std::min<BS::concurrency_t>(std::thread::hardware_concurrency(), 4);
|
||||
dual_println("Resetting pool to ", n, " threads.");
|
||||
@@ -894,14 +1095,53 @@ void check_pausing()
|
||||
pool.reset(std::thread::hardware_concurrency());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that purge() works.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_purge(BS::thread_pool& pool)
|
||||
{
|
||||
dual_println("Resetting pool to 1 thread.");
|
||||
pool.reset(1);
|
||||
constexpr size_t num_tasks = 10;
|
||||
dual_println("Submitting ", num_tasks, " tasks to the pool.");
|
||||
std::unique_ptr<std::atomic<bool>[]> done = std::make_unique<std::atomic<bool>[]>(num_tasks);
|
||||
for (size_t i = 0; i < num_tasks; ++i)
|
||||
pool.push_task(
|
||||
[&done, i]
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
dual_println("Task ", i, " done.");
|
||||
done[i] = true;
|
||||
});
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
dual_println("Purging the pool and waiting for tasks...");
|
||||
pool.purge();
|
||||
pool.wait_for_tasks();
|
||||
dual_println("Checking that only the first task was executed...");
|
||||
check(done[0]);
|
||||
bool any_flag_set = false;
|
||||
for (BS::concurrency_t i = 1; i < num_tasks; ++i)
|
||||
any_flag_set = any_flag_set || done[i];
|
||||
check(!any_flag_set);
|
||||
|
||||
dual_println("Resetting pool to ", std::thread::hardware_concurrency(), " threads.");
|
||||
pool.reset(std::thread::hardware_concurrency());
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// Functions to verify exception handling
|
||||
// ======================================
|
||||
|
||||
/**
|
||||
* @brief Check that exception handling works.
|
||||
* @brief Check that exceptions are forwarded correctly by submit().
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_exceptions()
|
||||
template <typename P>
|
||||
void check_exceptions_submit(P& pool)
|
||||
{
|
||||
dual_println("Checking that exceptions are forwarded correctly by submit()...");
|
||||
bool caught = false;
|
||||
@@ -921,15 +1161,28 @@ void check_exceptions()
|
||||
caught = true;
|
||||
}
|
||||
check(caught);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check that exceptions are forwarded correctly by BS::multi_future.
|
||||
*
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_exceptions_multi_future(BS::thread_pool& pool)
|
||||
{
|
||||
dual_println("Checking that exceptions are forwarded correctly by BS::multi_future...");
|
||||
caught = false;
|
||||
BS::multi_future<void> my_future2;
|
||||
my_future2.push_back(pool.submit(throws));
|
||||
my_future2.push_back(pool.submit(throws));
|
||||
bool caught = false;
|
||||
auto throws = []
|
||||
{
|
||||
dual_println("Throwing exception...");
|
||||
throw std::runtime_error("Exception thrown!");
|
||||
};
|
||||
BS::multi_future<void> my_future;
|
||||
my_future.push_back(pool.submit(throws));
|
||||
my_future.push_back(pool.submit(throws));
|
||||
try
|
||||
{
|
||||
void(my_future2.get());
|
||||
void(my_future.get());
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
@@ -945,8 +1198,14 @@ void check_exceptions()
|
||||
|
||||
/**
|
||||
* @brief Check that parallelized vector operations work as expected by calculating the sum of two randomized vectors of a specific size in two ways, single-threaded and multithreaded, and comparing the results.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
* @param vector_size The size of the vectors.
|
||||
* @param num_tasks The number of tasks to split the calculation into.
|
||||
*/
|
||||
void check_vector_of_size(const size_t vector_size, const BS::concurrency_t num_tasks)
|
||||
template <typename P>
|
||||
void check_vector_of_size(P& pool, const size_t vector_size, const BS::concurrency_t num_tasks)
|
||||
{
|
||||
std::vector<int64_t> vector_1(vector_size);
|
||||
std::vector<int64_t> vector_2(vector_size);
|
||||
@@ -962,15 +1221,15 @@ void check_vector_of_size(const size_t vector_size, const BS::concurrency_t num_
|
||||
for (size_t i = 0; i < vector_size; ++i)
|
||||
sum_single[i] = vector_1[i] + vector_2[i];
|
||||
std::vector<int64_t> sum_multi(vector_size);
|
||||
pool.parallelize_loop(
|
||||
0, vector_size,
|
||||
[&sum_multi, &vector_1, &vector_2](const size_t start, const size_t end)
|
||||
{
|
||||
for (size_t i = start; i < end; ++i)
|
||||
sum_multi[i] = vector_1[i] + vector_2[i];
|
||||
},
|
||||
num_tasks)
|
||||
.wait();
|
||||
pool.push_loop(
|
||||
0, vector_size,
|
||||
[&sum_multi, &vector_1, &vector_2](const size_t start, const size_t end)
|
||||
{
|
||||
for (size_t i = start; i < end; ++i)
|
||||
sum_multi[i] = vector_1[i] + vector_2[i];
|
||||
},
|
||||
num_tasks);
|
||||
pool.wait_for_tasks();
|
||||
bool vectors_equal = true;
|
||||
for (size_t i = 0; i < vector_size; ++i)
|
||||
vectors_equal = vectors_equal && (sum_single[i] == sum_multi[i]);
|
||||
@@ -979,15 +1238,18 @@ void check_vector_of_size(const size_t vector_size, const BS::concurrency_t num_
|
||||
|
||||
/**
|
||||
* @brief Check that parallelized vector operations work as expected by calculating the sum of two randomized vectors in two ways, single-threaded and multithreaded, and comparing the results.
|
||||
*
|
||||
* @tparam P The type of the thread pool to check.
|
||||
* @param pool The thread pool to check.
|
||||
*/
|
||||
void check_vectors()
|
||||
template <typename P>
|
||||
void check_vectors(P& pool)
|
||||
{
|
||||
pool.reset();
|
||||
std::mt19937_64 mt(rd());
|
||||
std::uniform_int_distribution<size_t> size_dist(0, 1000000);
|
||||
std::uniform_int_distribution<BS::concurrency_t> task_dist(1, pool.get_thread_count());
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
check_vector_of_size(size_dist(mt), task_dist(mt));
|
||||
check_vector_of_size(pool, size_dist(mt), task_dist(mt));
|
||||
}
|
||||
|
||||
// ==================
|
||||
@@ -999,44 +1261,81 @@ void check_vectors()
|
||||
*/
|
||||
void do_tests()
|
||||
{
|
||||
print_header("Checking that the constructor works:");
|
||||
check_constructor();
|
||||
print_header("Checking that the constructor works in the full thread pool:");
|
||||
check_constructor(pool_full);
|
||||
print_header("Checking that the constructor works in the light thread pool:");
|
||||
check_constructor(pool_light);
|
||||
|
||||
print_header("Checking that reset() works:");
|
||||
check_reset();
|
||||
print_header("Checking that reset() works in the full thread pool:");
|
||||
check_reset(pool_full);
|
||||
|
||||
print_header("Checking that push_task() works:");
|
||||
check_push_task();
|
||||
print_header("Checking that push_task() works in the full thread pool:");
|
||||
check_push_task(pool_full);
|
||||
print_header("Checking that push_task() works in the light thread pool:");
|
||||
check_push_task(pool_light);
|
||||
|
||||
print_header("Checking that submit() works:");
|
||||
check_submit();
|
||||
print_header("Checking that submit() works in the full thread pool:");
|
||||
check_submit(pool_full);
|
||||
print_header("Checking that submit() works in the light thread pool:");
|
||||
check_submit(pool_light);
|
||||
|
||||
print_header("Checking that submitting member functions works:");
|
||||
check_member_function();
|
||||
print_header("Checking that submitting member functions works in the full thread pool:");
|
||||
check_member_function(pool_full);
|
||||
print_header("Checking that submitting member functions works in the light thread pool:");
|
||||
check_member_function(pool_light);
|
||||
|
||||
print_header("Checking that submitting member functions from within an object works:");
|
||||
check_member_function_within_object();
|
||||
print_header("Checking that submitting member functions from within an object works in the full thread pool:");
|
||||
check_member_function_within_object(pool_full);
|
||||
print_header("Checking that submitting member functions from within an object works in the light thread pool:");
|
||||
check_member_function_within_object(pool_light);
|
||||
|
||||
print_header("Checking that wait_for_tasks() works...");
|
||||
check_wait_for_tasks();
|
||||
check_wait_for_tasks_deadlock();
|
||||
check_wait_for_tasks_duration();
|
||||
check_wait_for_tasks_until();
|
||||
print_header("Checking that wait_for_tasks() works in the full thread pool...");
|
||||
check_wait_for_tasks(pool_full);
|
||||
check_wait_for_tasks_blocks(pool_full);
|
||||
check_wait_for_tasks_deadlock<BS::thread_pool>();
|
||||
check_wait_for_tasks_duration(pool_full);
|
||||
check_wait_for_tasks_until(pool_full);
|
||||
print_header("Checking that wait_for_tasks() works in the light thread pool...");
|
||||
check_wait_for_tasks(pool_light);
|
||||
check_wait_for_tasks_blocks(pool_full);
|
||||
check_wait_for_tasks_deadlock<BS::thread_pool_light>();
|
||||
|
||||
print_header("Checking that push_loop() and parallelize_loop() work:");
|
||||
check_push_loop();
|
||||
print_header("Checking that push_loop() and parallelize_loop() work in the full thread pool:");
|
||||
check_loop(pool_full);
|
||||
print_header("Checking that push_loop() works in the light thread pool:");
|
||||
check_loop(pool_light);
|
||||
|
||||
print_header("Checking that task monitoring works:");
|
||||
check_task_monitoring();
|
||||
print_header("Checking that task monitoring works in the full thread pool:");
|
||||
check_task_monitoring(pool_full);
|
||||
|
||||
print_header("Checking that pausing works:");
|
||||
check_pausing();
|
||||
print_header("Checking that pausing works in the full thread pool:");
|
||||
check_pausing(pool_full);
|
||||
|
||||
print_header("Checking that exception handling works:");
|
||||
check_exceptions();
|
||||
print_header("Checking that purge() works in the full thread pool:");
|
||||
check_purge(pool_full);
|
||||
|
||||
print_header("Testing that vector operations produce the expected results:");
|
||||
check_vectors();
|
||||
print_header("Checking that exception handling works in the full thread pool:");
|
||||
check_exceptions_submit(pool_full);
|
||||
check_exceptions_multi_future(pool_full);
|
||||
print_header("Checking that exception handling works in the light thread pool:");
|
||||
check_exceptions_submit(pool_light);
|
||||
|
||||
print_header("Testing that vector operations produce the expected results in the full thread pool:");
|
||||
check_vectors(pool_full);
|
||||
print_header("Testing that vector operations produce the expected results in the light thread pool:");
|
||||
check_vectors(pool_light);
|
||||
|
||||
if (enable_long_deadlock_tests)
|
||||
{
|
||||
print_header("Checking for deadlocks:");
|
||||
dual_println("Checking for destruction deadlocks in the full thread pool...");
|
||||
check_deadlock([] { BS::thread_pool temp_pool; });
|
||||
dual_println("Checking for destruction deadlocks in the light thread pool...");
|
||||
check_deadlock([] { BS::thread_pool_light temp_pool; });
|
||||
dual_println("Checking for reset deadlocks in the full thread pool...");
|
||||
BS::thread_pool temp_pool;
|
||||
check_deadlock([&temp_pool] { temp_pool.reset(); });
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================
|
||||
@@ -1108,8 +1407,8 @@ double generate_element(const size_t i, const size_t j)
|
||||
*/
|
||||
void check_performance()
|
||||
{
|
||||
// Reset the pool to ensure that we have a fresh start.
|
||||
pool.reset();
|
||||
// Create a new pool to ensure that we have a fresh start. We use the light version here since we don't need any of the advanced features of the full version.
|
||||
BS::thread_pool_light pool;
|
||||
|
||||
// Set the formatting of floating point numbers.
|
||||
dual_print(std::fixed, std::setprecision(1));
|
||||
@@ -1205,6 +1504,7 @@ int main()
|
||||
dual_println("GitHub: https://github.com/bshoshany/thread-pool\n");
|
||||
|
||||
dual_println("Thread pool library version is ", BS_THREAD_POOL_VERSION, ".");
|
||||
dual_println("Light thread pool library version is ", BS_THREAD_POOL_LIGHT_VERSION, ".");
|
||||
dual_println("Hardware concurrency is ", std::thread::hardware_concurrency(), ".");
|
||||
if (output_log)
|
||||
dual_println("Generating log file: ", log_filename, ".\n");
|
||||
@@ -1214,13 +1514,6 @@ int main()
|
||||
if (enable_tests)
|
||||
do_tests();
|
||||
|
||||
if (enable_long_deadlock_tests)
|
||||
{
|
||||
print_header("Checking for deadlocks:");
|
||||
check_destructor_deadlock();
|
||||
check_reset_deadlock();
|
||||
}
|
||||
|
||||
if (tests_failed == 0)
|
||||
{
|
||||
if (enable_tests)
|
||||
@@ -0,0 +1,224 @@
|
||||
#!/usr/bin/env pwsh
|
||||
$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 $CurrentDir
|
||||
Exit $Code
|
||||
}
|
||||
|
||||
$CurrentDir = Get-Location
|
||||
|
||||
Write-Title '=== BS::thread_pool test script ==='
|
||||
|
||||
$SourceFile = '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
|
||||
}
|
||||
|
||||
$BuildFolder = Join-Path '..' 'build'
|
||||
If (Test-Path -Path $BuildFolder)
|
||||
{
|
||||
Write-Text 'Cleaning up build folder...'
|
||||
Remove-Item (Join-Path $BuildFolder '*')
|
||||
}
|
||||
Else
|
||||
{
|
||||
Write-Text 'Creating build folder...'
|
||||
$Null = New-Item $BuildFolder -ItemType 'Directory'
|
||||
}
|
||||
Write-Text 'Done.'
|
||||
|
||||
Write-Title 'Compiling...'
|
||||
|
||||
If ($IsWindows)
|
||||
{
|
||||
$Extension = '.exe'
|
||||
$PThread = ''
|
||||
}
|
||||
Else
|
||||
{
|
||||
$Extension = ''
|
||||
$PThread = ' -pthread'
|
||||
}
|
||||
|
||||
$ClangExe = 'BS_thread_pool_test_clang' + $Extension
|
||||
$GCCExe = 'BS_thread_pool_test_gcc' + $Extension
|
||||
|
||||
$MSVCName = 'BS_thread_pool_test_msvc'
|
||||
$MSVCExe = $MSVCName + $Extension
|
||||
$MSVCObj = $MSVCName + '.obj'
|
||||
|
||||
If (Get-Command 'clang++' -ErrorAction SilentlyContinue)
|
||||
{
|
||||
$Clang = $True
|
||||
$FullClangExe = Join-Path $BuildFolder $ClangExe
|
||||
Write-Text 'Compiling with Clang...'
|
||||
$ClangCommand = "clang++ $SourceFile$PThread -std=c++17 -O3 -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow -o $FullClangExe"
|
||||
Write-Command $ClangCommand
|
||||
Invoke-Expression $ClangCommand
|
||||
If ($LASTEXITCODE)
|
||||
{
|
||||
Write-Error "Failed to compile, aborting script! (Exit code: $LASTEXITCODE)"
|
||||
Exit-Script $LASTEXITCODE
|
||||
}
|
||||
Else
|
||||
{
|
||||
Write-Text "Successfully compiled to $FullClangExe."
|
||||
}
|
||||
}
|
||||
Else
|
||||
{
|
||||
$Clang = $False
|
||||
Write-Error 'Clang not found, skipping it!'
|
||||
}
|
||||
|
||||
Write-Host
|
||||
|
||||
If (Get-Command 'g++' -ErrorAction SilentlyContinue)
|
||||
{
|
||||
$GCC = $True
|
||||
$FullGCCExe = Join-Path $BuildFolder $GCCExe
|
||||
Write-Text 'Compiling with GCC...'
|
||||
$GCCCommand = "g++ $SourceFile$PThread -std=c++17 -O3 -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow -o $FullGCCExe"
|
||||
Write-Command $GCCCommand
|
||||
Invoke-Expression $GCCCommand
|
||||
If ($LASTEXITCODE)
|
||||
{
|
||||
Write-Error "Failed to compile, aborting script! (Exit code: $LASTEXITCODE)"
|
||||
Exit-Script $LASTEXITCODE
|
||||
}
|
||||
Else
|
||||
{
|
||||
Write-Text "Successfully compiled to $FullGCCExe."
|
||||
}
|
||||
}
|
||||
Else
|
||||
{
|
||||
$GCC = $False
|
||||
Write-Error 'GCC not found, skipping it!'
|
||||
}
|
||||
|
||||
Write-Host
|
||||
|
||||
If ($IsWindows)
|
||||
{
|
||||
$MSVCEnv = 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1'
|
||||
If (Test-Path $MSVCEnv)
|
||||
{
|
||||
$MSVC = $True
|
||||
$FullMSVCExe = Join-Path $BuildFolder $MSVCExe
|
||||
$FullMSVCObj = Join-Path $BuildFolder $MSVCObj
|
||||
Write-Text 'Compiling with MSVC...'
|
||||
$Env:BS_THREAD_POOL_MSVC_COMMAND = "cl $SourceFile /std:c++17 /permissive- /O2 /W4 /EHsc /Fe:$FullMSVCExe /Fo:$FullMSVCObj"
|
||||
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 $FullMSVCObj
|
||||
Write-Text "Successfully compiled to $FullMSVCExe."
|
||||
}
|
||||
}
|
||||
Else
|
||||
{
|
||||
$MSVC = $False
|
||||
Write-Error 'MSVC not found, skipping it!'
|
||||
}
|
||||
}
|
||||
Else
|
||||
{
|
||||
$MSVC = $False
|
||||
}
|
||||
|
||||
Write-Title 'Starting tests...'
|
||||
|
||||
Set-Location $BuildFolder
|
||||
$Repeats = 5;
|
||||
|
||||
Write-Text "Each test will be repeated $Repeats times. Outputs can be found in the generated log files."
|
||||
|
||||
Function Invoke-Test([String] $Compiler, [String] $Executable)
|
||||
{
|
||||
Write-Title "Starting $Compiler tests..."
|
||||
For ($i = 1; $i -Le $Repeats; $i++)
|
||||
{
|
||||
Write-Text "Running $Compiler test #$i of $Repeats..."
|
||||
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-Text "$Compiler tests done."
|
||||
}
|
||||
|
||||
if ($Clang)
|
||||
{
|
||||
Invoke-Test 'Clang' (Join-Path '.' $ClangExe)
|
||||
}
|
||||
if ($GCC)
|
||||
{
|
||||
Invoke-Test 'GCC' (Join-Path '.' $GCCExe)
|
||||
}
|
||||
if ($MSVC)
|
||||
{
|
||||
Invoke-Test 'MSVC' (Join-Path '.' $MSVCExe)
|
||||
}
|
||||
|
||||
Write-Title '=== All tests completed successfully! ==='
|
||||
|
||||
Exit-Script 0
|
||||
Reference in New Issue
Block a user