from threading import Thread
import threading
import time
def procesa1():
lock = threading.Lock()
lock.acquire()
for i in range(3):
print(threading.get_ident())
lock.release()
if __name__ == "__main__":
hilos = []
for i in range(5):
hilos.append(Thread(target=procesa1))
for thread in hilos:
thread.start()
The 3 print in each thread should come out in a row, shouldn't they?
A thread WINS the lock, prints its 3 outputs in a row (not interspersed with those of the other threads) and then the 3 outputs of the next thread.
This is an example of the multiple random outputs:
13108
13108
13108
12780
12780
12780
7756 // what?
6844
6844
6844
7756 // what?
7756 // what?
11936 //good
11936 //good
11936 //good, they are consecutive
The lock is supposed to be for a critical "atomic" section accessed by one thread AT A TIME.
What am I losing?