Click to See Complete Forum and Search --> : does c++ map find function support multithreading?


zeng.zinan
September 29th, 2009, 12:21 PM
I am running a program that have multithread trying to get access the
map via find function, while there is no write operation at the same
time. but unfortunately, it crashes all the time. I am using Visual
Studio to run [ so I believe the library it is using is from MS ]. So
it due the fact that the find function does not support
multithreading? as far as I know, STL does not support
multithreading..

Thanks

MrViggy
September 29th, 2009, 12:51 PM
STL does not support multithreading. However, are you sure there is no write at the same time? If so, then how are you accessing the map (from a DLL, for example)?

Viggy

DreamShore
October 1st, 2009, 06:03 PM
It's not that STL doesn't support multithreading... It's just that it is not thread-safe...

Add locks yourself. There's no point adding locks into STL in case that it would still be used without multithreading.

If you find while inserting, it for sure will crash.

Ajay Vijay
October 2nd, 2009, 05:19 AM
It's not that STL doesn't support multithreading... It's just that it is not thread-safe...Essentially means the same.

You may want to use Intel's TBB (http://www.threadingbuildingblocks.org/). I am not sure if it supports parallel map, but it supports parallel vector, list etc.

Lindley
October 4th, 2009, 08:17 PM
I suggest using a read/write lock. This will allow parallel reads, but only one write at a time (and no reads when that occurs). Most threading libraries have one.

DreamShore
October 5th, 2009, 02:53 PM
As for a map, reading while writing could still lead to crash. Both reads and writes need to be guarded.

MrViggy
October 5th, 2009, 03:24 PM
As for a map, reading while writing could still lead to crash. Both reads and writes need to be guarded.
Reading while writing anything in a multi-threaded app can cause issues. Unless the write operation is atomic.

Viggy

code_carnage
November 16th, 2009, 04:56 AM
>>Unless the write operation is atomic.

And how could it be atomic in STL map..?? Ofcourse when not properly synchronized...

zeng.zinan
December 13th, 2009, 09:06 AM
I am sure that there is no write operation when find is in progress, in this case, is it multithread-safe?

Codeplug
December 13th, 2009, 11:38 AM
Since there is no thread concept in C++03, your specific STL implementation should document what is and isn't thread-safe.

Some of the docs:
http://www.stlport.org/doc/sgi_stl.html#thread_safety
http://www.sgi.com/tech/stl/thread_safety.html
http://msdn.microsoft.com/en-us/library/c9ceah3b%28VS.80%29.aspx
http://developers.sun.com/solaris/articles/stl-new.html
http://www.dinkumware.com/manuals/?manual=compleat&page=thread_safety.html

gg

Arjay
December 13th, 2009, 03:44 PM
I am sure that there is no write operation when find is in progress, in this case, is it multithread-safe?Any number of read operations are allowed from different threads and long as no write operations are occurring at the same time. If you can ensure this, then you'll be okay.

However, I would still use a reader/writer lock to ensure this will always be the case. Too often a piece of code starts out with performing operations in a specific sequence (ie. the writes at a point in time, then the reads) and there isn't any issue. Then someone else comes along and changes the program without understanding the rigid sequence in which things must be performed and random errors start appearing.

Because of this, I would recommend that you always protect any resource that's shared between threads - regardless of whether at the time you are controlling the sequence of read and writes.