The task is to organize a state variable in which one trad writes, another reads. Lord Sishniki immediately indicated the need for a lock when writing, reading in a state variable. Did not believe. I try:

import thread status = '' def first(): global status mes = ['0000000000','1111111111'] while 1: status = mes[0] status = mes[1] def second(): mes = ['0000000000','1111111111'] while 1: if status not in mes: print('Ahtung!!!!!') thread.start_new_thread(first,()) thread.start_new_thread(second,()) 

Still runs without conflict. Who is right? Does python or the thread module destroy this situation? PS Naturally, I imagine that such flows and that such a problem should exist ...

  • > Still runs without conflict. What conflicts did you expect to see here? - fogbit
  • The first thread will have time to write only '11', since the second thread will start running. Hence the string will not be complete. It's all from crafty C. - moden
  • Then it is better to try 100 (or 1000) streams, not 2. 1 let him write and 99 check the occurrence. - fogbit
  • I read about GIL. The desire to check disappeared by itself ... - moden

1 answer 1

In fact, in python, there is such a thing as GIL, a global interpreter lock. In order for a thread to perform operations, you need to capture GIL, so more than one stream cannot be executed at a time. True, I don’t really understand at what point GIL can be intercepted, but most likely all standard data structures are designed to be consistent when using such a lock. See more here.

  • Thank you, the key word is GIL. - moden