GithubHelp home page GithubHelp logo

poolparty's Introduction

๐Ÿ“ƒ Description

poolparty is a simple and versatile C++20 thread-pool library.

๐Ÿ“ฆ Installation

  • Using CPM

    CPMFindPackage(
      NAME           poolparty
      VERSION        3.0.0
      GIT_REPOSITORY "https://github.com/Curve/poolparty"
    )
  • Using FetchContent

    include(FetchContent)
    
    FetchContent_Declare(poolparty GIT_REPOSITORY "https://github.com/Curve/poolparty" GIT_TAG v3.0.0)
    FetchContent_MakeAvailable(poolparty)
    
    target_link_libraries(<target> cr::poolparty)

๐Ÿ“– Examples

#include <iostream>
#include <poolparty/pool.hpp>

int expensive_calculation(int x)
{
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return x + 1;
}

int main()
{
    poolparty::pool pool{2};

    // Pause execution
    pool.pause();

    auto fut1 = pool.submit(expensive_calculation, 1);
    auto fut2 = pool.submit(expensive_calculation, 2);

    // Let's add an additional thread
    auto source = pool.add_thread();
    auto fut3   = pool.submit(expensive_calculation, 3);

    // Fire and forget
    pool.forget([]() { expensive_calculation(100); });

    // Resume execution
    pool.resume();

    std::cout << "Result: " << fut1.get() << ", " << fut2.get() << " and " << fut3.get() << std::endl;

    // ...and remove the thread we've added
    source.request_stop();
    pool.cleanup();

    return 0;
}

It's also possible to use a priority queue for more fine grained execution, see tests:

poolparty::pool<std::priority_queue, priority_task> pool{1};
expect(eq(pool.size(), 1));
expect(eq(pool.tasks(), 0));
pool.pause();
std::vector<int> order;
auto fut0 = pool.submit<false, 1>([&]() { order.emplace_back(1); });
auto fut1 = pool.submit<false, 2>([&]() { order.emplace_back(2); });
auto fut2 = pool.submit<false, 3>([&]() { order.emplace_back(3); });
auto fut5 = pool.submit<false, 6>([&]() { order.emplace_back(6); });
auto fut3 = pool.submit<false, 4>([&]() { order.emplace_back(4); });
auto fut4 = pool.submit<false, 5>([&]() { order.emplace_back(5); });

poolparty's People

Contributors

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