NOTE

Thread

1. What Is a Thread? An execution flow within a process. A thread is an execution flow of a process. Multithreading means that a process has multiple execution flows. If threads are implemented at user level, they are called user space threads. If th

Operating Systems / LinuxCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is a Thread?

An execution flow within a process. A thread is an execution flow of a process. Multithreading means that a process has multiple execution flows. If threads are implemented at user level, they are called user-space threads. If they are implemented at kernel level, they are called kernel-level threads. The difference is that with the former, the kernel cannot perceive the thread’s execution. Linux uses kernel-level threads (LWP). From the kernel’s point of view, each thread is a separate process. A thread can be created using the clone call.

2. Thread Scheduling

2.1. Who Schedules Them?

The operating-system kernel.

2.2. When Does Scheduling Occur?

  • A user program enters the operating-system kernel through a system call.
  • Hardware interrupt. Hardware interrupt handlers are provided by the operating system, so when a hardware interrupt occurs, operating-system code is executed. One particularly important hardware interrupt is the clock interrupt, which is the basis on which the operating system can initiate preemptive scheduling.

2.3. How Is Scheduling Done?

  • Save context
    • Values of a set of general-purpose registers.
    • Address of the next instruction to execute.
    • Stack.

3. Thread Local Storage (TLS)

3.1. What Is It?

Thread Local Storage. Ordinary global variables are shared in multithreading; if one thread modifies one, other threads can see the modification. With TLS, each thread has its own copy. A modification by one thread does not affect other threads.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub