《操作系统》的实验代码。
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

22 řádky
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()