diff --git a/README.md b/README.md index 36ee1f0..32a3c24 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,14 @@ I needed a Thread Pool for something I was writing, and I didn't see any that I Public Domain. If my licensing is wrong, please let me know. Use at your own risk for whatever you want. Feel free to change the namespaces as well. Apparently licensing is hard and complicated. If your country doesn't have a public domain, feel free to say you found this on the side of the road. ## Overview -`ThreadPool` is a super simple class that manages threads and jobs. `ThreadCount` threads are created at object instantiation time, and persist until the `ThreadPool` object is destroyed. You cannot change the thread count. A later version may allow you to set the thread count through the constructor rather than as a template parameter, but it's not something I care to do at the moment. Jobs are functions with no parameters or return values. This decision was to make it as generic as possible so it could be integrated into a variety of projects. If you can't get your job to work with those constraints, you're doing something wrong, or you need to roll your own ThreadPool. But you're probably making things overly complicated. +`ThreadPool` is a super simple class that manages threads and jobs. `threadCount` threads are created at object instantiation time, and persist until the `ThreadPool` object is destroyed. You cannot change the thread count. Jobs are functions with no parameters or return values. This decision was to make it as generic as possible so it could be integrated into a variety of projects. If you can't get your job to work with those constraints, you're doing something wrong, or you need to roll your own ThreadPool. But you're probably making things overly complicated. Below is a quick overview, but ThreadPool.h is documented, so just read that. It's less than 200 lines with comments. ```c++ -template class ThreadPool { public: - ThreadPool(); + ThreadPool( unsigned threadCount = std::thread::hardware_concurrency() ); ~ThreadPool(); void AddJob( std::function ); unsigned Size() const; @@ -36,7 +35,8 @@ public: int main() { using nbsdx::concurrent::ThreadPool; - ThreadPool pool; // Defaults to 10 threads. + // 10 threads for 100 jobs + ThreadPool pool(10); int JOB_COUNT = 100; for( int i = 0; i < JOB_COUNT; ++i ) @@ -51,12 +51,12 @@ int main() { Convience Function for running a list of jobs in a pool, assuming the type being iterated is of `std::function`: ```c++ -template +template void RunInPool( Iter begin, Iter end ) { - ThreadPool pool; + ThreadPool pool; for( ; begin != end; begin = std::next( begin ) ) pool.AddJob( *begin ); pool.JoinAll(); } ``` -It's worth nothing that the `pool.JoinAll();` is optional in this example, since `JoinAll` is invoked upon object deconstruction. +It's worth noting that the `pool.JoinAll();` is optional in this example, since `JoinAll` is invoked upon object deconstruction. diff --git a/ThreadPool.h b/ThreadPool.h index 21f8261..c17f12b 100644 --- a/ThreadPool.h +++ b/ThreadPool.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include @@ -13,15 +13,15 @@ namespace nbsdx { namespace concurrent { /** - * Simple ThreadPool that creates `ThreadCount` threads upon its creation, - * and pulls from a queue to get new jobs. The default is 10 threads. + * Simple ThreadPool that creates `threadCount` threads upon its creation, + * and pulls from a queue to get new jobs. The default is CPU thread count. * * This class requires a number of c++11 features be present in your compiler. */ -template class ThreadPool { - - std::array threads; + + unsigned threadCount; + std::vector threads; std::list> queue; std::atomic_int jobs_left; @@ -68,13 +68,14 @@ class ThreadPool { } public: - ThreadPool() - : jobs_left( 0 ) + ThreadPool(unsigned threadCount = std::thread::hardware_concurrency()) + : threadCount( threadCount ) + , jobs_left( 0 ) , bailout( false ) , finished( false ) { - for( unsigned i = 0; i < ThreadCount; ++i ) - threads[ i ] = std::thread( [this]{ this->Task(); } ); + for( unsigned i = 0; i < threadCount; ++i ) + threads.emplace_back( [this,i]{ this->Task(); } ); } /** @@ -88,7 +89,7 @@ class ThreadPool { * Get the number of threads in this pool */ inline unsigned Size() const { - return ThreadCount; + return threadCount; } /**