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!
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.