linux-sysprog · advanced · ~15 min

Sum an array using multiple threads

Parallel reduction without shared mutation — pass each thread a unique partial-sum slot.

Challenge

Implement long parallel_sum(const int *a, int n, int nthreads).

Split the array into nthreads roughly-equal contiguous chunks. Each thread sums its chunk into a partial result. Main joins all threads, adds the partial sums, and returns the total.

Each thread must NOT touch a shared accumulator (no mutex needed for this design). Return -1 on argument errors (nthreads <= 0 or > 64).

Why this matters

Parallel reduction — split work into chunks, each thread sums its chunk, main adds the partial sums.

Starter code

#include <pthread.h>
long parallel_sum(const int *a, int n, int nthreads) {
    /* TODO */
    return -1;
}

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.