andersch.dev

<2024-05-29 Wed>

Multithreading

Multithreading describes the concurrent running of multiple threads of execution in a CPU. Each thread runs within a process and maintains its own execution context (instruction pointer and state of registers) while sharing the process's memory space.

It introduces challenges like thread synchronization, race conditions, and deadlocks. Coordination mechanisms such as mutexes, semaphores, and atomic operations can be used to combat these problems.

Basic Thread API

int       thread_create(thread_t **thread, void *(*start_routine)(void *), void *arg);
int       thread_join(thread_t *thread, void **retval);
int       thread_detach(thread_t *thread);
void      thread_exit(void *retval);
thread_t* thread_self(void); // obtain ptr to own thread

Resources