Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threadsafe Vector class for C++

Does anyone know a quick and dirty threadsafe vector class for c++? I am multithreading some code, and I believe the problem I have is related to the way the vectors are used. I plan to rewrite the code, but before I go crazy redoing the code, I would like to test it with a threadsafe vector to be sure. I also figure if such a thing is out there, it would be much easier than writing my own version.

like image 224
ras2124 Avatar asked Jul 08 '09 17:07

ras2124


People also ask

Is ++ thread-safe in C?

The C++ standard does not define the term “thread-safe”, but it is common practice now within the C++ community to define it in the following way: thread-safe: A type is thread-safe if it is is safe to invoke any of its methods concurrently.

What is thread-safe code in C?

A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface.

Is printf () thread-safe?

the standard C printf() and scanf() functions use stdio so they are thread-safe.

What is not thread-safe in C?

These extended mathlib functions use a global variable, _signgam , so are not thread-safe. The C89 multibyte conversion functions (defined in stdlib. h ) are not thread-safe, for example mblen() and mbtowc() , because they contain internal static state that is shared between all threads without locking.


3 Answers

This is difficult because of algorithms.

Suppose you wrapped vector so that all its member functions are serialised using a mutex, like Java synchronized methods. Then concurrent calls to std::remove on that vector still wouldn't be safe, because they rely on looking at the vector and changing it based on what they see.

So your LockingVector would need to specialize every template in the standard algorithms, to lock around the whole thing. But then other algorithms like std::remove_if would be calling user-defined code under the lock. Doing this silently behind the scenes is a recipe for locking inversion as soon as someone starts creating vectors of objects which themselves internally take locks around all their methods.

In answer to your actual question: sorry, no, I don't know of one. For a quick test of the kind you need, I recommend that you start out with:

template <typename T>
class LockedVector {
    private:
    SomeKindOfLock lock;
    std::vector<T> vec;
};

Then drop it in as a replacement container, and start implementing member functions (and member typedefs, and operators) until it compiles. You'll notice pretty quickly if any of your code is using iterators on the vector in a way which simply cannot be made thread-safe from the inside out, and if need be you can temporarily change the calling code in those cases to lock the vector via public methods.

like image 183
Steve Jessop Avatar answered Oct 16 '22 00:10

Steve Jessop


You can check out TBB (like concurrent_vector). I've never used it though, honestly, I find putting the scope guard objects around access easier (especially if the vector is properly encapsulated).

like image 43
Todd Gardner Avatar answered Oct 15 '22 23:10

Todd Gardner


I think you'll find it far easier to continue to use std::vector, but to protect concurrent access using some kind of mutex or other operating system synchronization object. You'll also definitely want to use RAII if you are using a mutex.

like image 34
Brian Neal Avatar answered Oct 15 '22 22:10

Brian Neal