Is STL/STDC++ library thread safe? 
Author Message
 Is STL/STDC++ library thread safe?

MSDN seems to imply that you can link with a thread safe version of the
standard C++ template library. I'm currently using VC++ 6.0.

However, does this just mean that the library is re-entrant and uses a
locked allocator, or does it mean that you can have multiple threads
simultaneously updating a single STL object?

For example, if I wanted to have multiple threads updating the same STL
queue<> instance, would I have to add explicit locking code around the
updates?



Mon, 17 Dec 2001 03:00:00 GMT  
 Is STL/STDC++ library thread safe?

Quote:

> MSDN seems to imply that you can link with a thread safe version of the
> standard C++ template library. I'm currently using VC++ 6.0.

> However, does this just mean that the library is re-entrant and uses a
> locked allocator,

It means at least that much.

Quote:
> or does it mean that you can have multiple threads
> simultaneously updating a single STL object?

That much is not guaranteed at all.

Quote:
> For example, if I wanted to have multiple threads updating the same STL
> queue<> instance, would I have to add explicit locking code around the
> updates?

Absolutely yes.

The alternative, providing locks for every use
of containers and other objects, is way too
costly since most usage is single threaded,
even in multi-threaded programs.

--
Larry Brasfield
Above opinions may be mine alone.



Mon, 17 Dec 2001 03:00:00 GMT  
 Is STL/STDC++ library thread safe?
In that case, an ideal thing to be able to do would be to derive a "locked" version of such classes -
locked_map<>
locked_queue<>
etc.

I don't suppose anyone's heard of something like that?

For example, say I knew that it would be a queue of std::string's, I could
try writing something that uses the MFC CCriticalSection and CSingleLock classes
like the following:

typedef std::queue<std::string> string_queue;
class message_queue : public string_queue {
    private:
        CCriticalSection this_section;
    public:
        value_type& front() {
            CSingleLock lock(&this_section, true);
            return string_queue::front();
        }

        // and similar wrapper methods for EVERY SINGLE non-const accessor method

Quote:
};

However, implementing this would be tedious don't you think? Especially for every king of STL object. Do
you know of anyone
that's already "done" this tedious task (e.g. adding another template parameter for an optional "lock"
class that the user can supply)?

Or, does anyone know of some more elegant way of achieving the same thing? e.g. Doing some clever trick
with the template parameters? (I can't think of one myself but suspec that with all of C++'s bell's and
whistles, something might be arranged...)

Quote:


> > MSDN seems to imply that you can link with a thread safe version of the
> > standard C++ template library. I'm currently using VC++ 6.0.

> > However, does this just mean that the library is re-entrant and uses a
> > locked allocator,

> It means at least that much.

> > or does it mean that you can have multiple threads
> > simultaneously updating a single STL object?

> That much is not guaranteed at all.

> > For example, if I wanted to have multiple threads updating the same STL
> > queue<> instance, would I have to add explicit locking code around the
> > updates?

> Absolutely yes.

> The alternative, providing locks for every use
> of containers and other objects, is way too
> costly since most usage is single threaded,
> even in multi-threaded programs.

> --
> Larry Brasfield
> Above opinions may be mine alone.




Mon, 17 Dec 2001 03:00:00 GMT  
 Is STL/STDC++ library thread safe?

Quote:
>> For example, if I wanted to have multiple threads updating the same STL
>> queue<> instance, would I have to add explicit locking code around the
>> updates?

>Absolutely yes.

>The alternative, providing locks for every use
>of containers and other objects, is way too
>costly since most usage is single threaded,
>even in multi-threaded programs.

Its not too hard to make collections optionally thread safe and lockable.
All the collections in my CIDLib libraries are optionally
threadsafe/lockable on a per-instance basis. Each individual method will be
threadsafe, or you can lock it and do multiple operations atomically. It
uses my standard locker janitor, which can lock anything that implements an
MLockable interface. If you don't use it, the overhead is pretty trivial,
though I'm sure someone could come up with a really high performance
scenario where it might become an issue. But, for such people, the
implementation of some custom data structures is a small price to pay
relative to providing such a clean and powerful architecture for the other
98% of users.

--------------------------
Dean Roddey
The CIDLib Class Libraries
Charmed Quark Software

http://www.charmedquark.com

"100% Substance Free. More cost, less content"



Mon, 17 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ANN: sigslot - C++ Portable, Thread-Safe, Type-Safe Signal/Slot Library

2. Using a non-thread-safe library with threads?

3. Thread-safe STL containers

4. Does VC++ 6.0 have a thread-safe STL?

5. Making thread safe STL containers

6. STL thread-safe ?

7. Does VC STL thread-safe?

8. Is the VC++ STL Thread Safe?

9. Is STL thread safe ?

10. Is VC5 STL thread safe?

11. STL string class - is it thread safe

12. Is STL in VC5.0 thread safe?

 

 
Powered by phpBB® Forum Software