Skip to content

a simple and lightweight header only ThreadPool for your tasks

License

Notifications You must be signed in to change notification settings

CppPlayground/Simple-ThreadPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Header only simple thread pool

put your tasks into threads

How to use it

#include "ThreadPool.h"
#include <iostream>

void printNumber(int number) { std::cout << "Number: " << number << std::endl; }

int main() {
    ThreadPool pool(4);

    std::vector<std::future<void>> results;

    // starting the threads
    for (int i = 0; i < 8; ++i) {
        results.emplace_back(pool.enqueue(printNumber, i));
    }

    // getting the results
    for (auto &&result : results) {
        result.get();
    }

    return 0;
}

multiply example

#include "ThreadPool.h"
#include <iostream>

int multiply(const int a, const int b) {
    return a * b;
}

int main() {
    ThreadPool pool(12);

    std::future<int> result = pool.enqueue(multiply, 3, 5);

    std::cout<< result.get();

    return 0;
}

Releases

No releases published

Packages

No packages published