How to understand atomatics in a standalone implementation of C or C ++?

C11 and C ++ 11 define atoms in terms of threads of execution. Whereas in a hosted environment it is clear what a stream is, this is a rather vague term in the implementation of an autonomous language.

  • How to formally understand the atoms specified in C11 and C ++ 11 in a standalone implementation, where all the threads must be implemented inside the program? For example: is the ISR a separate thread of execution?
  • Why did standard committees define atomics in terms of flows, and not just in the field of code ordering?
  • Are there any built-in compilers that already support C11 / C ++ 11 atoms besides gcc?
+5
source share
2 answers

My somewhat mechanistic (and somewhat manual) approach to this issue is that atomistic guarantees three things: reading and writing will not be broken by the context switch (so that you only see the values ​​that were actually stored in the variable); caches are cleared (therefore you do not see outdated values); and the compiler cannot move instructions as part of an atomic operation (so that operations that occur logically before atomic access actually occurs before that access). Please note that I tried to avoid any concept of "stream" here, although it is a bit more complicated.

If you are writing your own streaming engine, these properties are obviously important. They are orthogonal to the details of the slicing mechanism used.

For signal handlers, they give you a place to wait when you need to learn the values ​​from the code executed in the signal handler, and when the signal handler needs to change the values ​​that the rest of the program cares about.

I’m not sure if the ISR standard is formal (I’m sure that this is not so), but from this mechanistic point of view, ISR is no different from a signal that does not come from a raise call. This is just an asynchronous function call, and it takes up the stack space that it receives from the thread that is interrupted. This is definitely not a stream; it is a parasite on an existing thread. Therefore, for ISR, I would go for a guarantee of signals, not a guarantee for flows.

+3
source

Nuclear scientists must deal with the conditions of the race, so without flows, they almost do not make sense. The only other context in which races can occur is signal handlers, and there the C standard offers the concept of blocking.

Thus, there are no differences between standalone and hosted environments. Threads and atomistics are optional features, and if an autonomous environment supports them, it must conform to the specifications of both. If it supports only atomics, it can provide atomic types without blocking for signal handlers, but other atomic types are then useless.

+3
source

Source: https://habr.com/ru/post/1261406/


All Articles