This is the mail archive of the
mailing list for the Cygwin project.
[OT] RE: Multi-threaded application using STL crashes
- From: "Gary R. Van Sickle" <g dot r dot vansickle at worldnet dot att dot net>
- To: <cygwin at cygwin dot com>
- Date: Fri, 22 Apr 2005 04:20:01 -0500
- Subject: [OT] RE: Multi-threaded application using STL crashes
This is a C++ question, but what the hell:
> I'm developing a multi-threaded application that makes
> extensive use of STL.
Problem number one, because...
> The number of threads is configurable.
> If I build the application to run in one thread, it's fine.
> However, if I build the application to run in 2 or more
> threads, it crashes in an unpredictable manner.
> I'm taking all the usual safeguards to protect shared
> resources (semaphores, critical sections etc), although I'm
> not building the application any differently from the way
> that I build single-threaded applications - I'm not aware
> that I have to.
> I've worked around some crashes, e.g.
> 1. I've replaced one use of std::vector with a proprietary
> solution because the application kept crashing in vector::resize().
> 2. I've had to add a call to vector::reserve() in another
> use of vector before making calls to vector::push_back(), to
> prevent the application from crashing.
> The fundamental questions I'd like to ask are:
> 1. Are there problems with using STL in multi-threaded applications.
...there are unfortunately rather serious problems in doing so. The C++
standard guarantees you *nothing* regarding STL's threadsafety, which means
that it does guarantee you that you'll have problems if you don't carefully
watch what you're doing. The main thing to remember is that if an STL
object is created in one thread, don't try to do anything with it directly
from a different thread or you'll:
- Crash if you're lucky.
- Have all kinds of flaky problems and portability issues if you're not.
If you want to access STL objects from multiple threads, you have to make
sure to take care of the synchronization yourself. The best thing to do
would be to create a wrapper object that threadsafe-ifies the underlying
Gary R. Van Sickle
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html