Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 1.07 KB

File metadata and controls

42 lines (32 loc) · 1.07 KB

Introduction

MIT licensed

This is a simple generic threadpool in c++.

Example

#include    "ThreadPool.hpp"

#include    <iostream>

std::ostream &operator<<(std::ostream &os, const std::vector<std::thread::id> &thread_ids)
{
    for (const auto &thread_id : thread_ids)
    {
        os << "[" << thread_id << "]";
    }
    os << std::endl;
    return os;
}

int     main(void)
{
    ThreadPool  threadpool(std::thread::hardware_concurrency());
    std::vector<std::thread::id>    thread_ids(1000, std::this_thread::get_id());

    for (std::size_t job_number = 0; job_number < 1000; ++job_number)
    {
        threadpool.add([=, &thread_ids]() {
            thread_ids[job_number] = std::this_thread::get_id();
        });
    }
    threadpool.wait_unfinished_jobs();
    std::cout << thread_ids << std::endl;
}

possible output :

[4636][5848][5848][5848][4636][5848][4636][3224][4636] ... [5848][5796][3224][5848][5796][3224][5848][5796][3224][5848][5796][3224]