Contrary to a commonly held belief in concurrency models, Stroustrup claims that modern hardware is incapable of storing or loading anything smaller than a word. However, modern x86 processors allow for the storage and manipulation of individual bytes. This apparent contradiction prompts an examination of Stroustrup's statement and its implications.
Stroustrup suggests that without a clearly defined memory model, concurrent threads could interfere with each other's byte manipulation operations, potentially leading to incorrect results. This argument assumes hardware that lacks explicit support for atomic byte storage and can lead to inadvertent modification of neighboring bytes.
A more nuanced interpretation of Stroustrup's statement is that it might apply to hardware with a specific internal implementation. CPUs may use read-modify-write cycles to manipulate data within cache lines, even for byte-sized operations. However, these internal operations are transparent to the programmer and do not affect the external visibility of the operation. Byte stores remain atomic and do not corrupt adjacent bytes in the cache line.
While a hypothetical ISA without atomic byte stores could indeed exist, all modern ISAs, including x86, provide byte-sized load and store instructions. These instructions are architecturally defined to preserve the integrity of neighboring bytes, ensuring correct program execution. Moreover, on modern CPUs with L1D cache, the handling of bytes and words is largely similar, as both require cache line fetches and write-backs.
Stroustrup's statement, while potentially applicable to hypothetical or obsolete hardware designs, does not accurately reflect the behavior of modern x86 processors. These processors provide efficient atomic byte storage, ensuring the correctness of software that relies on such operations. Thus, in the context of programming for modern hardware, there is no need to fear that byte manipulation can lead to memory corruption.
The above is the detailed content of Can Modern x86 Hardware Really Not Store a Single Byte?. For more information, please follow other related articles on the PHP Chinese website!