Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

asyncpp

> 编程语言
Open source

C++ async operations

100 stars0 likes1 views
WebsiteGitHub

About

C++ async operations

asyncpp

Asyncpp is a C++ utility library for asynchronous or functional programming using modern C++ lambdas, without getting into callback hell. It is ideally suited for use with Boost ASIO.

This was inspired by the popular async Node.js library.

What problem does this solve?

In asynchronous programming (e.g. network programming using Boost ASIO, where the thread doesn't block on network calls) you lose the ability to pass data back to a calling function via return values. And if there are many chained asynchronous operations, or some combination of serial and parallel asynchronous operations, then you quickly wind up with a mess of callbacks.

This library helps by packaging several common patterns of asynchronous operations, to keep your code clean and reasonable.

Here's a contrived example. Imagine we have to call a blocking function three times in a row. If any of the invocations returns false, we want to return false to the caller.

…

This now works, but we're in callback hell.

Cleaned up with Asyncpp

Using Asyncpp library, the code becomes:

cpp
using KeepGoingCallback = std::function;
using FinalCallback = std::function;

void func_async(KeepGoingCallback callback);

void call_function_three_times_async(FinalCallback final_callback) {
    async::ntimes(3, func_async, final_callback);
}

We've completely generalized the pattern of multiple serial calls to an asynchronous function.

The Asyncpp library also has functions for parallel calls, parallel calls with a limit on the number of simultaneous outstanding calls, loops, filters, and more.

Boost example

Here's an example using the Boost ASIO network library. Instead of writing:

…

with asyncpp we can write this as a flat sequence of steps:

…

Functions

each

Takes an input vector and a function, and applies that function to each element in the vector

map

Takes an input vector and a function, and applies that function to each element in the vector. Returns (via the final_callback) a new vector with the transformed values.

series

Invokes a series of tasks, and collects result values into a vector. As each task completes, it invokes a callback with an error code and a result value. If the error code is not async::OK, iteration stops. Once all tasks complete or there is an error, final_callback is called with the last error and the results vector.

Each task may invoke its callback immediately, or at some point in the future.

parallel

Invokes a series of tasks, each of which produces some output value, and aggregates the results into an output vector.

A task may run to completion immediately, or it may defer calling its completion callback. The latter would be if using with an asynchronous execution framework like Boost ASIO. If each task runs to completion immediately, then this call becomes equivalent to series.

There is no limit on how many calls may be outstanding at the same time.

parallelLimit

Same as parallel, but allows the setting of a limit on how many tasks may be concurrently outstanding.

filter

Takes a vector of input data, and passes each element through a test function that returns true or false. If true, that element is added to an output vector.

reject

Similar to filter, except each element is added to the output vector if the test function returns false.

whilst

Takes two functions, test and func. It repeatedly calls test, and if that returns true, then calls func. It test ever returns false or func passes a non-OK (non-zero) error code to its callback, then the whilst function stops.

doWhilst

Similar to whilst, except if follows do..while control flow. That is, it calls func first, then test. Otherwise, the same rules rules.

until

Similar to whilst, except that instead of stopping when test returns false, it stops when test returns true.

doUntil

Similar to doWhilst, except that instead of stopping when test returns false, it stops when test returns true.

forever

Executes a function repetedly, until it passes a non-OK (non-zero) error code to its callback. This is equivalent to whilst with a test function that always returns true.

ntimes

Executes a function a given number of times, or until it passes a non-OK (non-zero) error code to its callback.

Summary

Function Concurrency Executes vec of functions Applies single function to data vec Returns vec of results Output vec same size as input
each limit = n no yes no n/a
map limit = n no yes yes yes
series 1 yes no yes yes
parallel no limit yes no yes yes
parallelLimit limit = n yes no yes yes
filter limit = n no yes yes no
reject limit = n no yes yes no
whilst 1 no no no n/a
doWhilst 1 no no no n/a
until 1 no no no n/a
doUntil 1 no no no n/a
forever 1 no no no n/a
ntimes 1 no no no n/a

Examples

Build using scons. Binaries will be in bin/ directory.

Examples are in /examples directory.

Run tests with scons test.

Requirements

  • C++11.
  • SCons (brew install scons)
  • Boost (brew install boost)

Tested with:

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

This library was developed primarily for use with Boost ASIO, but should support other single-thread asynchronous frameworks. It has not been tested with multithreaded code.


Made with :horse: by Paul Rademacher.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言