地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

【Python】スレッドセーフでないコード

import threading

count = 0

def increment():
    global count
    for _ in range(100000):
        count += 1

threads = [threading.Thread(target=increment) for _ in range(2)]
[t.start() for t in threads]
[t.join() for t in threads]

print("Expected:", 200000)
print("Actual:", count)