Reputation: 30615
Wondered if I could get your thoughts on what I should do in this scenario.
Assume I have between 4 to 8 threads and I have a vector of values which will never be written to, only read by the threads.
I have the option of creating a copy of the vector for each thread and then no thread locking between the threads, trying to access a shared copy. Or, I could lock one copy of the vector and make all threads access that.
What is the latency of a thread lock, in comparison to copying the vector? How big would the vector have to be, to make the overhead of the lock faster than copying the vector?
Upvotes: 6
Views: 3310
Reputation: 227438
Assuming the vector doesn't change after the threads start accessing it, there is no need to lock it. If the thread that populates the vector finishes populating before the reader threads start reading, you are safe without any further synchronization.
Upvotes: 7
Reputation: 234514
If no thread ever writes to it, you can safely share it without any lock or copy. Data races can only occur if there are write accesses involved.
Upvotes: 12