Are STL Map or HashMaps streams safe?

Can I use a card or hash card in a multi-threaded program without the need for blocking? that is, they are thread safe?

I want to potentially add and remove from the card at the same time.

There seems to be a lot of conflicting information.

By the way, I use the STL library that comes with GCC under Ubuntu 10.04

EDIT: Like the rest of the Internet, do I seem to get conflicting answers?

+4
source share
5 answers

You can safely perform simultaneous read operations, i.e. call member functions. But you cannot perform simultaneous operations if one of them includes recording, i.e. A call to non-constant member functions must be unique to the container and cannot be mixed with any other calls.

i.e. You cannot change a container from multiple threads. Therefore, you need to use lock / rw-lock to make access secure.

+14
source

Not.

Honest. Not.

change

OK, I will get the qualification.

You can have any number of threads viewing the same map. It makes sense because reading it has no side effects, so it doesn't matter if anyone else does it.

However, if you want to write to it, you need to get exclusive access, which means preventing any other threads from writing or reading until you finish.

Your initial question was about adding and removing in parallel. As they both record, the answer to the question of whether they are thread safe is a simple, unequivocal no.

+4
source

TBB is a free, open source library that provides thread-safe associative containers. ( http://www.threadingbuildingblocks.org/ )

+3
source

The most commonly used STL container stream security model is SGI one:

The SGI STL implementation is thread safe only in the sense that simultaneous access to individual containers is safe and at the same time read access to shared containers is safe.

but, in the end, before the authors of the STL-AFAIK library, the standard does not say anything about the security of STL streams.

But according to docs, the GNU stdC ++ implementation follows it (starting with gcc 3.0+) if a number of conditions are met.

E.I.V.

+2
source

The answer (like most threading issues) will work most of the time. Unfortunately, if you catch the card while it changes, then you will be in trouble. Then no.

To get maximum performance, you will need a multi-stage lock. Firstly, a read lock, which allows accessors that cannot change the map and which can be held by multiple threads (more than one thread reading element in order). Secondly, a record lock, which is exclusive, which allows you to modify the card in such a way that it can be dangerous (add, delete, etc.).

edit Read-write locks are good, but whether they are better than the standard mutex depends on the usage pattern. I can not recommend without knowing more. Profile and see what best suits your needs.

0
source

All Articles