简体   繁体   中英

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.

I don't have the official standard so the following is from 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 ]


Here is the Herb Sutter video where I first learned about the meaning of const in the C++11 standard. (see around 7:00 to 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).

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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