Click here to Skip to main content
15,881,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 17-Jul-11 22:50pm
v3

See this discussion[^] in MSDN.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jul-11 13:19pm    
It does answers the question, my 5.
--SA
ThatsAlok 19-Jul-11 6:48am    
nice link!
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.
 
Share this answer
 
v2
Comments
Guyverthree 18-Jul-11 8:51am    
Indeed Emilio is correct, vectors are just like any base type of data type and NOT thread safe.
Sergey Alexandrovich Kryukov 18-Jul-11 13:19pm    
True, my 5.
--SA
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.
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900