"why is reading not thread-safe?" Code Answer


reading is thread safe, no problems..... until something writes to the location you're reading from, and then... well, hopefully you'll read before the data was changed, or read after the data was changed (in these cases, no worries), but sometimes, just when you really don't want it, you'll read half-way through the write and then you'll get compete garbage data.

the way to mitigate this is to ensure that you only read either before or after any writes, which requires you to check that a write is occurring and thus use a synchronisation lock of some sort. this makes things slower though, as you're obviously checking the lock and then reading instead of reading. if you're working with primitive data types (an int for example) then you can use a cpu synchronisation to speed this up dramatically.

as fr python, chances are python data is always synchronised for you by the language runtime, if it isn't then you will get the same thread read problems sooner or later. (quick google says yes, python will suffer the same problems is you're not careful)

By Andy Clarke on June 15 2022

Answers related to “why is reading not thread-safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.