News

In an Atomic World -- Lucian Radu Teodorescu

logo.pngAtomics form a relatively low level, but fundamental part of sharing data across threads. Lucian Radu Teodorescu reminds us what atomics are and how and when to use them.

In an Atomic World

by Lucian Radu Teodorescu

From the article:

We often discuss mutexes as the basic building blocks of concurrency. However, there are more fundamental concepts upon which concurrent programs and synchronization primitives are constructed. The C++ language defines a memory model, which describes how programs behave when multiple threads are involved. Additionally, C++ introduces atomic operations that serve as foundation for working with data across threads, ensuring both safety and performance. The goal of C++ atomics is to closely align with the hardware and eliminate the need for lower-level operations that must work across threads.

The topic of atomics is often overlooked, and the prevailing advice is to avoid them. While this advice is generally sound, there are occasions when we need to use atomics to fully leverage the language’s capabilities. This article aims to give atomics the attention they deserve, as they have yet to be featured in an Overload article.

The subject of atomics is extensive. For a comprehensive exploration, readers are encouraged to consult books by Anthony Williams [Williams19] and Mara Bos [Bos23]. While the Bos book primarily focuses on Rust, there is still much to be learned about atomics for C++ programmers. The reader can also consider cppreference.com for a quick reference to the atomics library [cppreference-1] In this article, we will examine various memory ordering models and illustrate their usage through simplified practical examples.

2024-09 Mailing Available

The 2024-09 mailing of new standards papers is now available.

 

