Reputation: 11
What if all threads read a global variable which was assigned a value by the main() prior to the creation of the threads. Do we need any Mutex for synchronization?
Upvotes: 1
Views: 67
Reputation: 464
If any of the threads wants to change the value of your global variable then yes, you need a new mutex. Otherwise no synchronization is needed.
Upvotes: 0
Reputation: 477040
No.
A data race happens when multiple threads access a memory location (through a non-atomic value) and at least one of the accesses is a write and the operations are not ordered.
Since thread creation is a synchronization point, all the accesses after thread creation are ordered after the initial write access, and the later accesses are only reads. So there is no race.
Upvotes: 1
Reputation: 461
For reading the variable: no
For writing to and reading the variable: yes
Upvotes: 1