Technical skill

Linux thread concurency

Two workers need a rule for shared data

Imagine two workers adding finished jobs to the same counter. Each reads 4, adds one, then writes 5. The program should record 6, but the two writes overlap and one is lost. This can happen when two threads in a program update shared data at the same time.

A process is a running program. A thread is one path of work inside it. A program can have several threads, and threads in one process share its memory and open file descriptors, small numbers used to refer to files or devices that are already open. Each thread keeps its own execution state and stack, a private area used for function calls. On Linux, POSIX thread functions, commonly called pthreads, create and coordinate these threads. pthread_create() starts one.

Concurrency means tasks make progress during overlapping periods. The system can switch between threads as they run or wait. Parallelism means two tasks run at the same instant on different CPU cores. Threads can be concurrent on one core. After a thread starts, the scheduler generally does not promise which thread runs first.

Divide work while protecting shared state

For a file-hashing program, the main thread can start a fixed number of workers and give them a shared queue, a list of files waiting to be processed. A mutex is a lock that lets one thread at a time change the queue. A worker locks the queue, removes one file, and unlocks it. The worker then reads the file and calculates its hash, a short fingerprint of its contents. It locks again only to record the result. Because file reading happens outside the locked section, one slow disk operation does not keep every other worker waiting.

A condition variable lets an idle worker sleep until there may be a file to process. The queue's state is the condition the worker checks. It checks that condition in a loop while holding the mutex, then waits. Waiting releases the mutex during sleep and reacquires it before returning. After a wake-up, the worker checks again because another thread may have taken the item first.

Keep threads and their data alive in order

New pthreads are joinable by default, which means another thread can wait for them to finish and collect a result. The coordinating thread does this with pthread_join(). Once the join finishes, the worker has stopped using its arguments and shared data. A detached thread frees its thread resources when it exits, but another thread cannot join it to collect a result.

A data race happens when threads access the same data at the same time without a safe rule. Deadlock can occur when threads wait on locks in a cycle. A thread can also starve by waiting too long, or misuse data after it has been freed. These problems can break a threaded program. Creating a new thread for every task can exhaust system resources; a bounded worker pool caps the number. During review, list each shared variable, the rule that protects it, and how long each thread may use it. Test with several workers and check pthread return codes, the numbers the functions return to report success or failure. On Linux, ps -L lists a process's threads; a debugger or race detector can help find failures that depend on timing.

Interactive exercises

Materials

Skillsets
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 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