C++ Standard Library C++ STL Library

C++ <csignal> - sig_atomic_t Type



The C++ <csignal> sig_atomic_t type is an integer type which can be accessed as an atomic entity even in the presence of asynchronous interrupts made by signals.

In the <csignal> header file, it is defined as follows:

typedef /* unspecified */ sig_atomic_t;            

Example:

The example below shows the usage of sig_atomic_t type.

#include <csignal>
#include <cstdio>
 
volatile sig_atomic_t gSignalStatus = 0;
 
void signal_handler(int signal) {
  gSignalStatus = signal;
}
 
int main(void) {
  //installing a signal handler
  signal(SIGINT, signal_handler);

  printf("SignalValue: %d\n", gSignalStatus);
  printf("Sending signal %d\n", SIGINT);
  raise(SIGINT);
  printf("SignalValue: %d\n", gSignalStatus);

  return 0;
}

The output of the above code will be:

SignalValue: 0
Sending signal 2
SignalValue: 2

❮ C++ <csignal> Library