Thread safe of STL::vector

matchz
matchz used Ask the Experts™
on
Hi experts,
I would like to know is std::vertor is thread safe or not?

Can I have 2 threads, one for adding element into the vector while another one is removing the element from that vector?

If it is not thread safe, what should i do in windows environment in order to make it safe? It is good if any code examples can be given.

Thank you very much.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
It depends on the STL implementation you are using. In general, the answer would be "no".
Commented:
>>If it is not thread safe, what should i do in windows
>>environment in order to make it safe? It is good if any
>>code examples can be given.

If you're using VC++, it should be thread safe.
In any case, you could use Critical-Section API functions to make sure it's thread safe.

List of Critical-Section API Functions
DeleteCriticalSection
EnterCriticalSection
InitializeCriticalSection
InitializeCriticalSectionAndSpinCount
LeaveCriticalSection
SetCriticalSectionSpinCount
TryEnterCriticalSection

Author

Commented:
I am using the STL with MSVC. So is it not thread safe?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Commented:
As I previously stated, you could use Critical-Section API functions to make sure it's thread safe.
jkr
Top Expert 2012

Commented:
The STL version that comes with VC++ is _not_ thread safe (see the header files). AS Axter mentioned, you will have to take care of the thread-safety on your own.

Author

Commented:
Thank you.

Let me try.
jkr
Top Expert 2012

Commented:
Interesting - you accepted an answer that stated that VC's STL is thread safe and points you to functions that allow you to make it thread safe...

Commented:
As I previously stated, you could use Critical-Section API functions to make sure it's thread safe.

Commented:
Sorry for the double post.

jkr,
What makes you think the stl::vector is not thread safe?

Author

Commented:
Actually, I should divide the points into half.
You gave the answer of thread safety and he gave me about critical section.

Commented:
I agree with jkr about the part that it depends on STL implementation.

http://www.sgi.com/tech/stl/thread_safety.html

and I found this in forum for STLPort

http://www.stlport.com/dcforum/DCForumID6/1057.html

Commented:
FYI:
I'm adding the following information to make this PAQ more complete (and more accurate).

VC++ comes with a thread safe STL version, which of course include the std::vector class.
However, just because a class is thread safe, that does not make the an instance of the class thread safe.

I've created a sync_ptr wrapper class that can be used to make an instance of any object thread safe.
See following link:
http://code.axter.com/sync_ptr.h

The above class is portable between Win32, UNIX, Linux, and any other POSIX compliance OS.
There's example code in the commented section, and later I'm going to include a link in the comment section that will have more detailed examples and a better description.

You can easily use the sync_ptr class to make an instance of std::vector thread safe.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial