Giter VIP home page Giter VIP logo

threadpool's Introduction

threadpool

my threadpool with condition_variable, mutex, unique_lock, lock_guard, atomic

1. Naive Version

#include "threadpool.hpp"

void func(...)
{
    ...
}

constexpr int threadCount = 4;
ThreadPool tp(threadCount);

push task (with bind)

std::function<void()> task = std::bind(func, ...);
tp.pushTask(task);

push task (with lambda)

tp.pushTask(std::function<void()> { [](){ ... } });

2. Custom Version - custom future & promise

#include "threadpool.hpp"
#include "myFuture.hpp"

template <typename T>
void func_my_promise(ReturnObjectDelivery<T> promise, ...)
{
    ...
    T ret = a + b;
    promise.set_value(ret);
}

constexpr int threadCount = 4;
constexpr int taskCount = 4;
ThreadPool tp(threadCount);

std::vector<ReturnObject<int>> futures(taskCount);

push task (with bind)

ReturnObjectDelivery<int> promise;
futures[i].connectToROD(&promise);

std::function<void()> task = std::bind(func_my_promise<int>, promise, ...);
tp.pushTask(task);

push task (with lambda)

ReturnObjectDelivery<int> promise;
futures[i].connectToROD(&promise);

std::function<void()> task = [promise, ...]() { func_my_promise<int>(promise, ...); };
tp.pushTask(task);

get return value

auto ret = futures[idx].get();

3. Standard Version - std::future & std::packaged_task

#include "threadpool.hpp"
#include <future>

template <typename T>
T func_std_package(T a, T b, std::mutex *mtxCout)
{
    ...
    T ret = a + b;
    return ret;
}

constexpr int threadCount = 4;
constexpr int taskCount = 4;
ThreadPool tp(threadCount);

std::vector<std::future<int>> futures;

push task

auto sp_packaged_task = std::make_shared<std::packaged_task<int()>>(
    std::bind(func_std_package<int>, a, b, &(tp.mtxCout)));

futures.emplace_back(sp_packaged_task->get_future());

std::function<void()> task = [sp_packaged_task](){ (*sp_packaged_task)(); };
tp.pushTask(task);

get return value

auto ret = futures[idx].get();

4. Example Output [threadCount = 4, taskCount = 4]

thread idx = 0 generated & detached
        thread 2 starting...
        thread 2 waiting...
thread idx = 1 generated & detached
        thread 3 starting...
        thread 3 waiting...
thread idx = 2 generated & detached
        thread 4 starting...
        thread 4 waiting...
thread idx = 3 generated & detached
        thread 5 starting...
        thread 5 waiting...
        thread 5 awaken !
                        task pushed
                        task pushed
                        task pushed
        thread 2 awaken !
        thread 2 tasking...
        thread 5 tasking...
        thread 4 awaken !
        thread 4 tasking...
        thread 3 awaken !
                        task pushed
        thread 3 tasking...
                [inside task .. thread 3]
                [inside task .. thread 4]
        thread 4 task (done)
        thread 4 waiting...
        thread 3 task (done)
                [inside task .. thread 5]
        thread 5 task (done)
        thread 3 waiting...
                [inside task .. thread 2]
task 1 return value = 40
task 2 return value = 40
        thread 2 task (done)
        thread 5 waiting...
task 3 return value = 40
        thread 2 waiting...
task 4 return value = 40
releaseWorkers()...
all task assigned
        thread 2 awaken !
        thread 2 terminated
        thread 3 awaken !
        thread 3 terminated
all task done
        thread 4 awaken !
        thread 5 awaken !
        thread 4 terminated
        thread 5 terminated
all thread terminated

threadpool's People

Contributors

peozh 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.