Post by FaisalBut still MFC CString class uses this reference counting and they are
thread safe too. right?
Reference Counting : It does for Visual C++ 6.0. I am not sure if that is
true for later versions.
Thread Safety:
They are thread safe at the class level and only with the programmers help.
It is possible to deliberately break CString
See
http://msdn.microsoft.com/en-us/library/aa300688(VS.60).aspx
Extract:
CString assists you in conserving memory space by allowing two strings
sharing the same value also to share the same buffer space. However, if you
attempt to change the contents of the buffer directly (not using MFC), you
can alter both strings unintentionally. CString provides two member
functions, CString::LockBuffer and CString::UnlockBuffer, to help you
protect your data. When you call LockBuffer, you create a copy of a string,
then set the reference count to -1, which "locks" the buffer. While the
buffer is locked, no other string can reference the data in that string, and
the locked string will not reference another string. By locking the string
in the buffer, you ensure that the string's exclusive hold on the data will
remain intact. When you have finished with the data, call UnlockBuffer to
reset the reference count to 1.
But you seem to be clinging to thread-safety like a lifebelt.
You realise that even if 2 objects are individually thread-safe, combined
they may not be.
If you had
CString husband;
CString wife;
and the contents of both are only supposed to be updated together, there is
nothing stopping a thread-switch after husband is updated.
Now a 2nd read reading both will have the wrong pair.
Also see
http://msdn.microsoft.com/en-us/library/h14y172e(VS.80).aspx
where it says
For size and performance reasons, MFC objects are not thread-safe at the
object level, only at the class level. This means that you can have two
separate threads manipulating two different CString objects, but not two
threads manipulating the same CString object. If you absolutely must have
multiple threads manipulating the same object, protect such access with
appropriate Win32 synchronization mechanisms, such as critical sections. For
more information about critical sections and other related objects, see
Synchronization in the Platform SDK.
The bottom line is you have to do some work yto make your application
thread-safe.
It is insufficient just to rely on components being thread-safe.
Stephen Howe