[wx-dev] wxString counting thread-safe?

Armel Asselin asselin.armel at wanadoo.fr
Tue Aug 8 09:51:32 PDT 2006


> AA> Because of the very cool implications of the "copy on write" rules, 
> just
> AA> changing the ++ and -- to interlocked increment / decrement is enough 
> to
> AA> reach our target
>
> I'm unsure of this. Maybe you're right but maybe not. wxString was not
> written to be MT-safe.
>
> For example wxStringData methods IsEmpty(), IsShared() and Validate()/
> IsValid() have no meaning in an MT-safe class. So all code using them 
> would
> have to be reviewed.
just some explanation:
- the first expectation from any reference counting stuff: if you do not 
have data, you do not have a reference count for you, if you have you count 
for exactly 'one' in the count, so
- the pointer object (wxString in our case) is never itself shared between 
threads or it must be protected (or must be designed for Single Assignment 
== immutable == never change once created)
- IsEmpty( ): if data was empty, it cannot become not empty unless the 
current thread making not empty: same assumption as in non-multithreaded
- IsShared( ): if you have no data, it's nonsense. if you have data, either 
you are the single one to have a reference and it means that no other thread 
else can acquire anymore the data, so you are really single or the count is 
more than 1 and the current wxString could become alone pointing to the data 
during the copy but that's ok, not really efficient though. but shared 
string are by definition not changed more than once.

don't know exactly what is Validate/IsValid, but they probably are based on 
above assumptions.

> I'd advise building with wxUSE_STL==1, most std::string implementations
> nowadays are MT-safe. This seems by far the safest way to solve the
> problem.
hum it looks somewhat as if wxString non STL implementation is more or less 
bound to be abandonned... so why not i already have some std::string around, 
so i'll try that.

Armel





More information about the wx-dev mailing list