Re: [Phix] Question About Assignment
- Posted by petelomax Nov 08, 2021
- 666 views
Thread safety. If multiple threads are calling (eg) [s]printf(), and that was using a common shared "" constant, they c/would stomp all over each other's reference counts, so using repeat(' ',0) instead of "" creates a new private thread-specific (and in fact call-specific) string, obviously with a private reference count that no other thread can possibly access.
If you have a (hidden) reference count of 2, and (without either locking or privacy) one thread increments it at the exact same time as another decrements it, you will end up with either 1 or 3 instead of the desired 2.
Naturally the reference count on a constant should never drop below 1, and catastrophy ensues if it ever does, but it is incremented to trigger either copy-on-write processing or a p2js violation under "with js".