Code Monkey home page Code Monkey logo

threadpool's Introduction

My Header-Only Thread Pool Library

Installation

Run sudo make install to install the library to /usr/local/include/.

If you want to install it elsewhere, you can run the command as INCLUDEDIR=/some/other/install/directory sudo make install.

Uninstallation

Run sudo make uninstall to uninstall the library from the $INCLUDEDIR environment variable (defaults to /usr/local/include/).

If you have forgotten where you installed it for some reason, you can see where it's located with the c-preprocessor (via cpp or g++ -E) and just remove it yourself. It's just header file.

Usage

An example usage is below. Please make sure to run your compilation command with the -pthread flag.

#include <iostream>
#include <string>
#include <unistd.h>
#include <ThreadPool> // Include my library

int main(void){
    uint nproc = std::thread::hardware_concurrency();

    ThreadPool p(nproc); // Initialize the thread pool with nproc threads
    std::vector<std::future<std::string>> futures; futures.resize(nproc); // Create a vector of futures

    char sc = 'A';
    for(auto& f : futures){
        // Enqueue the lambda task with the ThreadPool, storing the future so we can get our result later (and to prevent blocking).
        f = p.enqueue([](const char c) {
            sleep(2); // Sleep for 2 seconds. The *entire program* should only sleep for 2-3 seconds, due to the multithreading.
            return std::string(20, c);
        }, sc++);
    }

    // Print out our results.
    for(auto& f : futures){
        std::cout << f.get() << "\n";
    }

    std::cout << std::flush;
    return 0;
}

Why

I needed a thread pool and wanted to learn more about C++ threading and templating.

threadpool's People

Contributors

cjacob314 avatar

Watchers

 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.