Thread, clone, futex and CMPXCHG

This is a page compiled from my notes I’ve taken on thread(pthread)-related topics sporadically over time. It’s my effort to sort out understanding and remember by putting them together.

pthread Internals

pthreads calls clone(). It creates a task_struct.

Notes:

  • pthread (POSIX threads) is part of libc. Most of Linux have it implemented by glibc (GNU C Library).

  • Implemented in user space, and interacts with kernel through system calls like clone() and futex.

  • fork() (libc) also calls clone() but with different params.

Notes: libc vs kernel system call

Standard interface for kernel system calls is libc for portability and safety. Technically man 2 [name] describes kernel system call and man 3 [name] describes libc function, however even man 2 displays lib in library section.

To find out if a libc function is just a wrapper or has more logics on top of system calls, we need to find out internals / knowledge of the function. But if a function only exists in man 3 then it’s obvious that the function has custom logics wrapping system calls in libc.

Kernel Perspective

Both processes and threads are represented as task_struct in kernel.

Controlling Kernel Schedule

User-level Threading

Languages without m:n

Languages like Java or Rust provide provide an interface to kernel threads with 1:1 mapping as a language feature instead of providing m:n or green thread model.

In those language, m:n, green thread or fiber model of multithreading are typically implemented as libraries outside of language features / designs.

Synchronization Mechanisms

With multiple threads, contentions need to be handled. pthread provides synchronized primitives such as mutex, condition variable and semaphore.

Futex

Stands for “fast userspace mutex.” Only when contention occurs futex interacts with the kernel.

Notes:

It might seem like futex is not part of the kernel at first glance as it is designed to minimize kernel involvement in typical use cases, but it is a kernel construct.