Code Monkey home page Code Monkey logo

threadpool's Introduction

ThreadPool

ThreadPool implementation in C++. The main purpose of creating this project was to learn threadpool. The code is easy to read and understand. If you don't know about threadpool. Read here

Compilation

Tested on Linux Machine. Compile using below command

g++ -std=c++11 main.cpp -pthread

Usage

  1. Create a threadpool of three threads
Tpool pool(3);

It will create three workers which will execute tasks submitted to the threadpool.

  1. Queue a work. If we have a function accepting two integer arguments and returning their sum( e.g - int add(int, int)), we can queue it using
auto fut1 = pool.enq(add, 100, 200);

You have to pass function name, followed by the arguments accepted by the function. No need to worry about the function signature. It returns an appropriate future object(future<int> in the above case). You can use auto to catch the return future object from the threadpool without worrying about the return type. To print the result, use get() method of future object.

std::cout << "foo returned: " << fut1.get() << "\n";

Lets understand another example. Suppose we have a class Test

struct Test{};

There are two functions - one accepts rvalue reference and another lvalue reference of Test object as args.

void foo(Test&& obj);
void bar(Test& obj);

You can queue these functions in our threadpool and pass rvalue/lvalue. Threadpool will make sure that the value type of argument is preserved when executing the work.

Test t;
pool.enq(bar, t);  //threadpool forwards arg as lvalue
pool.enq(foo, Test()); //threadpool forwards arg as rvalue

There are more examples in the file main.cpp

Implementation Overview

Below is the threadpool class overview with its members.

class Tpool {
using Task = std::function<void(void)>;
public:
    Tpool(std::size_t sz);
    ~Tpool();
	template<typename T, typename ... Args>
    auto enq(T&& func, Args&& ... args) -> std::future<decltype(func(std::forward<Args>(args)...))>();
private:
	void start();
	void stop() noexcept;

    std::queue<Task> q;
    std::vector<std::thread> workers;
    std::mutex mu;
    std::condition_variable cv;

    bool pstop = false;
    std::size_t sz;
};

License

THE MIT LICENSE Copyright (c) 2020 Kumar Sourav

threadpool's People

Contributors

ku-sourav 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.