SiddiqSoft.asynchrony 2.2.2

dotnet add package SiddiqSoft.asynchrony --version 2.2.2
                    
NuGet\Install-Package SiddiqSoft.asynchrony -Version 2.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SiddiqSoft.asynchrony" Version="2.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SiddiqSoft.asynchrony" Version="2.2.2" />
                    
Directory.Packages.props
<PackageReference Include="SiddiqSoft.asynchrony" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SiddiqSoft.asynchrony --version 2.2.2
                    
#r "nuget: SiddiqSoft.asynchrony, 2.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SiddiqSoft.asynchrony@2.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SiddiqSoft.asynchrony&version=2.2.2
                    
Install as a Cake Addin
#tool nuget:?package=SiddiqSoft.asynchrony&version=2.2.2
                    
Install as a Cake Tool

alternate text is missing from this package README image

asynchrony : Add asynchrony to your apps

Build Status alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Documentation

For comprehensive documentation, visit our GitHub Pages:

Motivation

  • We needed to add asynchrony to our code.
    • A periodic worker
    • A resource pool
    • A round robin pool
    • A simple thread pool
    • A simple worker pool
  • The code here is a set of helpers that utilize the underlying deque, semaphore, mutex features found in std.
  • Be instructive while providing functional code.
  • Use only C++20 standard code: jthread, deque, semaphore, and concepts
  • Depends on RunOnEnd for encapsulating cleanup code on destructor.

Usage

Requires C++20 support!

Specifically we require jthread and stop_token support. This library works with GCC 10+, MSVC 16.11+, or Clang 10+.

The library uses concepts to ensure the type T meets move construct requirements.

Single threaded worker

#include "siddiqsoft/simple_worker.hpp"
#include <chrono>
#include <iostream>
#include <format>

// Define your data
struct MyWork
{
   std::string urlDestination{};
   std::string data{};
   void operator()(){
      // magic_post_to(urlDestination, data);
      std::cout << "Processing: " << urlDestination << std::endl;
   }
};

int main()
{
   // Declare worker with our data type and the driver function.
   siddiqsoft::simple_worker<MyWork> worker{[](auto& item){
                                              // call the item's operator()
                                              // to invoke actual work.
                                              item();
                                           }};
   // Fire 100 items
   for( int i=0; i < 100; i++ )
   {
      // Queues into the single worker
      worker.queue({std::format("https://localhost:443/test?iter={}",i),
                    "hello-world"});
   }

   // As the user, you must control the lifetime of the worker
   // Trying to delete the worker will cause it to stop
   // and abandon any items in the internal deque.
   std::this_thread::sleep_for(std::chrono::seconds(1));
   return 0;
}

Multi-threaded worker pool

#include "siddiqsoft/simple_pool.hpp"
#include <chrono>
#include <iostream>
#include <format>

int main()
{
   // Declare worker with our data type and the driver function.
   siddiqsoft::simple_pool<MyWork> worker{[](auto& item){
                                           // call the item's operator()
                                           // to invoke actual work.
                                           item();
                                        }};
   // Fire 100 items
   for( int i=0; i < 100; i++ )
   {
      // Queues into the single queue but multiple worker threads
      // (defaults to CPU thread count)
      worker.queue({std::format("https://localhost:443/test?iter={}",i),
                    "hello-world"});
   }

   // As the user, you must control the lifetime of the worker
   // Trying to delete the worker will cause it to stop
   // and abandon any items in the internal deque.
   std::this_thread::sleep_for(std::chrono::seconds(1));
   return 0;
}

Multi-threaded roundrobin pool

#include "siddiqsoft/roundrobin_pool.hpp"
#include <chrono>
#include <iostream>
#include <format>

int main()
{
   // Declare worker with our data type and the driver function.
   siddiqsoft::roundrobin_pool<MyWork> worker{[](auto& item){
                                               // call the item's operator()
                                               // to invoke actual work.
                                               item();
                                             }};
   // Fire 100 items
   for( int i=0; i < 100; i++ )
   {
      // Queues into the thread pools individual queue by round-robin
      // across the threads with simple counter.
      // (defaults to CPU thread count)
      worker.queue({std::format("https://localhost:443/test?iter={}",i),
                    "hello-world"});
   }

   // As the user, you must control the lifetime of the worker
   // Trying to delete the worker will cause it to stop
   // and abandon any items in the internal deque.
   std::this_thread::sleep_for(std::chrono::seconds(1));
   return 0;
}

Periodic Worker

Execute a function at regular intervals:

#include "siddiqsoft/periodic_worker.hpp"
#include <chrono>
#include <iostream>

int main()
{
   // Create a periodic worker that executes every 500ms
   siddiqsoft::periodic_worker<> timer{
      []() {
         std::cout << "Tick!" << std::endl;
      },
      std::chrono::milliseconds(500)
   };

   // Keep the program running
   std::this_thread::sleep_for(std::chrono::seconds(5));
   return 0;
}

Resource Pool

Provides a basic resource pool useful for keeping a pool of connection objects for the various threadpools to checkout/checkin.

namespace siddiqsoft {
    template<typename T>
    class resource_pool {
        public:
        auto size();                           // Get current pool size
        [[nodiscard]] T checkout();            // Checkout a resource (throws if empty)
        void checkin(T&& rsrc);                // Return a resource to the pool
        void clear();                          // Clear all resources from the pool
    };
}

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

<p align="right"> © 2021 Siddiq Software LLC. All rights reserved. </p>

Product Compatible and additional computed target framework versions.
native native is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on SiddiqSoft.asynchrony:

Package Downloads
SiddiqSoft.restcl

Focussed REST Client for modern C++

SiddiqSoft.CosmosClient

Azure Cosmos REST-API Client for Modern C++

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.2 29 7/4/2026
2.2.1 41 7/3/2026
2.1.5 106 6/21/2026
2.1.4 150 6/10/2026
2.1.3 201 6/2/2026
2.1.2 176 5/21/2026
2.1.1 271 4/28/2026
1.9.3 537 4/6/2025
1.9.2 471 1/14/2025
1.9.1 474 1/10/2025
1.9.0 480 1/10/2025
1.8.0 1,001 12/16/2024
1.7.3 648 12/7/2024
1.7.2 501 12/7/2024
1.7.1 497 12/7/2024
1.6.1 744 10/28/2024

Refer to the project repository for release notes. Documentation at https://siddiqsoft.github.io/asynchrony/