WG21 Number Title Author Document Date Mailing Date Previous Version Subgroup
N4990 Business Plan and Convener's Report Herb Sutter 2024-08-26 2024-09   All of WG21
P0472R2 Put std::monostate in <utility> David Sankel 2024-09-09 2024-09 P0472R1 LEWG Library Evolution
P1030R7 std::filesystem::path_view Niall Douglas 2024-09-06 2024-09 P1030R6 LEWG Library Evolution
P1061R9 Structured Bindings can introduce a Pack Barry Revzin 2024-08-24 2024-09 P1061R8 CWG Core
P2019R7 Thread attributes Corentin Jabot 2024-09-16 2024-09 P2019R6 LWG Library
P2287R3 Designated-initializers for base classes Barry Revzin 2024-09-09 2024-09 P2287R2 EWG Evolution
P2319R1 Prevent path presentation problems Victor Zverovich 2024-09-14 2024-09 P2319R0 LEWG Library Evolution
P2688R2 Pattern Matching: `match` Expression Michael Park 2024-09-17 2024-09 P2688R1 EWG Evolution
P2786R7 Trivial Relocatability For C++26 Mungo Gill 2024-09-17 2024-09 P2786R6 EWG Evolution,LEWG Library Evolution
P2835R5 Expose std::atomic_ref's object address Gonzalo Brito Gadeschi 2024-09-02 2024-09 P2835R4 LEWG Library Evolution
P2835R6 Expose std::atomic_ref's object address Gonzalo Brito Gadeschi 2024-09-03 2024-09 P2835R5 LEWG Library Evolution,LWG Library
P2841R4 Concept and variable-template template-parameters Corentin Jabot 2024-09-14 2024-09 P2841R3 CWG Core
P2846R3 reserve_hint: Eagerly reserving memory for not-quite-sized lazy ranges Corentin Jabot 2024-09-14 2024-09 P2846R2 LEWG Library Evolution
P2879R0 Proposal of std::dump Andrew Tomazos 2024-09-17 2024-09   LEWGI SG18: LEWG Incubator,SG20 Education
P2945R1 Additional format specifiers for time_point Barry Revzin 2024-09-09 2024-09 P2945R0 LEWG Library Evolution
P2988R7 std::optional<T&> Steve Downey 2024-09-10 2024-09 P2988R6 LEWG Library Evolution,LWG Library
P3016R4 Resolve inconsistencies in begin/end for valarray and braced initializer lists Arthur O'Dwyer 2024-09-14 2024-09 P3016R3 LWG Library
P3019R9 Vocabulary Types for Composite Class Design Jonathan Coe 2024-09-15 2024-09 P3019R8 LEWG Library Evolution,LWG Library
P3037R3 constexpr std::shared_ptr Paul Keir 2024-09-03 2024-09 P3037R2 LEWG Library Evolution
P3074R4 trivial unions (was std::uninitialized<T>) Barry Revzin 2024-09-09 2024-09 P3074R3 EWG Evolution
P3096R3 Function Parameter Reflection in Reflection for C++26 Adam Lach 2024-09-15 2024-09 P3096R2 EWG Evolution,LEWG Library Evolution
P3128R1 Graph Library: Algorithms   2024-09-12 2024-09 P3128R0 SG14 Low Latency,SG19 Machine Learning
P3128R2 Graph Library: Algorithms   2024-09-12 2024-09 P3128R1 SG14 Low Latency,SG19 Machine Learning
P3210R2 A Postcondition *is* a Pattern Match Andrew Tomazos 2024-09-09 2024-09 P3210R1 SG21 Contracts,EWG Evolution
P3245R2 Allow `[[nodiscard]]` in type alias declarations Xavier Bonaventura 2024-09-15 2024-09 P3245R1 EWGI SG17: EWG Incubator
P3248R2 Require [u]intptr_t Gonzalo Brito Gadeschi 2024-09-06 2024-09 P3248R1 EWG Evolution,LEWG Library Evolution
P3290R2 Integrating Existing Assertions With Contracts Joshua Berne 2024-09-06 2024-09 P3290R1 SG21 Contracts,EWG Evolution
P3295R1 Freestanding constexpr containers and constexpr exception types Ben Craig 2024-09-15 2024-09 P3295R0 LEWG Library Evolution
P3299R1 Range constructors for std::simd Daniel Towner 2024-09-16 2024-09 P3299R0 LEWG Library Evolution
P3309R2 constexpr atomic and atomic_ref Hana Dusíková 2024-08-29 2024-09 P3309R1 LEWG Library Evolution
P3335R1 Structured Core Options René Ferdinand Rivera Morell 2024-09-17 2024-09 P3335R0 SG15 Tooling
P3371R1 Fix C++26 by making the rank-1, rank-2, rank-k, and rank-2k updates consistent with the BLAS Mark Hoemmen 2024-09-14 2024-09 P3371R0 LEWG Library Evolution
P3372R1 constexpr containers and adapters Hana Dusíková 2024-09-17 2024-09 P3372R0 LEWG Library Evolution
P3375R0 Reproducible floating-point results Guy Davidson 2024-09-10 2024-09   SG6 Numerics,SG14 Low Latency,LEWG Library Evolution
P3379R0 Constrain std::expected equality operators Jonathan Wakely 2024-08-27 2024-09   LEWG Library Evolution
P3380R0 Extending support for class types as non-type template parameters Barry Revzin 2024-09-09 2024-09   EWG Evolution
P3381R0 Syntax for Reflection Barry Revzin 2024-09-16 2024-09   EWG Evolution
P3382R0 Coarse clocks and resolutions Antony Polukhin 2024-08-28 2024-09   LEWGI SG18: LEWG Incubator,LEWG Library Evolution,LWG Library
P3383R0 mdspan.at() Stephan Lachnit 2024-09-04 2024-09   LEWGI SG18: LEWG Incubator
P3384R0 __COUNTER__ Jeremy Rifkin 2024-09-05 2024-09   EWG Evolution
P3385R0 Attributes reflection Aurelien Cassagnes 2024-09-16 2024-09   SG7 Reflection
P3388R0 When Do You Know connect Doesn't Throw? Robert Leahy 2024-09-10 2024-09   LEWG Library Evolution
P3389R0 Of Operation States and Their Lifetimes (LEWG Presentation 2024-09-10) Robert Leahy 2024-09-10 2024-09   LEWG Library Evolution
P3390R0 Safe C++ Sean Baxter 2024-09-11 2024-09   SG23 Safety and Security
P3391R0 constexpr std::format Barry Revzin 2024-09-12 2024-09   LEWG Library Evolution
P3392R0 Do not promise support for function syntax of operators Corentin Jabot 2024-09-14 2024-09   LEWG Library Evolution
P3396R0 std::execution wording fixes Lewis Baker 2024-09-16 2024-09   LWG Library
P3397R0 Clarify requirements on extended floating point types Hans Boehm 2024-09-16 2024-09   SG6 Numerics
P3398R0 User specified type decay Bengt Gustafsson 2024-09-17 2024-09   EWGI SG17: EWG Incubator
P3401R0 Enrich Creation Functions for the Pointer-Semantics-Based Polymorphism Library - Proxy Tian Liao 2024-09-17 2024-09   LEWGI SG18: LEWG Incubator,LEWG Library Evolution
P3402R0 A Safety Profile Verifying Class Initialization Marc-André Laverdière 2024-09-17 2024-09   SG23 Safety and Security

