thread_lock2.py 465 B

123456789101112131415161718192021222324
  1. # test _thread lock objects with multiple threads
  2. #
  3. # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
  4. try:
  5. import utime as time
  6. except ImportError:
  7. import time
  8. import _thread
  9. lock = _thread.allocate_lock()
  10. def thread_entry():
  11. lock.acquire()
  12. print('have it')
  13. lock.release()
  14. # spawn the threads
  15. for i in range(4):
  16. _thread.start_new_thread(thread_entry, ())
  17. # wait for threads to finish
  18. time.sleep(1)
  19. print('done')