Skip to main content

Threaded Sum Pipeline

Challenge on lesson: Checkpoint: The Pipeline

The checkpoint: a two-stage pipeline. One producer thread pushes 0..n-1 into the bounded queue; one consumer thread pops everything and sums. The boilerplate wires the queue (a correct reference implementation), the thread entry wrappers, and a run_pipeline harness; YOU implement only the two worker bodies: ``c /* producer body: push 0..n-1 into q, then signal completion by incrementing producers_done under done_m with cnd_broadcast. 0 ok, -1 bad args. */ int producer_body(BQ *q, int n); /* consumer body: pop exactly expected items (bq_ref_pop BLOCKS until data exists — that blocking IS the synchronization), summing them into *sum. 0 ok, -1 bad args. */ int consumer_body(BQ *q, int expected, long *sum); `` The harness starts both threads, joins them, and reports *total — it must equal the exact sum of 0..n-1.

Difficulty: intermediate

Back to lesson: Checkpoint: The Pipeline