tags:

views:

147

answers:

3

I have some experience with multithread programming under Linux (C/C++ & POSIX threads), however most obvious cases are sometimes very complicated.

I have several static constant variables (global and function local) in my code, can I access them simultaneously from multiple threads without using mutexes? Because I don't modify them it should be ok, but it's always better to ask.

I have to do heavy speed optimization, so even as fast operations as mutex lock/unlock are quite expensive for me, especially because my application is going to access these variables form long loops.

+2  A: 

If you're only reading and not modifying you shouldn't need any locks

Andreas Brinck
+11  A: 

If you initialize them on just one thread and then never modify them, it should be ok to read them concurrently from multiple threads without mutexes etc.

Moron
+1, In case it is not obvious enough from the answer: the first call to a function with a constant static variable is not thread safe.
David Rodríguez - dribeas
A: 

I don't know about other architectures, but intel guarantees that all reads are atomic, however, if you do feel like adding some, use something like value = atomic_add(&variable,0);, this will force all writes then add 0 to the value then return the old value, which doesn't get changed

Necrolis
Good to know, that all read's are atomic, can you give me source of this information?
Goofy