Thread: Which methods in std::vector are thread safe?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    Which methods in std::vector are thread safe?

    Which methods in std::vector are thread safe? If none, what data structures are there that support insert, read and replace operations that are thread safe? I'm using c++14

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_weed
    Which methods in std::vector are thread safe?
    std::vector actually has no methods in the C++ sense of the word: a method in C++ is another way of saying "virtual member function". But you're probably using the term in the more general object-oriented sense to mean a member function in C++, in which case the answer can be found in the standard (I'm quoting from C++11 because I never did get round to obtaining a copy of C++14):
    Quote Originally Posted by C++11 Clause 23.2.2
    Container data races

    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.

    [ Note: For a vector<int> x with a size greater than one, x[1] = 5 and *x.begin() = 10 can be executed concurrently without a data race, but x[0] = 5 and *x.begin() = 10 executed concurrently may result in a data race. As an exception to the general rule, for a vector<bool> y, y[0] = true may race with y[1] = true. — end note ]
    To put it another way: the const member functions are thread safe (and the non-const overloads are still "considered const" for this purpose in that merely calling them to read but not modify would be thread safe), but otherwise modifying the individual elements is only "partially thread safe" in that modifying different elements would be thread safe, but modifying the same element wouldn't be thread safe. Oh, and since the copy constructors can be invoked with the argument being in a const context, they are thread safe too.

    Quote Originally Posted by c_weed
    If none, what data structures are there that support insert, read and replace operations that are thread safe? I'm using c++14
    As you're using C++14, you can use the facilities from <atomic> or <mutex> to make your use of insert, read and replace operations guaranteed thread safe.
    Last edited by laserlight; 03-02-2019 at 05:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this thread safe?
    By theoobe in forum C# Programming
    Replies: 2
    Last Post: 09-22-2012, 02:27 AM
  2. popen thread safe?
    By Drogin in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2011, 07:36 AM
  3. Safe Thread Queue
    By mauricio.sch in forum C++ Programming
    Replies: 5
    Last Post: 01-29-2008, 06:19 PM
  4. Is this thread safe
    By Bru5 in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2006, 05:34 PM
  5. Thread-safe singleton.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 10:45 AM

Tags for this Thread