Loading…
Loading…
Each of N worker threads is given one "partial" value. Every worker doubles its own value, then all workers wait for each other before any of them adds their doubled value to a shared total. Print the final total.
Why this is gradable. Concurrent code can legitimately produce different interleavings on every run, so this practice bank only includes problems whose correctness reduces to one deterministic final value or invariant — never a specific thread ordering. If your solution is correct, it will print the exact same output on every run, no matter how the scheduler interleaves your threads. Every partial is doubled and summed exactly once — the total is fixed regardless of which worker finishes its doubling first.
Input: 5 10 15 20 25
Output: 150 (doubled: 10 20 30 40 50, summed: 150)
This models a common real pattern: a parallel computation phase (each worker doubling its own value independently) that must fully complete before a second phase begins (safely combining results). A threading.Barrier(n) blocks every thread that calls .wait() until all n threads have called it — so no worker can start phase two before every worker has finished phase one.
doubled[i] = partials[i] * 2.barrier.wait().Line 1: space-separated integers (one partial value per worker)
Print the total.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.