What are atomic types in the C language? What are atomic types in the C language? c c

What are atomic types in the C language?


Atomic types are those for which reading and writing are guaranteed to happen in a single instruction. More explanation from gnu.org:

24.4.7.2 Atomic Types

To avoid uncertainty about interrupting access to a variable, you can use a particular data type for which access is always atomic: sig_atomic_t. Reading and writing this data type is guaranteed to happen in a single instruction, so there’s no way for a handler to run “in the middle” of an access.

The type sig_atomic_t is always an integer data type, but which one it is, and how many bits it contains, may vary from machine to machine.

Data Type: sig_atomic_t
This is an integer data type. Objects of this type are always accessed atomically.

In practice, you can assume that int is atomic. You can also assume that pointer types are atomic; that is very convenient. Both of these assumptions are true on all of the machines that the GNU C Library supports and on all POSIX systems we know of.

For even more detail and some C11-specific stuff, check out CppReference.com (no affiliation).


Heres an anwser for IoS machines. @stackoverflow

Ed Cottrells answer was good but if you want to know what the difference between floats and doubles ints and longs. Those types use different byte sizes double floats store raddix data for decimals. And signed stores negative numbers backwards using two's complement so try casting signed to unsigned types. Look up maxsize int long etc.

To really use atomic types you need to know why they were created. The need for read write Assembly low level coded accesses relates to Mutex lock semophores and Multi-Threading on multi-core machines.

The idea was that two processes shouldn't be able to modify the same data at the same time. But I have heard that lock locks happen when two processes try to lock a memory location or file. So in linux theres NMI watchdog that was hacked to scan for these locks. On my single core machine I have to disable this with sudo sysctl kernel.nmi_watchdog=0.

Try wikipedia for more info