
Swift value constant can mutate using Copy-on-Write (CoW) and multi-threading - mpweiher
https://gist.github.com/drewmccormack/b1c4487935cf3c3e0a5feaf488a95ebd
======
borland
I don't understand the reason behind this example?

It boils down to "I did a read-modify-write without a lock, now there is a
bug". Of course there is! that's what locks are for!

The swift value system and copy-on-write do not somehow absolve you from
locking.

------
bsaul
just to make sure : do we agree here that the race condition actually happen
on the « isUniquelyReferenced » check followed by the write ?

So the race isn’t because COW over struct in swift isn’t atomic ( it is), but
because he uses a class and tries to build cow over it ( but does so
improperly) ?

------
drefanzor
Synchronize your threading.

~~~
AstralStorm
Or make it atomic. CoW write is not necessarily atomic. (Obviously you can end
up with old state.)

------
olliej
TLDR: user doesn’t lock/synchronize around multithreaded write to a data type.
Blames the language rather than their buggy code.

~~~
AstralStorm
Well, non-atomic CoW types are counterintuitive.

