Use volatile variable in multi-thread programming

About volatile variable, I think 4.3.4.2 A Volatile Solution from Is Parallel Programming Hard, And, If So, What Can You Do About It? is a good reference. The following is excerpted from it:

To summarize, the volatile keyword can prevent load tearing and store tearing in cases where the loads and stores are machine-sized and properly aligned. It can also prevent load fusing, store fusing, invented loads, and invented stores. However, although it does prevent the compiler from reordering volatile accesses with each other, it does nothing to prevent the CPU from reordering these accesses. Furthermore, it does nothing to prevent either compiler or CPU from reordering non-volatile accesses with each other or with volatile accesses. Preventing these types of reordering requires the techniques described in the next section.

So if your variables satisfies following conditions:

(1) The load and store of variable is machine-sized and properly aligned;
(2) Code reordering doesn’t affect the logic of program.

You can use volatile.

BTW, there is a code sample for reference.

Use alignof in gcc

alignof is defined in stdalign.h for gcc:

$ cat /usr/lib/gcc/x86_64-unknown-linux-gnu/9.1.0/include/stdalign.h
......
#ifndef _STDALIGN_H
#define _STDALIGN_H

#ifndef __cplusplus

#define alignas _Alignas
#define alignof _Alignof

#define __alignas_is_defined 1
#define __alignof_is_defined 1

#endif

#endif  /* stdalign.h */

To use alignofstdalign.h must be included, otherwise following errors will be reported:

......
warning: implicit declaration of function 'alignof' [-Wimplicit-function-declaration]
    3 |  return alignof(int);
      |         ^~~~~~~
 error: expected expression before

It is no need to include any header files to use _Alignof and __alignof__.

Empty struct in C

Empty struct in C is undefined behaviour (refer C17 spec, section 6.7.2.1):

If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined.

On my Linux, I use both gcc (v9.1.0) and clang (v8.0.0) to compile following code:

$ cat test.c
#include <stdio.h>

typedef struct A {} A;

int main()
{
    printf("%zu\n", sizeof(A));
}

No warnings, and both ouput:
0

Reference:
C empty struct — what does this mean/do?.

As a system programming language, C still deserves learning today

C is a system programming language which is old but with “bad” fame: undefined behavior, notorious memory related bugs, etc. Especially with Go and Rust having gone viral right now, C seems already forgotten by people. Nevertheless, IMHO, C is still a thing which is worth for you spending some time on it.

Whether you are are a C novice or a C veteran,I highly recommend you read Modern C if you haven’t read it before. Then you will find C also evolves stealthily and is not as primitive as you think. E.g., C11 has defined standard thread APIs like C++ has done, which makes C more like a “modern” language, not an outdated thing. You may get a new perspective of C from this book.

Regardless if you are a systems language programmer, DevOps, performance engineer or wear other hats, the more you know about the Operating System, the more you can do your job better. Take all prevailing Unix-like Operating Systems as an example, from kernel to command line tools, they are almost implemented in C. To study related source code can make you delve into Operating System internal deeper. E.g., I knew there is a taskset command which can bind specified process of a dedicated CPU, but I wanted to know the magic behind it, so I went through its code. Then I learned 2 things:
a) There is a “/proc/%pid/task” folder which records thread information of process;
b) taskset actually calls sched_setaffinity and sched_getaffinity APIs to attain its goal.

Last but not least, because C is so “low-level”, you can leverage it to write highly performant code to squeeze out CPU when performance is critical in some scenarios.

Although I have listed many reasons which I think you should learn C, you may have sufficient excuses to refuse to dabble into C too. It doesn’t matter. But if you want to give C a shot, firstly you should not be scared of it. There are many ways of writing wrong C code, but you only need to make sure what you write is correct and in defined behavior, that’s all about programming.