Search code examples
c++cmemoryconcurrencymemory-model

Memory Models and Concurrency


I wanted to know about C/C++ memory model but I found in all of the articles, blogs, youtube videos that "Memory models are only needed for concurrency"..

Can anyone please clarify to me why is this the case?

I understand that a memory model is an abstraction that allows the programmer to reason about the underlying memory system using a programming language (correct?) so generally speaking (independent of the programming language) don't we still need memory models for single threaded programs?

Thanks!


Solution

  • I understand that a memory model is an abstraction that allows the programmer to reason about the underlying memory system using a programming language (correct?)

    A memory consistency model is a set of rules that allows the programmer to reason about the possible states of the program at any point in time during its execution from the perspective of each individual agent (core) that is accessing the state, if certain conditions were satisfied (such as no data races).

    I wanted to know about C/C++ memory model but I found in all of the articles, blogs, youtube videos that "Memory models are only needed for concurrency"..

    Can anyone please clarify to me why is this the case?

    There are some rules that can be considered as part of the memory model that would also apply in the context of single threaded programs. In particular:

    • A read that follows a write to the same location will read the value written, not an older value.
    • A write that follows another write to the same location will overwrite the previously written value.
    • A write that follows a read to the same location will only modify the location after the read completes.

    However, these rules are considered to be so fundamental and intuitive that it is not worth stating them in every memory model. In case of VLIW architectures, the compiler automatically follows these rules. In case of traditional architectures, both the compiler (according to the order of the statements in the source code) and the processor follow these rules (according to the order of the instruction stream). So basically it's redundant to state them. Other than that, there are no other useful rules for single-threaded programs.