简体   繁体   English

线程安全:从单个const源读取多个线程

[英]Thread Safety: Multiple threads reading from a single const source

What should I be concerned about as far as thread safety and undefined behavior goes in a situation where multiple threads are reading from a single source that is constant? 如果线程安全和未定义的行为在多个线程从单个源读取的情况下进行,那么我应该关注什么呢?

I am working on a signal processing model that allows for parallel execution of independent processes, these processes may share an input buffer, but the process that fills the input buffer will always be complete before the next stage of possibly parallel processes will execute. 我正在研究一种允许并行执行独立进程的信号处理模型,这些进程可以共享一个输入缓冲区,但填充输入缓冲区的进程将始终在可能并行进程的下一阶段执行之前完成。

Do I need to worry about thread safety issues in this situation? 在这种情况下,我是否需要担心线程安全问题? and what could i do about it? 我能做些什么呢?

I would like to note that a lock free solution would be best if possible 我想指出,如果可能的话,无锁解决方案将是最好的

but the process that fills the input buffer will always be complete before the next stage of possibly parallel processes will execute 但是,在执行可能并行进程的下一阶段之前,填充输入缓冲区的进程将始终完成

If this is guaranteed then there is not a problem having multiple reads from different threads for const objects. 如果这是有保证的,那么从const对象的不同线程中读取多个就没有问题。

I don't have the official standard so the following is from n4296 : 我没有官方标准所以以下内容来自n4296

17.6.5.9 Data race avoidance 17.6.5.9避免数据竞争

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. 3 C ++标准库函数不应直接或间接修改除当前线程以外的线程可访问的对象(1.10),除非通过函数的非const参数直接或间接访问对象,包括此参数。

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. 4 [注意:这意味着,例如,实现不能在没有同步的情况下将静态对象用于内部目的,因为即使在未在线程之间显式共享对象的程序中,它也可能导致数据竞争。 —end note ] - 尾注]


Here is the Herb Sutter video where I first learned about the meaning of const in the C++11 standard. 这是Herb Sutter视频 ,我第一次了解了C ++ 11标准中const的含义。 (see around 7:00 to 10:30) (见7点到10点30分左右)

No, you are OK. 不,你没事。 Multiple reads from the same constant source are OK and do not pose any risks in all threading models I know of (namely, Posix and Windows). 来自同一个常量源的多次读取是可以的,并且不会对我所知的所有线程模型(即Posix和Windows)造成任何风险。

However, 然而,

but the process that fills the input buffer will always be complete 但填充输入缓冲区的过程将始终完成

What are the guarantees here? 这里的保证是什么? How do you really know this is the case? 你怎么知道这是这样的? Do you have a synchronization? 你有同步吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM