Is function call an effective memory barrier for modern platforms?

后端 未结 4 1254
盖世英雄少女心
盖世英雄少女心 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:50

    In practice, he's correct and a memory barrier is implied in this specific case.

    But the point is that if its presence is "debatable", the code is already too complex and unclear.

    Really guys, use a mutex or other proper constructs. It's the only safe way to deal with threads and to write maintainable code.

    And maybe you'll see other errors, like that the code is unpredictable if send() is called more than one time.

提交回复
热议问题