Access a static constant variable from multiple threads in C

I have experience with multi-threaded Linux programming (C / C ++ and POSIX threads), however the most obvious cases are sometimes very complicated.

I have several constant constant variables (global and local functions) in my code, can I access them simultaneously from several threads without using mutexes? Since I do not change them, this should be good, but it is always better to ask.

I need to do speed optimization, so even fast operations, such as locking / unlocking mutexes, are quite expensive for me, especially because my application will access these form variables with long cycles.

+3
source share
3 answers

If you initialize them only on one thread, and then never change them, it should be normal to read them simultaneously from several threads without mutexes, etc.

+13
source

If you are only reading and not changing, you do not need any locks

+4
source

I don’t know of other architectures, but Intel guarantees that all reads are atomic, however, if you want to add some, use something like value = atomic_add(&variable,0);this, it will force all records to then add 0 to the value, then return the old value, which does not change

0
source

Source: https://habr.com/ru/post/1745758/


All Articles