gpt4 book ai didi

c++ - 线程安全 : Multiple threads reading from a single const source

转载 作者:行者123 更新时间:2023-11-30 02:31:38 26 4
gpt4 key购买 nike

就线程安全和未定义行为而言,在多个线程从一个不变的单一源读取的情况下,我应该关注什么?

我正在研究一个允许独立进程并行执行的信号处理模型,这些进程可能共享一个输入缓冲区,但是填充输入缓冲区的进程总是会在下一阶段可能的并行进程执行之前完成.

在这种情况下我需要担心线程安全问题吗?我能做些什么呢?

我想指出,如果可能的话,无锁解决方案是最好的

最佳答案

but the process that fills the input buffer will always be complete before the next stage of possibly parallel processes will execute

如果能保证这一点,那么从不同线程多次读取 const 对象就不会有问题。

我没有官方标准所以以下来自n4296 :

17.6.5.9 Data race avoidance

3 A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this.

4 [ Note: This means, for example, that implementations can’t use a static object for internal purposes without synchronization because it could cause a data race even in programs that do not explicitly share objects between threads. —end note ]


这是 Herb Sutter video我第一次了解到 C++11 标准中 const 的含义的地方。 (见7:00到10:30左右)

关于c++ - 线程安全 : Multiple threads reading from a single const source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37463861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com