Can I force cache coherency on a multicore x86 CPU?

后端 未结 9 1107
一个人的身影
一个人的身影 2020-11-29 18:43

The other week, I wrote a little thread class and a one-way message pipe to allow communication between threads (two pipes per thread, obviously, for bidirectional communica

9条回答
  •  独厮守ぢ
    2020-11-29 19:04

    There are several sub-questions in your question so I'll answer them to the best of my knowledge.

    1. There currently is no portable way of implementing lock-free interactions in C++. The C++0x proposal solves this by introducing the atomics library.
    2. Volatile is not guaranteed to provide atomicity on a multicore and its implementation is vendor-specific.
    3. On the x86, you don't need to do anything special, except declare shared variables as volatile to prevent some compiler optimizations that may break multithreaded code. Volatile tells the compiler not to cache values.
    4. There are some algorithms (Dekker, for instance) that won't work even on an x86 with volatile variables.
    5. Unless you know for sure that passing access to data between threads is a major performance bottleneck in your program, stay away from lock-free solutions. Use passing data by value or locks.

提交回复
热议问题