OUString is mutable?
nthiebaud at gmail.com
Wed Oct 3 09:27:22 PDT 2012
On Wed, Oct 3, 2012 at 11:19 AM, Lubos Lunak <l.lunak at suse.cz> wrote:
> On Monday 01 of October 2012, Michael Stahl wrote:
>> On 01/10/12 11:40, Noel Grandin wrote:
>> > On 2012-10-01 10:15, Stephan Bergmann wrote:
>> >> While that argument is irrelevant in a C/C++ context, immutability is
>> >> also an important concept when reasoning about multi-threaded code.
>> >> Therefore, the distinction between OUString and OUStringBuffer IMO
>> >> does make sense after all.
>> > I don't see how the design helps you in a multithreaded context.
>> > If you share an OUString instance between two threads, either thread
>> > could assign to it, replacing it's contents, and invalidating what the
>> > other thread sees.
>> but if you share 2 copies of an OUString instance in 2 threads, they
>> could access the one underlying buffer concurrently without issue, and
>> when they want to point their copy at a different value, they can do
>> that independently of the other -- again, OUString works in exactly the
>> same way as a pointer to an immutable object here, if you copy the
>> pointer you can dereference it without issue and point your copy to a
>> different object; you only need a lock if you don't copy the pointer and
>> share a single pointer that one thread may update (which depending on
>> the situation may be necessary, say you don't just want to save memory
>> but actually always want the threads to point to the same value).
> Either I don't get what immutable actually means in this context (in which
> case I'd like to be enlightened), or you are talking about things that
> actually have nothing to do with immutability. OUString is not the same like
> a pointer to an immutable object, because this object contains a reference
No it does not... the underlying rtl_uString (the part that _is_
immutable) is ref-counted
but OUString itself is not.
iow: a given OUString may be changed to point to a rlt_ustring
instance or another, but cannot mess with the one it points to
More information about the LibreOffice