0

Is my impression correct that if shared memory between threads is only read and never written, by any of the threads, mutex protection is unneccessary?

It's because wherever a thread is preempted, it can always take off again when rescheduled, and the memory contents would not have changed.

Iceman
  • 3,904
  • 7
  • 25
  • 37

2 Answers2

4

Yes, you're right, multiple threads reading the same variable do not introduce a race.

Alex Telishev
  • 2,254
  • 12
  • 14
3

Both C++11 and C11 define a data race to be access to a memory location by more than one execution context without ordering where at least one of the accesses is a write. If you have no writes, you have no races.

Kerrek SB
  • 447,451
  • 88
  • 851
  • 1,056