multithreading


What operations are thread-safe on std::map?


Suppoes I have:
stl::map<std::string, Foo> myMap;
is the following function thread safe?
myMap["xyz"] ?
I.e. I want to have this giant read-only map that is shared among many threads; but I don't know if even searching it is thread safe.
Thanks!
EDIT:
Everything is written to once first.
Then after that, multiple threads read from it.
I'm trying to avoid locks to make this as faast as possible. (yaya possible premature optimization I know)
In theory no STL containers are threadsafe. In practice reading is safe if the container is not being concurrently modified. ie the standard makes no specifications about threads. The next version of the standard will and IIUC it will then guarantee safe readonly behaviour.
If you are really concerned, use a sorted array with binary search.
C++11 requires that all member functions declared as const are thread-safe for multiple readers.
Calling myMap["xyz"] is not thread-safe, as std::map::operator[] isn't declared as const.
Calling myMap.at("xyz") is thread-safe though, as std::map::at is declared as const.
At least in Microsoft's implementation, reading from containers is thread-safe (reference).
However, std::map::operator[] can modify data and is not declared const. You should instead use std::map::find, which is const, to get a const_iterator and dereference it.
Theoretically, read-only data structures and functions do not require any locks for thread-safety. It is inherently thread-safe. There are no data races on concurrent memory reads. However, you must guarantee safe initializations by only a single thread.
As Max S. pointed out, mostly implementation of reading an element in map like myMap["xyz"] would have no write operations. If so, then it is safe. But, once again, you must guarantee there is no thread which modifies the structure except the initialization phase.
STL collections aren't threadsafe, but it's fairly simple to add thread safety to one.
Your best bet is create a threadsafe wrapper around the collection in question.

Related Links

What is process interleaving? (in the realm of Concurrency)
gcc : migrate __sync to __atomic
Accessing file handles across multiple threads in TCL
Timer in a child thread not working
Mule VM - data loss / Not processing all data in the VM
pthread_create () not creating threads
Is there a way to keep visual studio 2010 from switching threads when debugging?
How to handle ADO fatal network errors without interrupting threads' execution?
Faster thread safe function for splitting Strings in C
Using Linux C. I use sleep in thread function,but the whole process ends? [closed]
How to stop Azure Worker Role
Designing concurrency in a Python program
How to use an initialization list with std::thread as a member in C++11
Implementing solution for readers-writers with readers preference
Point of having more than one condition variable per lock?
Setting Number of Threads in Distributed Testing in JMeter

Categories

HOME
compiler-construction
wso2-am
omnet++
tizen
google-api-php-client
smarty
layout
tesseract
read-eval-print-loop
xmpp
setup-deployment
azure-media-services
webrequest
moonmail
windows-server-2012
quickfix
decomposition
telephony
graphlab
serilog
database-replication
excel-vba-mac
jprofiler
java-3d
pass-by-reference
tibco-mdm
format-specifiers
sqlcipher
sparse-matrix
cas
url-scheme
internet-explorer-9
srcset
gsoap
android-kernel
mmenu
wixsharp
vapor
unobtrusive-validation
hue
temporary-files
elasticsearch-plugin
fedex
jvm-languages
firebase-admin
android-mediaprojection
isbn
password-encryption
reactive-cocoa-5
pango
diagnostics
feeds
dtexec
boost-preprocessor
zendesk-app
query-performance
sts-springsourcetoolsuite
azure-virtual-network
mikroc
merge-conflict-resolution
xml-attribute
autorest
react-native-listview
essence
master-slave
pervasive-sql
msys
jqgrid-formatter
sframe
website-monitoring
issuu
google-reader
tld
jubula
valueconverter
poller
didselectrowatindexpath
oracle-warehouse-builder
access-rights
google-cloud-save
itmstransporter
buildr
amazon-appstore
deobfuscation
android-sdk-2.1
subviews
preference
backcolor
data-driven
project-hosting
aquaticprime
thread-local-storage
3gp
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App