What is multithreading in programming? How does it work?

The main feature of a thread is the execution of a program on its own virtual machine (CPU).In the past, multitasking was called multiprocessing, which became multiprocessing in modern operating systems.As Patrick Krusenotto correctly describes, MultiThreading is a simple form of MultiProcessing.Processes have separate memory areas, while threads share a memory area.I also used the word task.task) as a synonym for thread or process.

The main idea of the MultiTasking is to carry out the sequence of various tasks, which in principle have nothing to do with each other, simultaneously – i.e. in parallel. In order to achieve this, one basically needs a separate CPU for each task, which executes the sequential program for this task step by step.Tasks often contain in/out commands that need to be waited for. During this wait, a CPU can perform another task. Although today’s computers often have 2, 4 or 8 CPU’s, these are not enough to often operate 30 – 300 tasks at the same time when modern operating systems run.On my Linux machine, 258 processes are running at the same time. (ps -ef | wc -l) The ps command does not count threads in the different processes.

The term virtual CPU was introduced for the implementation of MultiTasking in OS/360 (IBM OPERATING SYSTEM/360, described in “Programming Systems and Languages”, ed Saul Rosen, 1966, p. 639).Today, the principle of multitasking is used in almost unchanged form in all operating systems.

In OS/360, each virtual CPU is called a task.Each task has a Task Control Block (TCB) – also called context and a program.

The TCB stores all CPU registers as well as some important memory cells.

At any time, a task is in one of three states:

  1. ACTIVE – a task is active when a CPU executes statements in a program that are part of the task or shared with other tasks and is shared with data that also belongs to the task or with other tasks.

To mark this state, the address of the currently active TCB is stored in a known location. In addition, the registers in the TCB, including the program counter (PC), are transferred to a physical CPU when the task is activated, which then continues to run the program.

  • READY – A task is in this state when it is ready to use a CPU but is not active because higher-priority tasks or system programs use all physical CPU’s.
  • The registers of this task are stored in your TCB.

  • SUSPENDED – A task is in a suspend state when it has to wait for an event to occur.
  • Such an event can be the completion of an input/output operation, a real-time clock clock, or the execution of one of the synchronizing macro commands in another task that can reactivate a suspended task (e.g. MUTEX).

    The various tasks are either suspended or placed in a READY state when they are waiting for an event, or at regular intervals.In both cases, the CPU registers, including the PC’s pointing to the next command of the task program, are stored in the TCB of the task.This quick switch from many virtual CPU’s that run regularly in a physical CPU gives the impression that many tasks run simultaneously and in parallel.The same applies to threads and processes.

    For more information on this topic, see my blog article Real Time Operating System “OSCAR”.

    Leave a Reply