Thread API

2026-11-18 ยท concurrency

notes / Operating Systems / 02_con

Thread Synchronisation

Thread Creation

#include <pthread.h>

int pthread_create(pthread_t *restrict thread,
                  const pthread_attr_t *_Nullable restrict attr,
                  typeof(void *(void *_Nullable)) *start_routine,
                  void *_Nullable restrict arg);

The pthread_create() function starts a new thread in the calling process. The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().

start_routine must be of the signature

void* start_routine(void* arg);

The new thread terminates in one of the following ways:

Thread attributes:
    Detach state        = PTHREAD_CREATE_JOINABLE
    Scope               = PTHREAD_SCOPE_SYSTEM
    Inherit scheduler   = PTHREAD_INHERIT_SCHED
    Scheduling policy   = SCHED_OTHER
    Scheduling priority = 0
    Guard size          = 4096 bytes
    Stack address       = 0x7f2c52e12000
    Stack size          = 0x800000 bytes

These are the various attributes available for a thread when creating it. Listed above are the default values on an x86_32 machine

Thread Completion

#include <pthread.h>

int pthread_join(pthread_t thread, void **retval);

void** retval can be used to check the return value of the completed thread

Make sure to avoid returning the address of variables declared on the thread's stack, the stack would be destroyed when the thread completes lol

Lock Creation

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int rc = pthread_mutex_init(&lock, NULL);
assert(rc == 0); // always check success!

Locking/Unlocking

pthread_mutex_lock(&lock);
x = x + 1; // or whatever your critical section is
pthread_mutex_unlock(&lock);

and some fancier ones

int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_timedlock(pthread_mutex_t *mutex,
struct timespec *abs_timeout);

trylock returns immediately, and timedlock returns after a timeout. Generally don't use these though.

Condition Variables

Useful for signalling between threads. Each condition variable always associated with a lock. What they do is

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

Thread A:

pthread_mutex_lock(&lock);
while (ready == 0)
    pthread_cond_wait(&cond, &lock);
pthread_mutex_unlock(&lock);

Thread B:

pthread_mutex_lock(&lock);
ready = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);

Atomicity

It is an essential feature that condition variables reacquire their mutex atomically. Doing this is quite hard without hardware support and is a crucial aspect of condition variables. Check out this blog about implementing CVs and our lab assignment

Barriers

These also exist and are quite cool. Basically a barrier of size N says that the threads in this barrier will only run after N threads have joined the barrier. Ensures that threads wait for their friend and only start together.

Process Synchronisation

Apparently you can share the above stuff between processes too. idk. Alternatively use either POSIX or SystemV semaphores and Shared Memory. Check out Beej's IPC chapters 10 and 11. POSIX stuff is nicer to use and usually less retarded.