GithubHelp home page GithubHelp logo

threadpool's Introduction

ThreadPool

Lightweight, Generic, Pure C++11 ThreadPool

Rational

I needed a Thread Pool for something I was writing, and I didn't see any that I liked. This is still somewhat a work in progress; it's stable, but there are probably places where some of the locking logic could be better. ThreadPool::JoinAll is a little sloppy, but it works.

Licensing

Public Domain. If my licensing is wrong, please let me know. Use at your own risk for whatever you want. Feel free to change the namespaces as well. Apparently licensing is hard and complicated. If your country doesn't have a public domain, feel free to say you found this on the side of the road.

Overview

ThreadPool is a super simple class that manages threads and jobs. ThreadCount threads are created at object instantiation time, and persist until the ThreadPool object is destroyed. You cannot change the thread count. A later version may allow you to set the thread count through the constructor rather than as a template parameter, but it's not something I care to do at the moment. Jobs are functions with no parameters or return values. This decision was to make it as generic as possible so it could be integrated into a variety of projects. If you can't get your job to work with those constraints, you're doing something wrong, or you need to roll your own ThreadPool. But you're probably making things overly complicated.

Below is a quick overview, but ThreadPool.h is documented, so just read that. It's less than 200 lines with comments.

template <unsigned ThreadCount = 10>
class ThreadPool {
public:
    ThreadPool();
    ~ThreadPool();
    void AddJob( std::function<void(void)> );
    unsigned Size() const;
    unsigned JobsRemaining();
    void JoinAll( bool WaitForAll = true );
    void WaitAll();
};

Examples

#include "ThreadPool.h"

#include <iostream>
#include <chrono>

int main() {
    using nbsdx::concurrent::ThreadPool;
    
    ThreadPool pool; // Defaults to 10 threads.
    int JOB_COUNT = 100;
    
    for( int i = 0; i < JOB_COUNT; ++i )
        pool.AddJob( []() { 
            std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
        } );
    
    pool.JoinAll();
    std::cout << "Expected runtime: 10 seconds." << std::endl;
}

Convience Function for running a list of jobs in a pool, assuming the type being iterated is of std::function<void(void)>:

template <typename Iter, unsigned Count = 10>
void RunInPool( Iter begin, Iter end ) {
    ThreadPool<Count> pool;
    for( ; begin != end; begin = std::next( begin ) )
        pool.AddJob( *begin );
    pool.JoinAll();
}

It's worth nothing that the pool.JoinAll(); is optional in this example, since JoinAll is invoked upon object deconstruction.

threadpool's People

Contributors

1feng avatar nbsdx avatar zouyxdut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

threadpool's Issues

error: could not convert ‘__p.ThreadPool::next_job()::<lambda()>()’ from ‘void’ to ‘bool’

I'm getting this error /usr/include/c++/5.3.0/condition_variable: In instantiation of ‘void std::condition_variable::wait(std::unique_lock<std::mutex>&, _Predicate) [with _Predicate = ThreadPool::next_job()::<lambda()>]’: lib/../lib/ThreadPool.hpp:53:78: required from here /usr/include/c++/5.3.0/condition_variable:97:13: error: could not convert ‘__p.ThreadPool::next_job()::<lambda()>()’ from ‘void’ to ‘bool’ while (!__p())

while using your header with lambda:

ThreadPool<8> pool;
pool.AddJob( [it, &l, this]() {
    // Do something
    if (...) {
    }else {...}
    std::lock_guard<std::mutex> lock(this->mtx);
    {...}
});

Dead-Lock

  • Hi, nbsdx, I try the following code to use the ThreadPool, but it seemed to be a dead-lock there.
  • I use a script to run the program many times, but it occasionally got stuck.
  • The code I wrote is as follows, which could also be found in my branch.
  • Briefly speaking, I just want to do stage-synchronization-computation, where the inner loop is regarded as a stage.
  • I am not able to solve that.
  • Could you please help me with that?
    Thanks,
    CHE Yulin
#include <thread>
#include <iostream>

#include "ThreadPool.h"

using std::cout;
using std::endl;

int main() {
    using namespace nbsdx::concurrent;
    ThreadPool<20> pool;
    for (auto j = 0; j < 3000; j++) {
        cout << "Round:" << j << endl;
        for (int i = 0; i < 5000; ++i) {
            std::function<int(void)> task_function = [i]() {
                return i * i;
            };
            pool.AddJob(task_function);
        }
        cout << "Finish Add" << endl;
        pool.WaitAll();
    }
}

dynamic `ThreadCount`

Is there a way to make the number of threads (ThreadCount) dynamically used by the program?
Something like: ThreadPool pool(std::thread::hardware_concurrency())

Why use lk.unlock(); in line 151?

  • Could not understand one line of code
    • std::unique_lockstd::mutex lk, the std::unique_lockstd::mutex is designed with RAII, why do you add extra lk.unlock(); in line 151?
    • I am puzzled about that. Could you give me some hints? @nbsdx

Purpose of introducing jobs_left

Hi nbsdx,
I am confused about the introduction of jobs_left.
What is the purpose of introducing atomic_int type jobs_left(in line 27)?
I think, we could simply add the queue_mutex's lock in function WaitAll() (in line 148), rather than using the atomic_int variable jobs_left?
So, what is your consideration here?

Best,
CHE Yulin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.