Reputation: 13369
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;
Upvotes: 0
Views: 132
Reputation: 737
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;
Upvotes: 1