Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ threads
Hi,
 
I am using std::vector in a multithreaded envioronment. All threds work on diffferent elements of the vector. But what i observed is vector operations are taking more time in multithreaded env than in single threaded.
 
Is vector thread safe which might be causing the delay
 

Thanks in advance.
Posted 17-Jul-11 23:48pm
Edited 17-Jul-11 23:50pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See this discussion[^] in MSDN.
  Permalink  
Comments
SAKryukov at 18-Jul-11 13:19pm
   
It does answers the question, my 5.
--SA
ThatsAlok at 19-Jul-11 6:48am
   
nice link!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The discussion pointed out by Richard is nothing more than a complicated way to say
"no: no locking is done on the collection and on elements themselves", as you can (not easily, because of lot of macros and definitions that obfuscate the real code) see by looking the source code. There is nothing protecting against threads conflicts.
 
The different timing observed is not due to "locking" but to the impossibility to manage all the iteration into the 1st level cache of a core: if the various threads are hosted on different cores, a RAM access is required for every single write, an every subsequent read.
  Permalink  
v2
Comments
SafarTimura at 18-Jul-11 8:51am
   
Indeed Emilio is correct, vectors are just like any base type of data type and NOT thread safe.
SAKryukov at 18-Jul-11 13:19pm
   
True, my 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Although other solutions are goods, I will just provide some analogies and hints that might help understand how safe it is...
 
Safety would be very similar to what you would expected from a dynamically allocated C array.
 
Knowing which operations can invalidate iterators would also help to understand what would be safe to do.
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 18 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100