An Overview of C++26: Concurrency

Today, I will finish my overview of C++26 and write about concurrency.

There are still two library features left before I jump into the concurrency: saturation arithmetic and debugging support.

Saturation Arithmetic

Saturation arithmetic is a version of arithmetic in which all operations, such as addition and multiplication, are limited to a fixed range between a minimum and maximum value.

If the result of an operation is greater than the maximum, it is set (“clamped“) to the maximum; if it is below the minimum, it is clamped to the minimum. The name comes from how the value becomes “saturated” once it reaches the extreme values; further additions to a maximum or subtractions from a minimum will not change the result. (https://en.wikipedia.org/wiki/Saturation_arithmetic)

C++26 introduced a set of saturating arithmetic operations: addition, subtraction, multiplication, division, and saturate cast. If the specified integral type T cannot represent the result of the operation, the result is instead std::numeric_limits::min<T>() or std::numeric_limits::max<T>() (whichever is closer).

cppreference.com has a nice example with an explanation to std::add_sat.

#include <climits>
#include <limits>
#include <numeric>
 
static_assert(CHAR_BIT == 8);
static_assert(UCHAR_MAX == 255);
 
int main()
{
    constexpr int a = std::add_sat(3, 4); // no saturation occurs, T = int
    static_assert(a == 7);
 
    constexpr unsigned char b = std::add_sat<unsigned char>(UCHAR_MAX, 4); // saturated
    static_assert(b == UCHAR_MAX);
 
    constexpr unsigned char c = std::add_sat(UCHAR_MAX, 4); // not saturated, T = int
        // add_sat(int, int) returns int tmp == 259,
        // then assignment truncates 259 % 256 == 3
    static_assert(c == 3);
 
//  unsigned char d = std::add_sat(252, c); // Error: inconsistent deductions for T
 
    constexpr unsigned char e = std::add_sat<unsigned char>(251, a); // saturated
    static_assert(e == UCHAR_MAX);
        // 251 is of type T = unsigned char, `a` is converted to unsigned char value;
        // might yield an int -> unsigned char conversion warning for `a`
 
    constexpr signed char f = std::add_sat<signed char>(-123, -3); // not saturated
    static_assert(f == -126);
 
    constexpr signed char g = std::add_sat<signed char>(-123, -13); // saturated
    static_assert(g == std::numeric_limits<signed char>::min()); // g == -128
}

T is the type of both function arguments:

template< class T >
constexpr T add_sat( T x, T y ) noexcept;

debugging Support

C++26 has three functions to deal with debugging.

  • std::breakpoint: pauses the running program when called and passes the control to the debugger
  • std::breakpoint_if_debugging: calls std::breakpoint if std::is_debugger_present returns true
  • std::is_debugger_present: checks whether a program is running under the control of a debugger

This was the first overview of the C++26 library. Let’s continue with the Concurrency.

Concurrency in C++26 has one dominant feature:

Rainer D 6 P2 500x500

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Generic Programming (Templates) with C++": October 2024
  • "Embedded Programming with Modern C++": October 2024
  • "Clean Code: Best Practices for Modern C++": March 2025
  • Do you want to stay informed: Subscribe.

     

    std::execution

    std::execution, previously known as executors or Senders/Receivers, provides “a Standard C++ framework for managing asynchronous execution on generic execution resources“. (P2300R10). It has three key abstractions: schedulers, senders, and receivers, and a set of customizable asynchronous algorithms.

    The “Hello word” program of the proposal P2300R10 shows them.

    using namespace std::execution;
    
    scheduler auto sch = thread_pool.scheduler();                                 // 1
    
    sender auto begin = schedule(sch);                                            // 2
    sender auto hi = then(begin, []{                                              // 3
        std::cout << "Hello world! Have an int.";                                 // 3
        return 13;                                                                // 3
    });                                                                           // 3
    sender auto add_42 = then(hi, [](int arg) { return arg + 42; });              // 4
    
    auto [i] = this_thread::sync_wait(add_42).value();  
    

    The explanation of the example is so good that I will directly quote them here:

    This example demonstrates the basics of schedulers, senders, and receivers:

    1. First we need to get a scheduler from somewhere, such as a thread pool. A scheduler is a lightweight handle to an execution resource.
    2. To start a chain of work on a scheduler, we call § 4.19.1 execution::schedule, which returns a sender that completes on the scheduler. A sender describes asynchronous work and sends a signal (value, error, or stopped) to some recipient(s) when that work completes.
    3. We use sender algorithms to produce senders and compose asynchronous work. § 4.20.2 execution::then is a sender adaptor that takes an input sender and a std::invocable, and calls the std::invocable on the signal sent by the input sender. The sender returned by then sends the result of that invocation. In this case, the input sender came from schedule, so its void, meaning it won’t send us a value, so our std::invocable takes no parameters. But we return an int, which will be sent to the next recipient.
    4. Now, we add another operation to the chain, again using § 4.20.2 execution::then. This time, we get sent a value – the int from the previous step. We add 42 to it, and then return the result.
    5. Finally, we’re ready to submit the entire asynchronous pipeline and wait for its completion. Everything up until this point has been completely asynchronous; the work may not have even started yet. To ensure the work has started and then block pending its completion, we use § 4.21.1 this_thread::sync_wait, which will either return a std::optional<std::tuple<...>> with the value sent by the last sender, or an empty std::optional if the last sender sent a stopped signal, or it throws an exception if the last sender sent an error.

    The proposal provides further excellent examples. I will analyze them in upcoming posts.

    Read-Copy Update (RCU) and Hazard Pointers

    Read-Copy Update and Hazard Pointers solve the classical problem of lock-free data structures such as a lock-free stack: When can a thread safely delete a data structure node while other threads can use this node simultaneously?

    Those structures are too special and need too much information to discuss in an overview article.

    What’s next?

    In the next post about C++26, I will change my focus and dive into the details.

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 176 5506 5086
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org

    Modernes C++ Mentoring,