Meeting C++ 2024: the online track is complete

The last part of the program for Meeting C++ 2024 is now ready: the online track.

The online track for Meeting C++ 2024 is complete!

by Jens Weller

From the article:

With this the program for Meeting C++ 2024 is now complete! The online track features 11 talks and will be prerecorded publically in October.

The talks of the online track are:

Temporarily Dropping a Lock: The Anti-lock Pattern -- Raymond Chen

RaymondChen_5in-150x150.jpgIn C++, it's common to use RAII types like std::lock_guard to manage synchronization primitives, ensuring a lock is acquired at object creation and released at destruction. However, a less common but useful pattern is the "anti-lock," which temporarily releases a lock and reacquires it later, useful in scenarios where you need to drop a lock while performing certain operations, like calling out to other components to avoid deadlocks.

Temporarily Dropping a Lock: The Anti-lock Pattern

by Raymond Chen

From the article:

There is a common pattern in C++ of using an RAII type to manage a synchronization primitive. There are different versions of this, but they all have the same basic pattern:

  • Creating the object from a synchronization object: Locks the synchronization object.
  • Destructing the object: Unlocks the synchronization object.

These types go by various names, like std::lock_guardstd::unique_lock, or std::coped_lock, and specific libraries may have versions for their own types, such as C++/WinRT’s winrt::slim_lock_guard and WIL’s wil::rwlock_release_exclusive_scope_exit (which you thankfully never actually write out; just use auto).

One thing that is missing from most standard libraries, however, is the anti-lock.

The idea of the anti-lock is that it counteracts an active lock.

CopperSpice: Template Design With Policy Classes

New video on the CopperSpice YouTube Channel:

Template Design With Policy Classes

by Barbara Geller and Ansel Sermersheim

About the video:

We have a new C++ video which discusses Policy Based Design and compares it to other styles of programming. Do you know which design pattern policy based programming solves? Have you considered the benefits of a design which provides a solution at compile time versus run time? Are you using policies and maybe you had no idea they had a name?

Please take a look and remember to subscribe.

Reflection-based JSON in C++ at Gigabytes per Second -- Daniel Lemire

portrait2018.jpgJSON is a widely-used format for data exchange, but in C++, handling JSON efficiently can be challenging. While current solutions like simdjson offer high-speed processing, upcoming features in C++26, such as powerful reflection, promise to simplify and accelerate the serialization and deserialization of JSON, making it both faster and more convenient for developers.

Reflection-based JSON in C++ at Gigabytes per Second

by Daniel Lemire

From the article:

