This is a question about read performance in multi-threaded code.
So here is the situation. I have a large amount of data that a couple thousand entities need to know in order to react. This data changes each frame so lets call it frame data
.
Each of these entities have a function that needs to be run each frame lets call it run()
. Function run() requires frequently reading (but never writing) to the frame data
. Lets also assume that frame data
is in the heap and thus not cloned when new threads are made.
These entities may all be run()
sequentially on a single thread or if the platform running this code would benefit the entities might be batched into several pthreads or each run()
on their own pthread.
So essentially each frame frame data
gets updated then every entity gets run()
in an arbitrary order on an arbitrary thread.
I realize that reads will take the same amount of time no-matter what but what I am worried about is threads getting blocked while waiting for another thread to finish reading the simulation data
. Is this a valid concern at all?
Disregarding the cost of copying would it be a good idea for me to make a copy of the simulation data
for each thread I create or will the CPU be fine with multiple threads reading this resource? How would this change if simulation data
was on the stack.