13

I remember I came across certain types in the C language called atomic types, but we have never studied them.

So, how do they differ from regular types like int,float,double,long etc., and what are their uses?

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52

2 Answers2

16

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).

elixenide
  • 42,388
  • 14
  • 70
  • 93
  • 1
    What happens on 8-bit machines with 16-bit `int`? For instance, you can force `avr-gcc` to use 8-bit `int` (-mint8), but by default they are 16 bits, occupying two adjacent bytes, and non-atomic. – iter Oct 26 '18 at 18:21
  • 1
    @iter That's really a different question (or set of questions) than the one I was answering. If you'd like to ask it, please use the "[Ask Question](https://stackoverflow.com/questions/ask)" button. – elixenide Oct 26 '18 at 18:47
  • It really is off-topic. I was just wondering about the last paragraph in your quote. – iter Oct 26 '18 at 21:36
  • `sig_atomic_t` is not atomic type. For atomic types, C11 `_Atomic` can be used. Before that, you should just create a critical section with `pthread_mutex_t`, to protect the variable. See [this](https://stackoverflow.com/questions/24931456/how-does-sig-atomic-t-actually-work) for details – Sam Protsenko Jun 21 '19 at 14:59
  • you can't assume that int is atomic. https://github.com/alexeyneu/json-demo/blob/4dba1fd7463723311cb99b35d34c97d2740d42d3/decline/f.cpp#L67 – Алексей Неудачин Oct 28 '20 at 18:30
0

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