STL thread safety ?

STL thread safety ?

Post by Pieter Thysebaer » Sat, 01 Sep 2001 00:31:16



Hi,

I'm concerned about the thread safety of the STL supplied with gcc.

So my question is basically : is it thread safe or not ? ;-)

By that I mean: do I have to protect calls to (for instance, could be any
read/write operation on an STL component) avector.push_back() myself using
mutex-operations (and the like) or will push_back protect itself from
another push_back (from another thread) on the same vector?

Since there's something out there called STLport, which claims to be
thread-safe, I've got a bad feeling about the STL that ships with gcc.

Pieter

 
 
 

STL thread safety ?

Post by Ben Hutching » Sat, 01 Sep 2001 06:13:57



> Hi,

> I'm concerned about the thread safety of the STL supplied with gcc.

> So my question is basically : is it thread safe or not ? ;-)

The answer isn't a simple yes or no, and it may depend on the
version of gcc.

See <http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#3>.

Quote:> By that I mean: do I have to protect calls to (for instance, could be any
> read/write operation on an STL component) avector.push_back() myself using
> mutex-operations (and the like) or will push_back protect itself from
> another push_back (from another thread) on the same vector?

> Since there's something out there called STLport, which claims to be
> thread-safe, I've got a bad feeling about the STL that ships with gcc.

The standard C++ library container are supposed to be pretty
lightweight, so few if any implementations will use mutexes to support
multiple writers.  In any case, mutexes on basic operations won't help
to deal with problems such as invalidated iterators.  So, yes, you do
have to use your own mutexes, whichever library you use.

 
 
 

1. glibc2: reentrancy, thread-safety, signal-safety

I have heard that glibc2 is close to thread-safe. Does anyone know how
thread thread safety is implemented. Does thread-safety mean signal-safety
that is: I can use --say-- malloc() both in the main process-flow code and
in signal handlers. I suppose it would fairly simply to enforce
thread-safety by using library owned mutexes or critical sections. But this
would preclude signal safety because of potential deadlocks.

By the same token, does anyone know how reentrancy relates to thread-safety
and signal-safety.

Thanks.

Peter

2. HP98751a/GDM-1952 Modelines for Xfree?

3. LinuxThreads and thread-safety, re-entrancy, async-safety!

4. Problems compiling kernel and pdksh

5. Thread safety confusion Pl help

6. Foolish project: an embedded fault tolerant disk array

7. Sockets and Thread Safety

8. Postscript & HPDeskjet

9. Linux libraries and thread-safety!!!

10. VisualAge C++, exception handling, and thread safety

11. A question on thread-safety

12. Thread-safety of X11-app

13. Thread safety confusion pl help