Python multithreading and Global Interpreter Lock -


i came across gil present in python according single thread can execute @ time , multithreading can not utilize cores.

now in 1 of projects, used multithreading , lots of locks , semaphores here question can achieve same thing if don't use locks , semaphores? i.e if remove concurrency logics project.

edit: want know is, whether it's possible attain same functionality if remove concurrency logics, know gil , prevents threads use cores , 1 thread run @ time.

the global interpreter lock ensures 1 thread executing byte code @ once. execution interrupted @ time.

consider simple function might intended atomically store related values attributes on instance x

def f(x, a, b):     x.a, x.b = a, b 

here disassembly bytecode

          0 load_fast                1 (a)           3 load_fast                2 (b)           6 rot_two           7 load_fast                0 (x)          10 store_attr               0 (a)          13 load_fast                0 (x)          16 store_attr               1 (b)          19 load_const               0 (none)          22 return_value 

suppose x not protected mutex. thread executing f(x, 1, 2) can interrupted between storing a (at 10) , storing b (at 16). interrupting thread see x in inconsistent state.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -