linux-sysprog · beginner · ~10 min

Create one thread that returns a value

pthread_create → pthread_join → cast return value.

Challenge

Implement int run_one_thread(void) that:

  1. Spawns one pthread running a function that returns (void *)(intptr_t)42.
  2. Joins the thread.
  3. Returns the value the thread returned (cast back to int).

Return -1 on any failure (pthread_create or pthread_join).

Why this matters

The smallest possible pthread program — spawn one thread, join it, read its return value.

Starter code

#include <pthread.h>
#include <stdint.h>
int run_one_thread(void) {
    /* TODO */
    return -1;
}

Background lessons

Up next

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