Is function call an effective memory barrier for modern platforms?

后端 未结 4 1266
盖世英雄少女心
盖世英雄少女心 2020-12-04 11:12

In a codebase I reviewed, I found the following idiom.

void notify(struct actor_t act) {
    write(act.pipe, \"M\", 1);
}
// thread A sending data to thread          


        
4条回答
  •  广开言路
    2020-12-04 11:32

    Memory barriers aren't just to prevent instruction reordering. Even if instructions aren't reordered it can still cause problems with cache coherence. As for the reordering - it depends on your compiler and settings. ICC is particularly agressive with reordering. MSVC w/ whole program optimization can be, too.

    If your shared data variable is declared as volatile, even though it's not in the spec most compilers will generate a memory variable around reads and writes from the variable and prevent reordering. This is not the correct way of using volatile, nor what it was meant for.

    (If I had any votes left, I'd +1 your question for the narration.)

提交回复
热议问题