Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

C++ thread pool

A simple thread pool implementation in C++

Getting Started

To use the thread pool in your project, you simply include the header where it's needed and add the .cpp implentation file in your compilation. Make sure that the header file is visible from the .cpp file. The preferred way of doing this is using the -I option of gcc/g++.

You also have to add the pthread library during the linking step, since the thread pool uses POSIX threads.

Example

#include<iostream>
#include"thread_pool.h"usingnamespacestd;// Extending the base thread_pool::task class to provide void run() implementation.classmy_task : publicthread_pool::task {
private:int i;
public:my_task(int i) : i(i) {}
voidrun() {
cout << "Hello from thread, i = " << i << endl;
}
};
intmain() {
// Creating a thread pool with 4 threads.
thread_pool thread_pool(4);
// Adding jobs in the thread pool.for (int i = 0; i < 50; i++)
thread_pool.add_task(newmy_task(i));
// Thread pool destructor will wait here, until all jobs are completed.
}

License

This project is licensed under the MIT License - see the LICENSE file for details

About

A simple thread pool implementation in C++.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages