C11 features in concurrency
C11 Overview
C11 is an informal name for ISO/IEC 9899:2011, the current standard for the C programming language. It includes multi-threading support, which enables us higher-level programming types to deal with concurrency now. Then, let's see how to use the C11 features to design the lock-free data structures.
C11 Atomic types
C11 defines a new _Atomic type specifier. The atomic declaration syntax is like this:
_Atomic(type_name)
or _Atomic type_name
For example, we could define an atomic integer like this:
Notice that the type_name can not be an array type or a function type, and the first syntax also can not be an atomic type, or a qualified type. Refer to here for more information.
Please be sure to include the header "#include<stdatomic.h>" when you are using the atomic type in your program.
Also, we could define a user-defined struct atomic types, which enable the struct to be manipulated atomically.
In addition to the ability to define user-defined atomic types, the _Atomic
keyword makes it possible to use objects of all atomic types in nearly all the same expressions and contexts as their direct (non-atomic) counterparts.
Here is a simple example of using C11 atomic type:
In order to compile C11 program, we need GCC4.9 or later. We should specify the parameter to tell the gcc compiler that we would like to compile the program by using C11 standard. So, the gcc command is as follows:
If the gcc version is too old, you may run the following command inside any CSE department machine to add gcc 7.3.1 to your path:
One of the possible outputs is:
Atomic operations
C11 provides some functions for us to change the content of atomic types like a user-defined struct in an atomic way. Here I will introduce the CAS function, which we will use in lock-free data-struct designing.
Description: Atomically, compares the value pointed to by object for equality with that in expected, and if true, replaces the value pointed to by object with desired, and if false, updates the value in expected with the value pointed to by object. (Here, A means atomic type, while the C means the non-atomic type corresponding to A, for example, if A is _Atomic int , then, C is int)
The volatile is a type qualifier, like const, and is a property of the type. It indicates that a value may change between different accesses (eg: shared variable in multi-threading program), even if it does not appear to be modified. With the volatile keyword, the compiler knows a variable needs to be reread from memory at each use, and it prevents the compiler from performing optimization on code. For more information, please refer to this link:
http://www.geeksforgeeks.org/understanding-volatile-qualifier-c-set-1-introduction/
Returns: the result of the comparison, true or false.
Here, we will use the function to implement a simple lock.
one of the possible outputs is:
For more information, please refer to http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Last updated