1

I have a question: Are std::map and std::set thread-safe? I use this collections on my multithread applications and sometimes map and set works worng.

Thanks!

upd. My code:

std::map<int, unsigned long> ClientTable;

int sendulong(int socket, char * data) //<--- Many threads uses this function
{
  write(socket, ClientTable[socket]); //<--- ClientTable[[socket] <-- using of map
}

How can i fix this code for thread safety? Thanks!

Robert
  • 507
  • 2
  • 9
  • 20
  • The code is *wrong*. The call to write has one too many `[`, and is missing the `size` argument. When talking about multithreading, you should be clear on what the combination of operations is. In particular whether all threads will be just reading, or there might be threads concurrently writing to the containers. – David Rodríguez - dribeas Jul 08 '12 at 11:43
  • @DavidRodríguez-dribeas. Thanks for your answer. My combination: one threads is writinig, and some count of thread reading paralelly. What multithread scheme will be good? I have no experience in multithread programming ( – Robert Jul 08 '12 at 11:47
  • possible duplicate of [Do I need to protect read access to an STL container in a multithreading environment?](http://stackoverflow.com/questions/187583/do-i-need-to-protect-read-access-to-an-stl-container-in-a-multithreading-environ) – Bo Persson Jul 08 '12 at 12:41
  • @BoPersson, I'd say it is not a duplicate since the answer has changed since the original question. C++11 introduces requirements in this area which didn't exist when that question was asked. – edA-qa mort-ora-y Jul 08 '12 at 14:02
  • @edA - The rules for one writer, many readers, are the same. You need a lock. – Bo Persson Jul 08 '12 at 14:26
  • @BoPersson, there were not standard rules before, since concurrecy simply wasn't defined. – edA-qa mort-ora-y Jul 08 '12 at 14:36

3 Answers3

7

It depends on what you want to do. If all you're doing is reading from them, then yes. If you also write to them and a separate thread attempts to do anything else, or has living iterators, it won't work as expected.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    You must separate between _modifying the container structure_ and _modifying items in the container_. Usually you can modify two different elements in the container simultaneously without introducing a data race. – edA-qa mort-ora-y Jul 08 '12 at 14:01
4

No, they are not defined to be thread safe. You have to add synchronization mechanisms on top of standard library containers.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

The C++ standard says nothing about this.1 You will have to look at the documentation for the particular implementation of the standard library that you're using. But it's quite likely that it won't be thread-safe, so you will need to do synchronisation yourself.

(If you want to know how to do that, well, that's a different question topic...)


1. Pre-C11.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 3
    C++11 gives data race requirements for the containers. Refer to section 23.2.2. This has basically been standard in libraries before as well since it is the only sane option. – edA-qa mort-ora-y Jul 08 '12 at 14:00