linux-sysprog · advanced · ~15 min

Parallel array sum

Split work across pthreads and combine with join.

Challenge

Implement long parallel_sum(const int *a, int n) that sums the array using two POSIX threads (each summing half), joins them, and returns the total.

Starter code

#include <pthread.h>

long parallel_sum(const int *a, int n) {
    /* TODO */
    return 0;
}

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