Four threads update one counter
QuizMediumPython onlyAsked in: Accenture, Capgemini, Cognizant, Infosys, TCS
Four threads each run count = count + step() 200,000 times on a global count (step() returns 1). Which statements are true for CPython 3.12? Pick all that apply.
- The final count can be well below 800,000
- The GIL guarantees exactly 800,000
- Wrapping the update in
with lock:using one sharedthreading.Lockgives exactly 800,000 - Each thread adding to its own local total and returning it avoids the problem
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.