tyler-hardin / thread_pool Goto Github PK
View Code? Open in Web Editor NEWThread pool using std::* primitives from C++20, with optional priority queue/greenthreading for POSIX.
Thread pool using std::* primitives from C++20, with optional priority queue/greenthreading for POSIX.
I tried something like
...
int myFunc(int a, int b, int c)
...
... myPool.async(std::function<int(int, int, int)>(myFunc), a, b, c)...
and get a ton of errors.
But if I using function with 1 argument everything works well.
What i'm doing wrong?
I integrated thread_pool in one of our projects and replaced the current threading mechanism.
Instead of creating new threads I wanted to reuse the threads. It was fairly easy to migrate to this lib, but I am experiencing high CPU load, when the thread pool is not used.
I simplified the original test code to be the following
`#include "thread_pool.hpp"
#include <vector>
#include <iostream> // std::cout, std::endl
using namespace std;
/*
bool func_bool_int(int n) {
if(n < 2)
return false;
for(int i = 2;i < n;i++)
if(n % i == 0)
return false;
return true;
}
int main() {
const int num_threads = 4;
thread_pool p(num_threads);
int n = 100000;
int max = 5 * n;
vector<future<bool>> f;
for( int i = n; i < max; i++ )
f.emplace_back(p.async(function<bool(int)>(func_bool_int), i));
std::cout << "..." << std::endl;
size_t accum = 0;
for(auto &i : f) {
accum += i.get();
}
std::cout << "accum = " << accum << std::endl;
return 0;
}
`
When I ran for num_threads = 8, 7, 6, 5, 4, 3 things went okay. But when I tried num_threads of 2 or 1 the runtime seemed to be in an infinite loop ie I was seeing about 7s to 12s runtimes above num_threads>2 but once I tried num_threads=2 or 1 I waited 3 minutes to 5 minutes (orders of magnitude longer than extrapolated runtime) before manually killing the process. Is this behavior to be expected? Note this was on a gcc 6.3 with "git checkout -b cv origin/cv" branch. Adding an "-O3" optimazation seemed to fix the issue; expected?
Very silly, but annoying; I get a:
thread_pool.hpp:107:19: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
warning, which is easily silenced by:
- for(int i = 0;i < num_threads;i++){
+ for(unsigned int i = 0;i < num_threads;i++){
I get this when using C++20:
thread_pool.hpp:13:9: warning: the 'bool' keyword is not allowed in a C++20 concept definition 13 | concept bool Callable = requires(Fn f, Args... args) {
(Edited by Tyler to show error that OP had before he deleted the comment):
...
main.cpp:(.text+0x31b): undefined reference to 'thread_pool::~thread_pool()'
...
Hello,
How do I wait for threads to finish?
I've got this:
fut.emplace_back(tp.async(std::function<int(std::string)>(breakPacket), completePacket));
I push into it about 3000 items for the threads ti process, but then I don't know how to wait for the threads to finish...
Thanks!
David
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.