Reputation: 59
What happens when multiple threads on a multi-core or multu-CPU machine try to access the same region in heap memory (read only - no mutating) at the same time? For example, trying to invoke a static method (the method does not mutate anything). Could the act of just trying to invoke the static method possibly create a race or deadlock condition?
EDIT: Can a read-only memory access by multiple threads at the same time cause a race condition (or any other issues)?
Upvotes: 0
Views: 336
Reputation: 182753
Every platform that supports multiple cores that you are likely to use for the foreseeable future will support some version of MESI that keeps the core's views of memory coherent. Memory that is read on one core shortly after being read on another core will wind up being shared by all the cores that read it until either a core writes to it (at which point it will be exclusive on the core that wrote to it and invalid on the others) or it gets pushed out of cache.
You can't cause a race condition by reading memory that is not being modified. This is one of the reasons you can't have a race condition on the code itself unless the code is being modified.
Upvotes: 1
Reputation: 3053
No, multi-threaded readings are fine.
Race conditions possible only if any thread tries to write. And even in this case it can work fine - it depends on lot of other things (cpu arch, write type etc)
Upvotes: 1