GithubHelp home page GithubHelp logo

thread_pool's Introduction

thread_pool

Thread pool using std::* primitives from C++11. The entire library is only one class (thread_pool) with one method (async) for submitting tasks.

An example that computes the primality of 2 to 10,000 using 8 threads (instead of creating 10,000 - 1 threads and causing your system to come to a grinding halt):

#include "thread_pool.hpp"

#include <iostream>
#include <list>
#include <utility>

using namespace std;

// Return the integer argument and a boolean representing its primality.
// We need to return the integer because the loop that retrieves results doesn't know which integer corresponds to which future.
pair<int, bool> is_prime(int n){
  for(int i = 2;i < n;i++)
    if(n % i == 0)
      return make_pair(i, false);
  return make_pair(n, true);
}

int main(){
  thread_pool pool(8);                   // Contruct a thread pool with 8 threads.
  list<future<pair<int, bool>>> results;
  for(int i = 2;i < 10000;i++){
  	// Add a task to the queue.
    results.push_back(pool.async(std::function<pair<int, bool>(int)>(is_prime), i));
  }
  
  for(auto i = results.begin();i != results.end();i++){
    pair<int, bool> result = i->get();  // Get the pair<int, bool> from the future<...>
    cout << result.first << ": " << (result.second ? "is prime" : "is composite") << endl;
  }
  return 0;
}

thread_pool::async is a templated method that accepts any std::function and arguments to pass to the function. It returns a std::future<Ret> where Ret is the return type of the aforementioned std::function.

To submit a task: future<Ret> fut(pool.async(std::function<Ret(Args...)>, args...));.

To wait on fut to complete: Ret result = fut.get();

thread_pool's People

Contributors

tyler-hardin avatar

Watchers

 avatar  avatar

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.