Threads and their benefits
Threads and their benefits
Recorded by Dr. Krithika L.B. Slot m2-threads
Why one process may contain many threads, how concurrency differs from parallelism, how user threads map to kernel threads, and how to create threads with Pthreads.
By the end of this session you should be able to:
A thread is a basic unit of CPU utilisation: it has a thread ID, a program counter, a register set and a stack. It shares with the other threads of the same process the code section, data section, heap, open files and signals. A traditional process has one thread; a multithreaded process has several and can do several things at once — a web browser downloads images in one thread while rendering text in another.
Single-threaded process
Multithreaded process
A long operation in one thread doesn't freeze the UI thread.
Threads share memory automatically — no IPC set-up needed.
Creating and switching threads is much cheaper than processes (no new address space).
Threads of one process can run in parallel on different cores.
Concurrency means more than one task is making progress (possible on one core by interleaving). Parallelism means more than one task executes at the same instant (needs multiple cores).
Single core (concurrent): T1T2T3T1T2…
Two cores (parallel):
core 0: T1T3T1
core 1: T2T4T2
If a fraction S of a program must run serially, the speed-up on N cores is at most
\[ \text{speedup} \le \frac{1}{S + \frac{1-S}{N}} \]
Example: 25% serial (S = 0.25), N = 2 → 1/(0.25 + 0.375) = 1.6×; N = 4 → 1/(0.25 + 0.1875) = 2.29×; N → ∞ → at most 1/0.25 = 4×. The serial part dominates as cores grow.
A multithreading model describes how user threads are mapped to kernel threads.
Many user threads → one kernel thread. Thread management is done by a user-level library, so it is efficient.
Problem: if one thread makes a blocking system call, the whole process blocks; and only one thread can be in the kernel at a time, so no parallelism on multicore. Example: early Solaris Green Threads, GNU Portable Threads.
Each user thread → its own kernel thread.
More concurrency: a blocking call blocks only that thread; threads run in parallel on multiple cores. Cost: creating a user thread creates a kernel thread, so systems may limit the number. Used by Linux and Windows (this is what you get with Pthreads on Linux).
Many user threads multiplexed onto a smaller or equal number of kernel threads.
Developers create as many user threads as they like, and the kernel threads still run in parallel; a blocking call lets another kernel thread be scheduled. Harder to implement — few systems use it today (Go's goroutine scheduler works this way in user space).
A variation of many-to-many that also allows a user thread to be bound to a kernel thread (older IRIX, HP-UX, Solaris 8).
A thread library provides the API for creating and managing threads. The three main ones are POSIX Pthreads, Windows threads and Java threads. Pthreads is a specification (IEEE 1003.1c); Linux implements it with one-to-one kernel threads.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
long sum; /* shared by all threads (data section) */
void *runner(void *param) /* the thread starts here */
{
long upper = atol(param);
sum = 0;
for (long i = 1; i <= upper; i++)
sum += i;
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t tid; /* thread identifier */
pthread_attr_t attr; /* thread attributes */
if (argc != 2) { fprintf(stderr, "usage: %s <n>\n", argv[0]); return 1; }
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, argv[1]); /* create */
pthread_join(tid, NULL); /* wait */
printf("sum = %ld\n", sum);
return 0;
}$ gcc sum_thread.c -o sum_thread -pthread
$ ./sum_thread 10
sum = 55exec() immediately, that is all it needs.sum, two threads updating it at once can race. Synchronization is Module 3.Threads and their benefits
Recorded by Dr. Krithika L.B. Slot m2-threads
Multithreading models and Pthreads
Recorded by Dr. Krithika L.B. Slot m2-thread-models
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0Shared: heap, global variables, open files. Private: stack and registers (and the program counter).
All user threads are mapped to a single kernel thread; when it blocks in the kernel, there is no other kernel thread to run the remaining user threads.
1 / (0.4 + 0.6/8) = 1 / 0.475 ≈ 2.1×.
Concurrency yes (interleaving threads); parallelism no — it needs several cores executing at the same instant.
It blocks the caller until the specified thread terminates — the thread analogue of wait().
Processes and threads all compete for the CPU. Which one should run next, and for how long? That is CPU scheduling.
Continue to 2.4 — Scheduling criteria and non-preemptive algorithms →