asynchrony

asynchrony : Asynchrony support library

CodeQL Build Status

Documentation

Getting started

Quick Start

Single-threaded Worker

#include "siddiqsoft/simple_worker.hpp"

struct MyWork {
   std::string urlDestination{};
   std::string data{};
   void operator()(){
      // Process work
   }
};

int main() {
   siddiqsoft::simple_worker<MyWork> worker{[](auto& item){
      item();
   }};
   
   for(int i=0; i < 100; i++) {
      worker.queue({std::format("https://localhost:443/test?iter={}",i),
                    "hello-world"});
   }

   std::this_thread::sleep_for(std::chrono::seconds(1));
   return 0;
}

Multi-threaded Pool

#include "siddiqsoft/simple_pool.hpp"

int main() {
   siddiqsoft::simple_pool<MyWork> pool{[](auto& item){
      item();
   }};
   
   for(int i=0; i < 100; i++) {
      pool.queue({std::format("https://localhost:443/test?iter={}",i),
                  "hello-world"});
   }

   std::this_thread::sleep_for(std::chrono::seconds(1));
   return 0;
}

Round-Robin Pool

#include "siddiqsoft/roundrobin_pool.hpp"

int main() {
   siddiqsoft::roundrobin_pool<MyWork> pool{[](auto& item){
      item();
   }};
   
   for(int i=0; i < 100; i++) {
      pool.queue({std::format("https://localhost:443/test?iter={}",i),
                  "hello-world"});
   }

   std::this_thread::sleep_for(std::chrono::seconds(1));
   return 0;
}

Periodic Worker

#include "siddiqsoft/periodic_worker.hpp"

int main() {
   siddiqsoft::periodic_worker<> timer{
      []() { std::cout << "Tick!" << std::endl; },
      std::chrono::milliseconds(1000)
   };

   std::this_thread::sleep_for(std::chrono::seconds(5));
   return 0;
}

API

Utility Description
siddiqsoft::simple_worker Provides a single thread with an internal deque.
Use this to make any “long” task asynchronous.
Use instead of std::async.
Just register your callback/lambda and you’re done. No need to worry about waiting for the result (no futures or waiting on them).
Your declared callback will be invoked!
siddiqsoft::simple_pool Implements an array of threads backed with a single deque. Each thread waits for and processes the next available item from the single deque.
siddiqsoft::roundrobin_pool Implements a vector of basic_workers (each worker has its independent queue therefore minimizing contention time).
The queue method implements a running counter based round-robin feeder.
siddiqsoft::periodic_worker Provides a facility where you can have your function/lambda invoked at a given periodic rate (in microseconds).
siddiqsoft::resource_pool Provides a basic resource pool useful for keeping a pool of connection objects for the various threadpools to checkout/checkin.

Implementation note

In order to use std::jthread on Clang 10 and later, we enable the compiler flag "CMAKE_CXX_FLAGS": "-fexperimental-library" in the CMakeLists.txt. This option will show up in your client library under Clang compilers.


Author: Siddiq Software LLC

© 2021 Siddiq Software LLC. All rights reserved.