tags:

views:

204

answers:

2

Let's say I am using a signal handler for handling an interval timer.

def _aHandler(signum, _):
  global SomeGlobalVariable
  SomeGlobalVariable=True

Can I set SomeGlobalVariable without worrying that, in an unlikely scenario that whilst setting SomeGlobalVariable (i.e. the Python VM was executing bytecode to set the variable), that the assignment within the signal handler will break something? (i.e. meta-stable state)

Update: I am specifically interested in the case where a "compound assignment" is made outside of the handler.

(maybe I am thinking too "low level" and this is all taken care of in Python... coming from an Embedded Systems background, I have these sorts of impulses from time to time)

+5  A: 

Simple assignment to simple variables is "atomic" AKA threadsafe (compound assignments such as += or assignments to items or attributes of objects need not be, but your example is a simple assignment to a simple, albeit global, variable, thus safe).

Alex Martelli
but what about "compound assignment" outside of the handler?
jldupont
If the handler does (e.g.) `gvar = 3`, `gvar` is initially 7, and the code outside the handler does (e.g.) `gvar += 2`, then `gvar` could end up as being either 3, 5, or 9, depending on how the operations end up interleaved. That's technically "safe" (meaning, the process won't crash;-) but unlikely to be semantically OK.
Alex Martelli
@Alex: Thanks again!
jldupont
A: 

Compound assignment involves three steps: read-update-write. This is a race condition if another thread is run and writes a new value to the location after the read happens, but before the write. In this case a stale value is being updated and written back, which will clobber whatever new value was written by the other thread. In Python anything that involves the execution of a single byte code SHOULD be atomic, but compound assignment does not fit this criteria. Use a lock.

Eloff
In the situation depicted above, I have only a single thread of execution. Furthermore, it is not like I can "delay" the execution of the signal handler. Of course I can have recourse to a thread-safe queue if the opinion of the brain trust of SO prescribes so.
jldupont
If you have a single thread, where does the handler run? If it's on the same thread, then nothing can alter state while it's running in the first place.
Max Shawabkeh
@Max S. : you sure? Look at @Alex Martelli 's answer.
jldupont
jldupont, Pretty sure. Alex's answer is excellent, but it applies only when you have multiple threads of control; there's no race conditions when there's only one racer.
Max Shawabkeh
@Max S.: but in this case I believe we have another racer: the "signal dispatching agent".... I don't see any polling for signals. Of course, I might be totally out-for-lunch: I'd like to see the relevant documentation to support your case, please.
jldupont
@jldupont: Why on earth do you care about atomicity if you have only one thread? By definition you cannot have a race condition with only one thread/process. Forget compound assignment, every single function call is atomic in that case, no matter how much you do in the function, including your entry point.
Eloff