spacepaste

  1.  
  2. # This is not at all my actual code, but the structure is the same
  3. import threading
  4. def main():
  5. lock = threading.Lock()
  6. threads_ran = 0
  7. def thread_fn():
  8. nonlocal threads_ran
  9. with lock:
  10. print("threads_ran is {} on entry".format(threads_ran))
  11. thread_ran += 1
  12. print("threads_ran is {} on exit".format(threads_ran))
  13. threads = []
  14. for _ in range(10):
  15. threads.append(threading.Thread(target=thread_fn))
  16. for t in threads:
  17. t.start()
  18. for t in threads:
  19. t.join()
  20. main()
  21.