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

Race Condition - Can Mutexes Be More Flexible?
How best to instantiate execution contexts?
Rxjava: Subscribe on the specific thread
OpenFileDialoug Current Thread Must Be STA before OLE calls made
OpenGL: glClientWaitSync on separate thread
Scala Actor: Will messages passed from one actor to another within the same process always be processed in the original order?
Changing hue of SKSpriteNode textures on background thread
OpenMPI vs OpenMP execution efficiency
TcpServer MultiThead
Time delay in Matlab for a specific function, while letting the rest of the functions run
How to control simultaneous access to multiple shared queues by multiple producers?
Does Swift have any native concurrency and multi-threading support?
Multiple consumer and producers, how do I unlock consumer thread
Javafx Task<ObserveableList>.updateValue only fires first change event
How does Blocking I/O operation works in a multithreaded C++ application?
WatchOS and when to put methods on the main thread

Categories

HOME
pdf
osgi
webpack
path-finding
youtube-api-v3
rdf
yum
networkx
mouse
directx
slick-slider
spring-xd
windows-server-2012
django-admin
saxon
finite-automata
reactcsstransitiongroup
normalizr
scichart
cx-freeze
calibre
avcapturesession
windows-dev-center
overlap
plsql-psp
io-redirection
devextreme
swisscomdev
hot-module-replacement
unobtrusive-validation
google-closure
spring-security-kerberos
modelmapper
angular-resource
upstart
pnotify
knockout-components
hittest
dynamics-sl
vtigercrm
thrust
xml-attribute
lttng
hover-over
packagist
revolution-r
asp.net5
two-factor-authentication
thredds
wordml
jqgrid-formatter
tarjans-algorithm
rgeo
arcanist
graph-drawing
mono-embedding
sonarqube5.1.2
umbraco6
npapi
facebook-graph-api-v2.4
dia
mdt
websocket4net
xojo
article
tld
datagridviewcolumn
hyprlinkr
pyhdf
limejs
twrequest
bubble-chart
hgsubversion
transactionscope
psi
servicehost
lang
gamequery
gin
zend-tool
sector
mediarss

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App