1

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?

0

3 Answers 3

1

For reading the variable: no

For writing to and reading the variable: yes

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.