Skip to content

iliastsa/cpp-thread-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

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"

using namespace std;

// Extending the base thread_pool::task class to provide void run() implementation.
class my_task : public thread_pool::task {
    private:
        int i;

    public:
        my_task(int i) : i(i) {}
        
        void run() {
            cout << "Hello from thread, i = " << i << endl;
        }
};

int main() {
    // 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(new my_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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages