Giter Site home page Giter Site logo

lingshaomao / coroutine-thread-pool.h Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jks-liu/coroutine-thread-pool.h

0.0 0.0 0.0 12 KB

Thread pool which supports c++20 coroutine. 一个支持c++20协程的线程池。

License: MIT License

C++ 100.00%

coroutine-thread-pool.h's Introduction

coroutine-thread-pool.h

Thread pool which supports c++20 coroutine. 一个支持c++20协程的线程池。

API

// Thread pool class
jks::thread_pool;

// Singleton to get thread pool
jks::thread_pool::get();

// Submit a task to thread pool
jks::thread_pool.submit(std::invocable)

// Return type of submit, which can be used to get task result
jks::future<T>

// Awaitbale class which submit coroutine to thread pool
jks::thread_pool::awaitable

Example

#include <iostream>
#include <chrono>
#include <string>

#include "thread-pool.h"

using namespace jks;

void a_ordinary_function_return_nothing()
{
    std::cout << __func__ << std::endl;
}

std::string a_ordinary_function_return_string()
{
    return std::string(__func__);
}

future<void> a_coroutine_return_nothing()
{
    co_await thread_pool::awaitable<void>();
    std::cout << __func__ << std::endl;
}

future<std::string> a_coroutine_return_string()
{
    auto h = co_await thread_pool::awaitable<std::string>();
    h.promise().set_value(__func__);
}


std::string a_function_calling_a_coroutine()
{
    auto r = a_coroutine_return_string();
    return r.get() + " in " + __func__;
}

// You can submit your coroutine handle in your own awaitable
// This implementation is a simplified version of jks::thread_pool::awaitable
struct submit_awaitable: std::suspend_never
{
    void await_suspend(std::coroutine_handle<> h)
    {
        thread_pool::get(0).submit_coroutine(h);
    }
};

future<void> submit_raw_coroutine_handle()
{
    co_await submit_awaitable();
    std::cout << __func__ << std::endl;
}

int main()
{
    using namespace std::chrono_literals;

    constexpr auto n_pool = 3;
    // get thread pool singleton
    auto& tpool = thread_pool::get(n_pool);

    // Ordinary function
    tpool.submit(a_ordinary_function_return_nothing);
    auto func_return_sth = tpool.submit(a_ordinary_function_return_string);

    // Coroutine
    tpool.submit(a_coroutine_return_nothing);
    auto coro_return_sth = tpool.submit(a_coroutine_return_string);

    // Function calling coroutine
    auto func_calling_coro = tpool.submit(a_function_calling_a_coroutine);

    // Raw coroutine handle
    submit_raw_coroutine_handle();
    
    std::this_thread::sleep_for(1s);

    // Also support lambda
    for (int i=0; i<=n_pool; ++i) {
        tpool.submit([i]() -> int{
            std::cout << "* Task " << i << '+' << std::endl;
            std::this_thread::sleep_for(3s);
            std::cout << "* Task " << i << '-' << std::endl;
            return i;
        });
    }
    std::this_thread::sleep_for(1s);


    std::cout << func_return_sth.get() << std::endl;
    std::cout << coro_return_sth.get().get() << std::endl;
    std::cout << func_calling_coro.get() << std::endl;

    // Destructor of thread_pool blocks until tasks current executing completed
    // Tasks which are still in queue will not be executed
    // So above lambda example, Task 3 is not executed
}

Result (your result may be a little different due to multi-threading):

$ g++ --version
g++ (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++20 -pthread -Wall -Wextra -Isrc test/main.cpp -o main
$ ./main
a_ordinary_function_return_nothing
submit_raw_coroutine_handle
a_coroutine_return_nothing
* Task 0+
* Task 2+
* Task 1+
a_ordinary_function_return_string
a_coroutine_return_string
a_coroutine_return_string in a_function_calling_a_coroutine
* Task * Task 02--
* Task 1-

Check memory leak

Valgrind

$ g++ -DDEBUG_VALGRIND_ -g -std=c++20 -pthread -Wall -Wextra -Isrc test/main.cpp -o main
$ valgrind --tool=memcheck ./main

Or you can bus gcc builtin santitizer.

$ g++ -fsanitize=address -std=c++20 -pthread -Wall -Wextra -Isrc test/main.cpp -o main
$ ./main

coroutine-thread-pool.h's People

Contributors

jks-liu 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.