Add support for per-thread initialization function #105
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request policy (please read)
Describe the changes
This adds support for running user-defined function once per thread on said thread's initialization, allowing user to assign custom resources to threads and closing #104. Actually storing and passing resources around is left to user, which can be accomplished fairly easily with
thread_local
variables. Function is given thread's id in thread pool as an argument, which is useful when resources are already allocated by the caller. All threads complete initialization before control is returned to caller in order to allow capturing references to caller's local variables.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):
g++ -std=c++17 -o BS_thread_pool_test -Wall -O3 BS_thread_pool_test.cpp
, replaceg++
withclang++
for clangAdditional information
I've also fixed a minor mistake in full version's tests where a few tests would fail on single-core machine due to half-concurrency being equal to zero.
Would you be interested to see a very simple build file for meson system submitted as a separate PR? It would simplify building/running tests by a fair bit, as well as make inclusion into https://github.com/mesonbuild/wrapdb seamless.
P.S.: why are .cpp files using CRLF line endings?