tags:

views:

156

answers:

3

Recently, I was buzzed by the following problem STL std::string class causes crashes and memory corruption on multi-processor machines while using VC6.

I plan to use an alternative STL libraries instead of the one provided by VC6.

I came across 2 libraries : STLPort and SGI STL

I was wondering what is the difference between the 2. Which one I should use? Which one able to guarantee thread safety?

Thanks.

A: 

I don't know much about STLPort, but looking at their page describing thread safety, they don't provide anything more than SGI's implementation. They refer you to SGI's page. STLPort seems mainly useful for platform portability.

The STL containers are not safe for concurrent writes, but can be read by multiple threads. If you intend to do concurrent writes, you will need to provide your own mutex (such as the one provided by boost).

The SGI site has a full explanation of the SGI thread safety policy.

It appears that VC6 was shipped with a bad library that had a bug in the reference counting of copy-on-write strings.

Stephen
A: 

Just a tip of advice.
When we made the move from VC6-standard to STLPort stl the major difference I noticed was the erase method for collections.

In VC6 erase returns the next valid iterator.
In STLPort it simply doesn't.

So for those cases you would have to write something like this:

for(iterator it = begin; it != end; )
{
    iterator next = it;
    ++next;

    if ( cond )
        collection.erase(it);
}

Good luck!

leiflundgren
things like `map` and `set` don't (no need because erasure doesn't invalidate other iterators. But the rest sure do. However, I could be mistaken, but I believe that it has been proposed to make `map/set` return the next valid one in a future standard.
Evan Teran
@leiflundgren: #1, the code you have posted invokes undefined behavior for every container except `std::list<t>`. #2, you should be using `std::remove_if` anyway.
Billy ONeal
@Billy: you're right, the correct solution for `set/map` is: `iterator it = c.begin(); while(it != c.end()) { if(cond) { c.erase(it++); } else { ++it; } }`
Evan Teran
A: 

Here is the story behind the relation of STLPort and SGI STL

http://stlport.sourceforge.net/History.shtml

Yan Cheng CHEOK