Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unsigned ThreadCount = 10>
class ThreadPool {
public:
ThreadPool();
ThreadPool( unsigned threadCount = std::thread::hardware_concurrency() );
~ThreadPool();
void AddJob( std::function<void(void)> );
unsigned Size() const;
Expand All @@ -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 )
Expand All @@ -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<void(void)>`:
```c++
template <typename Iter, unsigned Count = 10>
template <typename Iter>
void RunInPool( Iter begin, Iter end ) {
ThreadPool<Count> 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.
23 changes: 12 additions & 11 deletions ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <atomic>
#include <thread>
#include <mutex>
#include <array>
#include <vector>
#include <list>
#include <functional>
#include <condition_variable>
Expand All @@ -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 <unsigned ThreadCount = 10>
class ThreadPool {

std::array<std::thread, ThreadCount> threads;

unsigned threadCount;
std::vector<std::thread> threads;
std::list<std::function<void(void)>> queue;

std::atomic_int jobs_left;
Expand Down Expand Up @@ -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(); } );
}

/**
Expand All @@ -88,7 +89,7 @@ class ThreadPool {
* Get the number of threads in this pool
*/
inline unsigned Size() const {
return ThreadCount;
return threadCount;
}

/**
Expand Down