linux-sysprog · beginner · ~10 min

Spawn N threads, join all

Two parallel loops: spawn then join. Track all thread IDs.

Challenge

Implement int run_n_threads(int n) that spawns n threads (each one just immediately returns), joins them all in order, and returns n on success. Return -1 if any pthread_create or pthread_join fails.

Why this matters

Real programs spawn a pool of threads, not one. Practice the spawn-loop + join-loop pattern.

Starter code

#include <pthread.h>
int run_n_threads(int n) {
    /* TODO */
    return -1;
}

Background lessons

Up next

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