Created
January 30, 2016 09:59
-
-
Save TakesxiSximada/1ec4e3368af8cb6a983b to your computer and use it in GitHub Desktop.
スレッドセーフじゃない例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import time | |
from threading import Thread | |
count = 0 | |
class CountUpThread(Thread): | |
def run(self): | |
global count | |
for ii in range(1000): | |
value = count | |
time.sleep(0.001) | |
value += 1 | |
count = value | |
children = [CountUpThread() for ii in range(100)] | |
for child in children: | |
child.start() | |
for child in children: | |
child.join() | |
print(count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment