1

During a school project for concurrent programming, we included <stdatomic.h> in a project and modified the appropriate data types of variable declarations to allow atomic operations on some global variables.

When I began to look into the stdatomic header, I saw that a bunch of data types and operations were defined to allow the desired behavior.

What I noticed when I compiled and did an object dump was that memory fences were now added. I also make the assumption that for operations like my_atomic_variable += 5; are replaced with some of the macros/functions declared in the stdatomic header.

What I would like to know is how simply by changing the data types in the source code, memory fences and such are added. I imagine at some point functions like __atomic_signal_fence are subbed into the source code where read/writes take place, but I have no idea how this is actually done. Unfortunately reading the header files hasn't helped a lot. I get the impression there is some concept of how compilers work that I am not familiar with or don't understand.

UPDATE

In response to the comments, let me clarify a little bit. The way I understand C is that there are certain basic data types, like an int. I imagine stdatomic implements atomic_int in terms of int and some extra code. It seems to me that when some atomic int is assigned like my_atomic_int = 1, somehow this is changed into a more complex set of instructions than would normally be required to assign a regular integer.

What I don't understand is what could be present inside the stdatomic code that would allow the use of the = operator since C does not support operator overloading. What code actually does this? Does gcc know how to translate the atomic instructions because it has atomic data types built into it? If not, how could I write code similar to that of stdatomic where gcc would know how to handle the = operator for my custom data type?

tnibbles
  • 93
  • 5

0 Answers0