Safe parallel read-only access to a STL container Safe parallel read-only access to a STL container multithreading multithreading

Safe parallel read-only access to a STL container


A data-race, from the C++11 specification in sections 1.10/4 and 1.10/21, requires at least two threads with non-atomic access to the same set of memory locations, the two threads are not synchronized with regards to accessing the set of memory locations, and at least one thread writes to or modifies an element in the set of memory locations. So in your case, if the threads are only reading, you are fine ... by definition since none of the threads write to the same set of memory locations, there are no data-races even though there is no explicit synchronization mechanism between the threads.


Yes, you are right. You are safe as long as the thread that populates your vector finishes doing so before the reader threads start. There was a similar question recently.