multithreading - locking global variables under the Threading module of python -


let suppose have 2 threads , single global variable in python code threading module. in code, thread-1 modifies global variable's value, whereas, thread-2 reads value of global variable , perform task depending on value.

in situation, need protect access global variable lock()? in c, mutex must used under such condition. however, python gil? still case? lock() still required?

assigning object value global variable atomic operation in python. other threads cannot read variable incorrectly reading while it's being assigned. gil guarantees in c implementation of python, other implementations can , make same guarantee in different ways.

if global variable mutable object, list, , modifying object, depends on method use. methods on builtin objects lists atomic.

i can't sure don't need lock, though, without knowing more details purpose of variable , how using it. why thread-2 need change behavior based on value, , ok if thread-1 changes value after thread-2 has made decision? if it's not ok, may still need lock.

(an analogous situation in c - assigning value pointer variable - atomic under normal circumstances, though pointer assignments can reordered. can use in circumstances avoid locks in c.)


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -