Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesPython FoundationsModule 6 · Concurrency and parallelismthreading.Lock — predict the final counterpredict136 / 174
+100 XP
Task
📝 **Task:** Predict the final value of \`counter\` after 5 threads each run \`bump()\` once, with the lock protecting the increment. 📋 Edit the starter code below. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

import threading

counter = 0
lock = threading.Lock()

def bump():
    global counter
    with lock:
        counter += 1

threads = [threading.Thread(target=bump) for _ in range(5)]
for t in threads: t.start()
for t in threads: t.join()
print(counter)

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…