Technical skill

Linux Multithreads

Subject: .../LINUX

A music player may keep a song playing while it downloads album art and waits for you to press a button. One running application can divide these jobs among threads, which are separate paths of work inside the application. The operating system decides when each path runs. A process is the running program that owns the work; threads inside it can share information, so one part of the app can use data prepared by another.

Threads share the process's virtual address space, global variables, heap, and open file descriptors. Each has its own stack, thread ID, signal mask, and execution state. Shared data makes coordination possible, but it also creates race conditions when multiple threads update the same object without synchronization. A race condition is a bug where the result depends on which thread reaches shared data first.

POSIX Threads, or pthreads, are a C API for creating and coordinating threads. On modern Linux systems, the Native POSIX Thread Library (NPTL) maps POSIX threads to kernel tasks in one thread group. pthread_t is an opaque identifier for the thread library; it is not the numeric Linux thread ID (TID). getpid() returns the process ID shared by the thread group, while gettid() returns the calling thread's Linux TID.

A thread executes a start routine concurrently with other threads. pthread_create() starts one, and pthread_join() waits for a joinable thread to finish and retrieves its result. When threads write shared mutable state, use a synchronization method. In this example, a mutex protects each update to total:

#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static long total;

static void *worker(void *arg) {
    long iterations = *(long *)arg;
    for (long i = 0; i < iterations; ++i) {
        pthread_mutex_lock(&lock);
        ++total;
        pthread_mutex_unlock(&lock);
    }
    return NULL;
}

A complete program still needs to create threads with pthread_create, pass each a valid argument whose lifetime covers the call, and use pthread_join before reading total. Compile with the toolchain's -pthread option, as in cc -pthread example.c -o example. Locking each increment is clear for a demonstration but can be expensive; where the result allows it, compute locally and combine values under a shorter lock.

Threads do not guarantee faster execution. CPU-bound work competes for cores, and I/O-bound work may spend most of its time waiting. Lock contention, cache behavior, and memory bandwidth can outweigh the benefit of parallel execution. Measure under representative conditions.

Linux exposes per-thread information below /proc/PID/task/TID, where each TID directory represents one thread in the process. ps -T -p 1234 lists threads for a process; procps-ng top -H -p 1234 displays individual threads. Permissions, procfs mount options, and namespaces affect what is visible.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful