Multi-Threading (1), Concept - What, Why

This is a series of articles about Multi-threading.

This is the first article about Multi-Threading.

Introduction

The content of the article:

  • A - Multi-Threading: What
    • A-1 - What is Program, Job, Task, Process and Thread
    • A-2 - Computer vs. Process
    • A-3 - Program (Application) vs. Process
    • A-4 - Process vs. Thread
    • A-5 - Concurrency and Parallelism
  • B - Multi-Threading: Why? --- The Benefits
    • Responsiveness
    • Resource Sharing
    • Economy
    • Scalability

A - What is Multi-Threading

A-1 - What is Program, Job, Task, Process, and Thread

  • Program: a code collection stored in the computer, can be divided by
    • Logic
      • Job: a work that needs to be done; 
      • Task: a piece of work that needs to be done
    • Physical
      • Process: the execution of a program
      • Thread: a unit of execution within a process.

A-2 - Computer vs. Process

Single task (Process) computer

 

Sharp PC-1500

The Sharp PC-1500 was a pocket computer produced by Sharp between 1981 and 1985. It is absolutely a single task or process.

IBM: terminal, single task terminal

It is a single task terminal, although in fact, the IBM frame, the terminal linked, is a multiple shared system.

Unix multiple windows, multiple tasks

Multiple Task Moden computers, such as PC, we can run MS Word and Excel at the same time for sure.

A-3 - Program (Application) vs. Process

A program or application usually has single process, while some of them do have multiple processes, such as Google Chrome [ref] [ref]:

Here, we open three instances of Windows Explorer, only one instance of Google Chrome as above, 

while in the task manager, we can see there are three processes for Windows Explorer for the three opened instances, however, there are 10 processes for that single instance of Google Chrome.

i.e., Google Chrome is a multiple process application.

A-4 - Process vs. Thread

A thread is the unit of execution within a process. A process can have anywhere from one thread to many.

When a process starts, it receives an assignment of memory and other computing resources. Each thread in the process shares that memory and resources. With single-threaded processes, the process contains one thread.

In multi-threaded processes, the process contains more than one thread, and the process is accomplishing a number of things at the same time (to be more accurate, we should say "virtually" at the same time - you can read more about that in the section below on concurrency).

Both memory stack and the heap are available to a thread or process. While each thread will have its own stack, all the threads in a process will share the heap.

Some people call threads lightweight processes because they have their own stack but can access shared data. Since threads share the same address space as the process and other threads within the process, it is easy to communicate between the threads. The disadvantage is that one malfunctioning thread in a process can impact the viability of the process itself.

A-5 - Concurrency and Parallelism

A question: whether processes or threads can run at the same time. The answer is: It depends. On a system with multiple processors or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel. On a single processor, though, it is not possible to have processes or threads truly executing at the same time. In this case, the CPU is shared among running processes or threads using a process scheduling algorithm that divides the CPU’s time and yields the illusion of parallel execution. The time given to each task is called a “time slice.” The switching back and forth between tasks happens so fast it is usually not perceptible. The terms, “parallelism” (genuine simultaneous execution) and “concurrency” (interleaving of processes in time to give the appearance of simultaneous execution), distinguish between the two types of real or approximate simultaneous operation.

B - Why is Multi-Threading - The benefits of Multi-Threading

Multithreading allows the execution of multiple parts of a program at the same time. These parts are known as threads and are lightweight processes available within the process. So multithreading leads to maximum utilization of the CPU by multitasking.

Some of the benefits of multithreaded programming are given as follows -

Multithreaded Programming

  • Resource Sharing

    All the threads of a process share its resources such as memory, data, files, etc. A single application can have different threads within the same address space using resource sharing.

  • Responsiveness

    Program responsiveness allows a program to run even if part of it is blocked using multithreading. This can also be done if the process is performing a lengthy operation. For example - A web browser with multithreading can use one thread for user contact and another for image loading at the same time.

  • Scalability - Utilization of Multiprocessor Architecture

    In a multiprocessor architecture, each thread can run on a different processor in parallel using multithreading. This increases the concurrency of the system. This is in direct contrast to a single processor system, where only one process or thread can run on a processor at a time.

  • Economy

    It is more economical to use threads as they share the process resources. Comparatively, it is more expensive and time-consuming to create processes as they require more memory and resources. The overhead for process creation and management is much higher than thread creation and management.

References

Process and Thread

Benefits