JSON (JavaScript Object Notation) is a popular format for storing and transmitting data. It uses human-readable text to represent structured data in the form of attribute–value pairs and arrays. E.g., {"age":5, "name":"Daniel", toys:["wooden dog", "little car"]}. Ingesting and producing JSON documents can be a performance bottleneck. Thankfully, a few JSON parsers such as simdjson have shown that we can process JSON at high speeds, reaching gigabytes per second.

However, producing and ingesting JSON data can remain a chore in C++. The programmer often needs to address potential errors such as unexpected content.

Yet, often, the programmer only needs to map the content to and from a native C/C++ data structure.

User-Defined Formatting in std::format – Part 3 -- Spencer Collyer

logo.pngWe’ve seen formatting for simple classes and more complicated types. Spencer Collyer finishes his series by showing us how to apply specific formatting to existing classes.

User-Defined Formatting in std::format – Part 3

by Spencer Collyer

From the article:

In the previous articles in this series [Collyer24a], [Collyer24b] I showed how to write classes to format user-defined classes and container classes using the std::format library.

In this article I will show you how to create format wrappers, special purpose classes that allow you to apply specific formatting to objects of existing classes.

A note on the code listings: The code listings in this article have lines labelled with comments like // 1. Where these lines are referred to in the text of this article it will be as ‘line 1’ for instance, rather than ‘the line labelled // 1’.

Format wrappers

I’d now like to introduce a type of class which I call ‘format wrappers’. A format wrapper is a very simple class which wraps a value of another type. They exist purely so that we can define a formatter for the format wrapper. The idea is that the formatter will then output the wrapped value using a specific set of formatting rules. Hopefully this will become clearer when we discuss the Quoted format wrapper later.

A format wrapper is a very simple class, which normally consists of just a constructor taking an object of the wrapped type, and a public member variable holding a copy or reference to that value. They are intended to be used in the argument list of one of std::format’s formatting functions, purely as a way to select the correct formatter.

Adding trainings to Meeting C++ 2024

Meeting C++ now offers 4 trainings that align with Meeting C++ 2024, and will be held in the last week of November.

Adding C++ trainings to Meeting Cpp 2024

by Jens Weller

From the article:

Trainings listing

    C++ for C Developers - Migrating from C to C++ - a two day training by Slobodan Dmitrovic starting November 25th
    Program with GUTs - a half day training by Kevlin Henney on November 25th
    Generic programming in C++ with templates and auto - full day training by Nicolai Josuttis on November 28th
    Concepts, Ranges, and Views - The New Way of Programming in C++ - full day training by Nicolai Josuttis on November 29th

These 4 trainings focus on various important current aspects of C++. From the migration to C++ from C, which is also a great course if you migrate to Modern C++ from "C with classes" like code. Or a refresher on unit testing by Kevlin Henney himself. Nicolai Josuttis gives a two day training, which is also available as single days: on the first day generic programming with templates and auto is bringing you a referesher, while focusing on C++20 Concepts, Ranges and Views on the next day. I've made the decision that prices for trainings are now fixed, half/full day trainings are 499 € and two day trainings are 999 €, this already includes taxes and all fees from the ticketshop. Attending the trainings will let you learn great new ways to think about your code and it gives support Meeting C++!

CppCon 2024 High-Performance Numerical Integration in the Age of C++26 -- Vincent Reverdy

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

High-Performance Numerical Integration in the Age of C++26

Friday, September 20 13:30 - 14:30 MDT

by Vincent Reverdy

Summary of the talk:

Could we revisit numerical integrators in the light of C++26 and bring more genericity, performance, and expressivity to the domain? In this talk, we will explore how modern C++ techniques can add something new and relevant to one of the oldest and most basic task of scientific computing: the integration of systems of equations. We will examine, in particular, how most numerical integrators can be derived from a small set of first principles that can be easily mapped onto C++ concepts and composable algorithmic building blocks. One of the goal of the approach introduced in this presentation will be to achieve as much as possible with the simplest and smallest amount of code. C++23 and C++26 programming techniques, including reflection, will be leveraged to transfer some of the burden of implementation to the compiler while still ensuring maximum performance.

