4 ms·
Swift value constant can mutate using Copy-on-Write (CoW) and multi-threading
- borland 8y agoI 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.
- drefanzor 8y agoSynchronize your threading.
- AstralStorm 8y agoOr make it atomic. CoW write is not necessarily atomic. (Obviously you can end up with old state.)
- olliej 8y agoTLDR: user doesn’t lock/synchronize around multithreaded write to a data type. Blames the language rather than their buggy code.
- AstralStorm 8y agoWell, non-atomic CoW types are counterintuitive.
- bsaul 8y agojust 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) ?