1import threading
2
3class DownloadManager:
4 def __init__(self):
5 self.completed = 0
6
7 def download(self, url):
8 # ... download logic ...
9 self.completed = self.completed + 1
10
11manager = DownloadManager()
12threads = [threading.Thread(target=manager.download, args=(url,))
13 for url in urls]
14for t in threads: t.start()
15for t in threads: t.join()
16# self.completed often less than len(urls)
no lines flagged
#019PracticeMedium20 min · 120 XP
Thread Race Condition on Shared Counter
A multithreaded download manager counts completed tasks incorrectly because the counter is not thread-safe.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.