Skip to content

An easy to use and efficient ThreadPool in C++11, with absolutely no dependencies. Just include the code in your project.

License

Notifications You must be signed in to change notification settings

AidanShipperley/CPP-ThreadPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CPP-ThreadPool

An easy-to-use and efficient ThreadPool in C++11, with absolutely no dependencies.

How To Use

To use CPP-ThreadPool in your project, add this to your source code:

#include "ThreadPool.h"
#include <memory> // For unique pointer

To create the thread pool object on the heap, run this to define it:

std::unique_ptr<ThreadPool> threadPool;
// Here specify how many threads you want to use in the threadpool
threadPool = std::make_unique<ThreadPool>(std::thread::hardware_concurrency());

To start the thread pool, run start:

threadPool->start();

Now, we are ready to use the thread pool. You can add jobs easily to the pool with queuejob:

std::vector<int> my_vector(10);

for (int i = 0; i < 10; i++) {

  // Add job to pool
  threadPool->queueJob([this, i, &my_vector]() mutable {
    my_vector[i] = i;
  });

}

// Wait for all threads to finish
threadPool->waitForJobsToFinish();

You can also optionally query if the pool is busy with busy:

if (threadPool->busy()) {
  std::cout << "Pool has pending jobs!" << std::endl;
}

Finally, to stop the threadpool and deallocate all threads, run stop:

threadPool->stop();

About

An easy to use and efficient ThreadPool in C++11, with absolutely no dependencies. Just include the code in your project.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages