6

I am interested in multithreading. There are a lot of gotchas in the field, for example, there is no guarantee that pointer writes are atomic. I get this, but would like to know what are the most popular current configurations when this is actually the case? For example, on my Macbook Pro/gcc, pointer writes definitely seem to be atomic.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
  • 2
    Also if the pointer is stored in an unaligned memory location (such that its value may be stored in two different cache lines? –  Jan 20 '11 at 10:56
  • @jdv: That is not very likely to happen by accident. So I am not interested in cases like that. – Johan Kotlinski Jan 20 '11 at 11:10
  • Even if they're atomic, you'll have the problem of visibility - unless taken care of, that pointer could be stored in a register an never been written to memory - or the memory writes might be reordered and not happen when you think it does according to your code – nos Jan 20 '11 at 17:35

4 Answers4

3

This is mostly a problem for CPU architectures where the pointer width is larger than the width of the CPU architecture. For instance, on ATmega CPUs, an 8-bit architecture, the address space is 16-bit. If there aren't any specific instructions to load and store 16-bit addresses, at least two instructions are needed to load / store a pointer value.

Daniel Gehriger
  • 7,339
  • 2
  • 34
  • 55
2

See here.

Community
  • 1
  • 1
rkellerm
  • 5,362
  • 8
  • 58
  • 95
1

Nearly each architecture is impacted as Daniel said. Unless memory alignment is enforced each write potentially results into several operations but also this fails if the address bus is smaller than the data bus. So you will most likely need writing code using locking mechanisms. This is anyway a good idea as you probably want your code to be portable. For some very special architectures these locking functions would simply be empty.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
0

Pointers might not be atomic types on platforms that use a segmented address space, like MS-DOS or Win 3.x. But I'm not aware of any modern desktop/server platforms using this kind of architecture (at least at the platform's level).

However, even if a write is atomic from the point of view of the C compiler there might be other issues that come into play, even on modern desktop/server systems, especially when dealing with multicore/multiprocessor systems (caching, memory access reordering done at a lower level by the processor). 'Atomic' APIs provided by a platform deal with those issues using memory barriers (if required), so you still should probably use those APIs when trying to ensure that a memory access is atomic.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760