Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::vector thread-safe and concurrent by default? Why or why not?

What does it mean to make a dynamic array thread-safe and concurrent? Say, for example, std::vector.

  1. Two threads may want to insert at the same position. No synchronization needed as it will be done as per thread scheduling.
  2. One thread erasing and another going to access the same element? This is not a data structure issue I believe, it is a usage problem.

So is there anything that needs to be done over std::vector to make it thread-safe and concurrent or is it thread-safe and concurrent by default?

like image 726
Abhishek Jain Avatar asked Jun 30 '15 07:06

Abhishek Jain


People also ask

Are std :: vectors thread safe?

const and Thread Safety The C++11 standard does not expect to be able to safely call non const functions simultaneously. Therefore all classes available from the standard, e.g. std::vector<>, can safely be accessed from multiple threads in the same manner.

Is vector read thread safe C++?

YES for the scenario you mention, it is perfectly Thread Safe. Actually, STL is not a correct way of referring it. It is the C++ Standard Library. The C++03 Standard does not talk about concurrency at all, So the concurrency aspect is left out as an implementation detail for compilers.

Are vectors concurrent?

Vectors originating from same point are known as concurrent vectors. Vector lying in same plane are called coplanar vectors. In figure, A, B and C are coplanar and concurrent vectors.

Is std::vector continuous?

Yes, the elements of a std::vector are guaranteed to be contiguous.


1 Answers

C++11 says the following about the thread safetly of containers in the standard library:

23.2.2 Container data races [container.requirements.dataraces]

For purposes of avoiding data races (17.6.5.9), implementations shall consider the following functions to be const: begin, end, rbegin, rend, front, back, data, find, lower_bound, upper_bound, equal_range, at and, except in associative or unordered associative containers, operator[].

Notwithstanding (17.6.5.9), implementations are required to avoid data races when the contents of the contained object in different elements in the same sequence, excepting vector<bool>, are modified concurrently.

So, basically reading from a container from multiple threads is fine, and modifying elements that are already in the container is fine (as long as they are different elements).

So, neither of your two more specific questions are thread safe for std::vector:

1) Two threads inserting into the vector is modifying the vector itself - not existing separate elements.

2) One thread erasing and other walking to access the same element is not safe because erasing an element from the vector isn't an operation that is promised to be thread safe (or "free from data races", as the standard puts it).

To perform those operations safely will require that the program impose some external synchronization itself.

like image 162
Michael Burr Avatar answered Oct 05 '22 06:10

Michael Burr