简体   繁体   中英

read access from multiple threads

Read access (without using mutexes or atomics) from multiple threads is safe when there is no write access at the same time. Const variables can be read from multiple threads: const int x = 10; Can I also safe read a variable without const qualifier from multiple threads when I'm sure that there is not write access ? I know that it is not a good practise but I wonder if it is safe. What about pointers ? When I need using a pointer to read-only access from multiple threads it should be declared this way, right ? :

const int * const p = &x;

Of course you can read a non-const variable from multiple threads as long as you are sure there is no write operation is ongoing.

const int * const p = &x;

The above statement means you are preventing both the value and pointer from being modified. If you only want to protect the value itself, you can use

const int * p = &x;

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