linux-sysprog · advanced · ~15 min

Return a value from a thread

Collect a thread's result via join's value pointer.

Challenge

Implement long square_in_thread(long v) that runs a thread which returns v*v through the thread return value, retrieves it with pthread_join, and returns it.

Starter code

#include <pthread.h>

long square_in_thread(long v) {
    /* TODO */
    return 0;
}

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