Technical skill

Linux: threads

How a program can do several jobs

Imagine a music player that must respond to a pause button while it loads a song list. It can handle those jobs at the same time: one path responds to the controls while another loads the list. In a program, each path of work is called a thread; the running program is a process.

Threads in a process share memory, including global variables and heap data, and they share open file descriptors. Each thread has its own registers, stack, and signal mask. A stack is the private memory used for function calls and local values.

Shared memory makes it easy for threads to exchange data, but it must be protected. Imagine two threads adding one to the same counter at the same time: they can both read the old value and then write back the same new value, losing one update. A mutex or another synchronization primitive protects a critical section, the code that uses shared data. Every access path to that data must follow the same locking rule.

Start and wait for a thread

The POSIX threads API, usually called pthreads, lets a C program create and coordinate threads. A program includes <pthread.h>, supplies a start routine, and calls pthread_create(). A joinable thread can be collected with pthread_join():

pthread_t worker;
int error = pthread_create(&worker, NULL, worker_function, argument);

if (error == 0) {
    pthread_join(worker, NULL);
}

This fragment assumes worker_function and argument have been declared. pthread_t is an opaque library identifier; do not assume it is the same as a process ID or thread ID. New threads do not necessarily run before the caller continues, so use synchronization if the order matters. The second argument to pthread_join() is NULL here, so the caller discards the thread's completion value. A complete program should also check the join result.

Compile pthreads code with the compiler's thread option, commonly cc -pthread. Many pthread functions return an error number directly instead of setting errno, the error value used by many other C library calls. A join waits for a joinable thread to terminate. A detached thread releases its resources automatically when it terminates but cannot be joined.

Identify threads on Linux

On Linux with the Native POSIX Thread Library, threads in one process share the process ID returned by getpid(). Each kernel thread also has its own thread ID (TID). Linux-specific gettid() returns that ID. The POSIX pthread_t is an opaque library identifier and should not be assumed to equal the kernel TID.

Inspect a process's threads with:

ps -T -p 1234

The rows list threads belonging to the process; replace 1234 with its current process ID. Exact output depends on the ps implementation and options.

Threads help when work can overlap I/O and computation or needs shared memory. They do not guarantee faster work: synchronization overhead, contention, or tasks that cannot run in parallel can limit gains. Separate processes may fit better when isolation or independent resource ownership matters. Decide who owns shared objects and how threads start, stop, report errors, and release resources.

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