如何确保C 多线程程序从主存读取最新值?
How to make sure C multithreading program read the latest value from main memory?
假设线程 1(在 Core 0 中执行)更新了一个全局变量,并且更新后的全局值缓存在 Core 0 的 L1 缓存中(不刷新到主内存)。然后线程 2 开始在 Core 3 中执行,它试图读取全局变量,并从主内存中读取它(因为它没有缓存值),所以线程 2 正在读取一个过时的值。
我知道在 C 中你可以使用 volatile
强制编译器不读取 CPU 寄存器的值,这意味着 volatile 变量将从缓存或主内存中获取它的值。在我上面的场景中,即使我用 volatile
声明全局变量,最新的值仍然会缓存在 L1 缓存中,主内存仍然有一个旧值将被线程 2 读取。那么我们如何才能解决这个问题?
或者也许我的理解是错误的,使用 volatile
会使变量直接在主内存中更新,所以每次你尝试 read/write 一个 volatile 变量时,你 read/write 它 from/to 主要直接内存?
在某种程度上,人们注意到你问题的前提有缺陷是一个合理的答案。一般来说,这种情况很少发生,而且通常与竞争条件无法区分。
但是是的,它可能会发生。参见示例 memory barriers,这是一个很好的例子,说明了这种情况(尽管是由于 OOO 执行等)是如何发生的。
话虽这么说,但您要确保您注意到的 特定 事件不会发生的方法称为“cache flush". This can be genuinely important on ARM/ARM64 processors where separate data and instruction caches exist, but it's also a good habit to get into for data that is passed between threads this way. You can also check out the __builtin___clear_cache c compiler builtin,它执行类似的操作任务。希望其中一项可以帮助您找到问题的根源。
但是,很可能您 运行 没有陷入缓存问题,并且更有可能出现竞争条件。如果内存 barriers/cache 刷新不能解决您的问题,请非常仔细地检查您的代码是否存在异常。
How to make sure C multithreading program read the latest value from main memory?
您可能想使用一些线程库,例如 POSIX 线程。阅读一些 Pthread tutorial, see pthreads(7) and use pthread_create(3), pthread_mutex_init, pthread_mutex_lock, pthread condition variables,等等等等
另请阅读 GNU libc and of your C compiler (e.g. GCC, to be used as gcc -Wall -Wextra -g
) and of your debugger (e.g. GDB) 的文档。
准备与Heisenbugs作战。
一般来说,您不能静态地证明您的 C 程序没有竞争条件。请参阅 Rice's theorem. You could use tools like Frama-C or the Clang static analyzer, or write your own GCC plugin, or improve or extend Bismon described in this 报告草稿。
您可能会对 CompCert 感兴趣。
您无法确定您的程序从内存中读取了“最新”值。
(除非你添加一些汇编代码)
假设线程 1(在 Core 0 中执行)更新了一个全局变量,并且更新后的全局值缓存在 Core 0 的 L1 缓存中(不刷新到主内存)。然后线程 2 开始在 Core 3 中执行,它试图读取全局变量,并从主内存中读取它(因为它没有缓存值),所以线程 2 正在读取一个过时的值。
我知道在 C 中你可以使用 volatile
强制编译器不读取 CPU 寄存器的值,这意味着 volatile 变量将从缓存或主内存中获取它的值。在我上面的场景中,即使我用 volatile
声明全局变量,最新的值仍然会缓存在 L1 缓存中,主内存仍然有一个旧值将被线程 2 读取。那么我们如何才能解决这个问题?
或者也许我的理解是错误的,使用 volatile
会使变量直接在主内存中更新,所以每次你尝试 read/write 一个 volatile 变量时,你 read/write 它 from/to 主要直接内存?
在某种程度上,人们注意到你问题的前提有缺陷是一个合理的答案。一般来说,这种情况很少发生,而且通常与竞争条件无法区分。
但是是的,它可能会发生。参见示例 memory barriers,这是一个很好的例子,说明了这种情况(尽管是由于 OOO 执行等)是如何发生的。
话虽这么说,但您要确保您注意到的 特定 事件不会发生的方法称为“cache flush". This can be genuinely important on ARM/ARM64 processors where separate data and instruction caches exist, but it's also a good habit to get into for data that is passed between threads this way. You can also check out the __builtin___clear_cache c compiler builtin,它执行类似的操作任务。希望其中一项可以帮助您找到问题的根源。
但是,很可能您 运行 没有陷入缓存问题,并且更有可能出现竞争条件。如果内存 barriers/cache 刷新不能解决您的问题,请非常仔细地检查您的代码是否存在异常。
How to make sure C multithreading program read the latest value from main memory?
您可能想使用一些线程库,例如 POSIX 线程。阅读一些 Pthread tutorial, see pthreads(7) and use pthread_create(3), pthread_mutex_init, pthread_mutex_lock, pthread condition variables,等等等等
另请阅读 GNU libc and of your C compiler (e.g. GCC, to be used as gcc -Wall -Wextra -g
) and of your debugger (e.g. GDB) 的文档。
准备与Heisenbugs作战。
一般来说,您不能静态地证明您的 C 程序没有竞争条件。请参阅 Rice's theorem. You could use tools like Frama-C or the Clang static analyzer, or write your own GCC plugin, or improve or extend Bismon described in this 报告草稿。
您可能会对 CompCert 感兴趣。
您无法确定您的程序从内存中读取了“最新”值。
(除非你添加一些汇编代码)