Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-01-08 07:48:14


"Joe Gottman" <jgottman_at_[hidden]> writes:

>> > One major use for unique() is copy-on-write. The code would look
>> > something like:
>> >
>> > void write_to(shared_ptr<Foo> p, const Foo &newValue)
>> > { // For simplicity, assume p.get() != 0
>> > if (p.unique()) {
>> > *p = newValue;
>> > } else {
>> > p.reset(new Foo(newValue));
>> > }
>> > }
>> >
>> > Under the current definition of unique(), this code is impossible to make
>> > thread-safe if weak_ptrs might exist.
>>
>> Would it work just as well if unique() returned weak_count?
>>
>
> It would work better. If nobody is using weak_ptrs then weak_count equals
> 1 if and only if shared_count equals 1. And if weak_ptrs are possible, then
> having unique() depend on weak_count guarantees that this code is
> thread-safe.

Are there any other use cases? I think I was the one to argue for
"unique" in the first place, and COW was the only use I had in mind...

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk