Giter Site home page Giter Site logo

tyler-hardin / thread_pool Goto Github PK

View Code? Open in Web Editor NEW
81.0 10.0 13.0 26 KB

Thread pool using std::* primitives from C++20, with optional priority queue/greenthreading for POSIX.

C++ 100.00%
thread-pool priority-pool c-plus-plus-17 concepts c-plus-plus-20 concurrency cpp

thread_pool's People

Contributors

alex-anenkov avatar r0l1 avatar tyler-hardin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

thread_pool's Issues

How to pass a multiple arguments to my function?

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?

High CPU usage

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.

Test as number of threads decreased : infinite loop?

I simplified the original test code to be the following

test.cpp.gz

`#include "thread_pool.hpp"
#include <vector>
#include <iostream> // std::cout, std::endl
using namespace std;

/*

  • Intel Xeon ES-1620 v3 3.5GHz
  • git clone https://github.com/Tyler-Hardin/thread_pool.git
  • git checkout -b cv origin/cv
  • $ gcc --version
  • gcc.exe (Rev3, Built by MSYS2 project) 6.3.0
  • $ g++ --std=c++11 test.cpp thread_pool.cpp
  • $ time ./a.exe
  • ...
  • real 5m23.883s for num_threads = 1 (aborted manually)
  • real 3m55.198s for num_threads = 2 (aborted manually)
  • why do above configurations never finish but the ones below do finish?
  • Further, if add "-O3" to compiler option above, the run in 26s and 13s,why?
  • real 0m12.690s for num_threads = 3
  • real 0m9.199s for num_threads = 4
  • real 0m7.971s for num_threads = 5
  • real 0m7.654s for num_threads = 6
  • real 0m7.304s for num_threads = 7
  • real 0m7.740s for num_threads = 8
    */

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?

Compilation warning Wsign-compare

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++){

C++20 build failure

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) {

Waiting for threads to finish

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.