-
September 29th, 2009, 11:21 AM
#1
does c++ map find function support multithreading?
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
-
September 29th, 2009, 11:51 AM
#2
Re: does c++ map find function support multithreading?
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
-
October 1st, 2009, 05:03 PM
#3
Re: does c++ map find function support multithreading?
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.
Nope
-
October 2nd, 2009, 04:19 AM
#4
Re: does c++ map find function support multithreading?
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. I am not sure if it supports parallel map, but it supports parallel vector, list etc.
-
October 4th, 2009, 07:17 PM
#5
Re: does c++ map find function support multithreading?
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.
-
October 5th, 2009, 01:53 PM
#6
Re: does c++ map find function support multithreading?
As for a map, reading while writing could still lead to crash. Both reads and writes need to be guarded.
Nope
-
October 5th, 2009, 02:24 PM
#7
Re: does c++ map find function support multithreading?
Originally Posted by DreamShore
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
-
November 16th, 2009, 04:56 AM
#8
Re: does c++ map find function support multithreading?
>>Unless the write operation is atomic.
And how could it be atomic in STL map..?? Ofcourse when not properly synchronized...
Dont forget to rate my post if you find it useful.
-
December 13th, 2009, 09:06 AM
#9
Re: does c++ map find function support multithreading?
I am sure that there is no write operation when find is in progress, in this case, is it multithread-safe?
-
December 13th, 2009, 11:38 AM
#10
Re: does c++ map find function support multithreading?
-
December 13th, 2009, 03:44 PM
#11
Re: does c++ map find function support multithreading?
Originally Posted by zeng.zinan
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|