简体   繁体   English

从多个线程读取访问

[英]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; const变量可以从多个线程中读取: 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 ? 当我确定没有写访问权限时,是否可以从多个线程安全地读取没有const限定符的变量? 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;

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

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