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.
This was the first overview of the C++26 library. Let’s continue with the Concurrency.
Concurrency in C++26 has one dominant feature:
?
Modernes C++ Mentoring
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:
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.
领英推荐
An Overview of C++26: Concurrency
September 16, 2024/in C++26/by Rainer Grimm
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.
This was the first overview of the C++26 library. Let’s continue with the Concurrency.
Concurrency in C++26 has one dominant feature:
?
Modernes C++ Mentoring
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:
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.
Senior DSP/C++/Algorithm Developer at EXFO
2 个月Rainer Grimm Looks like ??add_42?? has to wait for ??hi ?? to complete. It doesn’t sound asynchronous to me on your conccurency description.