New keyword in c
A new keyword in C!
As of C11 (the 2011 standard), the new keyword in C is _Atomic
. It's used to declare atomic variables, which are variables that can be accessed and modified by multiple threads without the need for locks or other synchronization mechanisms.
Here's an example:
_Atomic int x = 0;
This declares an atomic integer variable x
that can be accessed and modified by multiple threads.
The _Atomic
keyword is used to specify that the variable is atomic, meaning that its value can be read and written in a single, uninterruptible operation. This is useful in multithreaded programs where multiple threads may be accessing and modifying shared variables.
Note that _Atomic
is not a type, but rather a keyword that modifies the type of the variable. You can use it with any basic type (e.g., int
, float
, char
, etc.) to create an atomic variable.
Additionally, C11 also introduced the _Atomic
keyword for atomic operations, which allows you to perform atomic operations on atomic variables. For example:
_Atomic int x = 0;
x = _Atomic_fetch_add(&x, 1);
This performs an atomic fetch-and-add operation on the atomic variable x
, adding 1 to its current value.
The _Atomic
keyword is a powerful tool for building thread-safe programs in C, and it's an important addition to the language.