In practice, the talk will combine aspects of high-performance computing, numerical methods, and software architecture. We will start by summarizing recent discoveries made in applied mathematics on Runge-Kutta methods, linear multistep methods, and general linear methods to see how it can help design better abstractions that can be translated into C++ concepts. We will then examine how a few carefully crafted algorithmic building blocks can be combined to generate the whole diversity of numerical integrators from first principles. The automation of this approach using C++23 and C++26's reflection to make the compiler generate highly efficient code will be then discussed in great length. Next, we will dive into parallelization strategies, including distributed ones as well as heterogenous computing. Some perspectives will also be given on the possibility for the compiler to branch on the best integrator given the mathematical properties of a system of equations as well as ways to derive new integrators at compile-time.

To illustrate our approach, we will examine the behavior and performance of numerous integrators on several real-world problems including a supercomputing N-body code for cosmology that simulates the gravitational dynamics of large scale astrophysical structures in an expanding Universe. A great care will be taken to make all the code and examples as reproducible and standalone as possible so that most of the presented content can simply be copied and pasted to make it work everywhere. Finally, even if the talk will focus on the particular case of numerical integration, the methodology presented in this talk will be applicable everywhere in scientific computing and beyond to achieve better software architecture in technical contexts.


Vincent Reverdy is a Full Researcher in Computer Science and Astrophysics at French Center for Scientific Research (CRNS) and located at the Annecy Laboratory for Particle Physics (LAPP) in the French Alps. He also is a member of the French delegation to the C++ Standards Committee. After a PhD at the Paris Observatory in 2014 on the topic of numerical cosmology and general relativity for which he explored extensively advanced metaprogramming techniques, he joined the University of Illinois at Urbana-Champaign in the US. There, he led an interdisciplinary research group in both computer science and computational astrophysics, trying to bridge the gap between programming languages and computational sciences. In late 2019 he moved back to France to continue to work on software architecture aspects related to astrophysics, and joined CNRS in January 2022 to lead long-term research aiming at building bridges between theoretical computer science including type theory and category theory on one side, and computational sciences with a focus on numerical astrophysics on the other side. Finally, as a member of the C++ committee, he has been working extensively on low-level programming components, including bit manipulation, as well as mathematical abstractions.

CppCon 2024 Contracts for C++ -- Timur Doumler

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

Contracts for C++

Wednesday, September 18 14:00 - 15:00 MDT

by Timur Doumler

Summary of the talk:

Design by Contract is a very effective approach for writing safer, more correct programs. It has been successfully implemented in programming languages like Eiffel and Ada. Attempts to add a Contracts facility to C++ have a long and storied history spanning two decades. Since the last attempt to standardise Contracts (for the C++20 Standard) has failed, SG21 — the Contracts Study Group on the C++ Standard Committee – has been working on a new design, the so-called Contracts MVP, which is now essentially feature-complete and on track to make it into the upcoming C++26 Standard.

In this talk, we present the current design of the Contracts MVP targeting C++26. We discuss preconditions, postconditions, assertions, contract-violation handling and much more. We consider how the Contracts MVP provides a superior replacement for custom assertion macros and, when used correctly, can significantly improve the safety and correctness of your code.


Timur Doumler is the co-host of CppCast and an active member of the ISO C++ standard committee, where he is currently co-chair of SG21, the Contracts study group. Timur started his journey into C++ in computational astrophysics, where he was working on cosmological simulations. He then moved into the audio and music technology industry, where he has been working for over a decade and co-founded the music tech startup Cradle. In the past, Timur also worked for JetBrains, first as a developer on CLion's C++ parser and later as a Developer Advocate for C++ developer tools. Currently, Timur lives in Finland, where he is organising the monthly C++ Helsinki meetup. Timur is passionate about clean code, good tools, low latency, and the evolution of the C++ language.