《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
552 B

  1. import threading
  2. import time
  3. counter = 0
  4. mutex = threading.Lock()
  5. class MyThread(threading.Thread):
  6. def __init__(self):
  7. threading.Thread.__init__(self)
  8. def run(self):
  9. global counter, mutex
  10. time.sleep(1);
  11. if mutex.acquire():
  12. counter += 1
  13. print "I am %s, set counter:%s" % (self.name, counter)
  14. mutex.release()
  15. if __name__ == "__main__":
  16. for i in range(0, 100):
  17. my_thread = MyThread()
  18. my_thread.start()