开发者

What are the most common configurations where pointer writes are not atomic?

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, p开发者_开发技巧ointer writes definitely seem to be atomic.


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.


See here.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