Article

From:
To:
John May
Subject:
Re: Thread safety with UnicodeString type [Edit]
Newsgroup:
embarcadero.public.cppbuilder.language.cpp

Re: Thread safety with UnicodeString type [Edit]

John wrote:

> Now assuming 2 threads share this task list throudh Queue functions
> (and possibly others like QueueDel, QueueRearrange etc.) which of
> course are protected by boost::mutex, is the above sufficient to
> protect it against possible issues with UnicodeString reference
> counting being accessed from 2 threads?

UnicodeString reference counting will work just fine.  You don't have to 
worry about protecting it.

> The way I understand it - reference counting is disabled when you use
> const UnicodeString reference as in above structure.

When you access a UnicodeString via a 'const' reference, the UnicodeString's 
reference count is not incremented.  That much is true.  But its data payload 
still has an active reference on it.  If you pass that const UnicodeString 
to a non-const UnicodeString, the data payload's reference count still gets 
incremented.  So in your example, TTaskListEntry's constructor increments 
the UnicodeString reference count (twice actually, but one of the references 
will get decremented when the constructor exits) when it assigns the TaskText 
member.  When QueueAdd() pushes into the vector, a new TTaskListEntry is 
created. So even though the input TTaskListEntry is passed by a const reference,
UnicodeString will still do the right thing and manage its own reference 
count appropriately.

> Should I be looking at copying strings or maybe passing pointer to
> wchar_t instead?

No, you do not need to do that (unless you really want to).  Let the reference 
counting do its job for you.

-- Remy Lebeau (TeamB)
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Thu, 14 Nov 2024 17:30:03 UTC
Copyright © 2009-2024
HREF Tools Corp.