C++C++11C++14C++17C++20Learn C++

How To Allow Atomics Use In C++ Signal Handlers

How To Allow Atomics Use In C++ Signal Handlers

C++11 allows the use of atomics in signal handlers, and with the advent of C++ 17 the signal handler feature was again improved. The std::atomic_flag is an atomic boolean type that is guaranteed to be lock-free and can be used in signal handlers. Moreover, the <csignal> header in C++ has an integer type std::sig_atomic_t that can be accessed as an atomic entity even in the presence of asynchronous interrupts made by signals. In this post, we explain how to use atomic_flag in C++.

How to allow atomics use in C++ signal handlers?

The <csignal> header in C++ (or signal.h in C) has an an integer type std::sig_atomic_t that can be accessed as an atomic entity even in the presence of asynchronous interrupts made by signals. In this post, we explain how to allow atomics use in C++ signal handlers.

Here is how we can use it:

Is there a full example of how to use atomics in C++ signal handlers?

Here is an example that has a signal handler and uses sig_atomic_t flag from <csignal>

The output will be as follows:

For more information on this feature, see Allow atomics use in signal handlers Proposal document.

If you want to know more about signals and error handlers, I recommend you “CERT C Coding Standard, Second Edition, The: 98 Rules for Developing Safe, Reliable, and Secure Systems”.
https://www.informit.com/store/cert-c-coding-standard-second-edition-98-rules-for-9780321984043

How To Allow Atomics Use In C++ Signal Handlers C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?