Skip to content

fangpin/ThreadPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

  • Simple C++11 implemented ThreadPool.
  • One loop per thread model.

Useage

#include <iostream>
#include <vector>
#include <chrono>
#include <string>
#include "ThreadPool.hpp"

void test(int val) {
    std::cout << val << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main() {
    std::vector<int> vec(10);
    {
        Fang::ThreadPool pool(4);

        // example: 1) std::function. 2) no return value.
        for (int i=0; i<10; ++i) {
            pool.insert_task(test, i);
        }

        // example: 1) lambda. 2) return by reference.
        for (int i=0; i<10; ++i) {
            pool.insert_task([&vec, i](){
                vec[i] = i * 10000;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                });
        }
    }
    for (auto x: vec) {
        std::cout << x << std::endl;
    }
    return 0;
}

About

cpp11 implemented thread pool

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages