Are variable reads atomic?

One thread has exclusive write access to a shared variable:

 requests_issued += 1

Another periodically reads the variable:

print("Approximately number of requests", requests_issued)

Does this require a lock or is the variable read atomic, always giving a value either before or after the first thread makes an update?

I am not worried about the race condition, any recent value will do. I would care if a mid-update read were to fail by raising an exception, segfaulting or returning something other than an integer.

ints are immutable, so that does requests_issued = requests_issued + 1. As you’re not bothered about whether you get the old or new value, it’s perfectly fine. No exception, no segfault.

1 Like

MRAB: Thank you