Skip to content

Curve/poolparty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“ƒ Description

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

Note

This library does not handle exceptions. Please make sure to not throw from within tasks!

πŸ“¦ 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); });

About

🌊 A simple, yet versatile C++ Thread-Pool library

Resources

License

Stars

2 stars

Watchers

1 watching

Forks

Contributors