Most likely you are accessing the same 'std::string' object
from multiple threads without proper locking.
1) in case of potential problem write s1.assign (s2.c_str(), s2.lenght());
This will force a copy to be taken.
2) Roll your own ie write a string class MyString with the same interface as
std::string but implement it in a tread safe way.
We opted for the first solution but after repeated problems (people
forgetting to do the copy thread-safe) went for solution 2.
Peter
"Daniel Etzold" <det...@gmx-ag.de> wrote in message
news:qf4d8.1$6h...@news.ecrc.de...
> Paul Pluzhnikov wrote:
>
> >
> > Most likely you are accessing the same 'std::string' object
> > from multiple threads without proper locking.
>
> It's not the same string object. It's something like this:
>
> void func1(std::string& s)
> {
> ...
> s.assign(local_string);
> }
>
> Now, a lot of Threads are accessing this function with
> a reference on their own string object.
>
std::string s1 ("Toto");
std::string s2;
s2 = s1;
if (s1.c_str() == s2.c_str()) // compare the pointers
{
// ref counted
}
"Peter Gotink" <peter....@nais.be> wrote in message
news:3c7554d4$0$33499$ba62...@news.skynet.be...
> I forgot, you can easily see that std::string is actually refcounted
>
> std::string s1 ("Toto");
> std::string s2;
>
> s2 = s1;
> if (s1.c_str() == s2.c_str()) // compare the pointers
> {
> // ref counted
> }
Correction: you can easily see that it's refcounted in the
specific implementation that you're using. The C++ standard
does not require it to be reference counted, and there are
some popular implementations in which it is not.
(The main reason some implementations avoid reference counting
is that, in the presence of threads, it's much harder to get
things to work correctly if you use refcounting. As you've
found.)
Also, of course, the C++ standard doesn't mention threads.
If your library implementation claims to be thread safe,
however, you have found a bug and you should complain to
the vendor.
Just a few ideas/food for thought:
immutable_string< ...,
class thread_sharing_policy_for_representation =
/* -pthread option: shared/private by default */ >
string< ...,
class thread_sharing_policy_for_representation =
/* -pthread option: shared/private by default */ >
http://groups.google.com/groups?threadm=3BC8463D.6E23002A%40web.de
(note that unref() needs to inject write/"release" mem.bars too IF
threads could unref() mutable objects INSIDE synchronized regions
for mutation/updates)
regards,
alexander.