Skip to main content
Engineering LibreTexts

2.3.2: Difference between Multiprogramming, multitasking, multithreading and multiprocessing (continued)

  • Page ID
  • Multithreading

    A thread is a basic unit of CPU utilization. Multithreading is an execution model that allows a single process to have multiple code segments (i.e., threads) running concurrently within the “context” of that process.
    e.g. VLC media player, where one thread is used for opening the VLC media player, one thread for playing a particular song and another thread for adding new songs to the playlist.

    Multithreading is the ability of a process to manage its use by more than one user at a time and to manage multiple requests by the same user without having to have multiple copies of the program.

    Multithreading system examples

    Example 1

    • Say there is a web server which processes client requests. Now if it executes as a single threaded process, then it will not be able to process multiple requests at a time. First one client will make its request and finish its execution and only then, the server will be able to process another client request. This is quite inefficient, time consuming and tiring task. To avoid this, we can take advantage of multithreading.
    • Now, whenever a new client request comes in, the web server simply creates a new thread for processing this request and resumes its execution to process more client requests. So the web server has the task of listening to new client requests and creating threads for each individual request. Each newly created thread processes one client request, thus reducing the burden on web server.

    Example 2

    • We can think of threads as child processes that share the parent process resources but execute independently. Take the case of a GUI. Say we are performing a calculation on the GUI (which is taking very long time to finish). Now we can not interact with the rest of the GUI until this command finishes its execution. To be able to interact with the rest of the GUI, this calculation should be assigned to a separate thread. So at this point of time, 2 threads will be executing i.e. one for calculation, and one for the rest of the GUI. Hence here in a single process, we used multiple threads for multiple functionality.

    The image helps to describe the VLC player example:

    Multithreading is a single app that is capable of performing multiple tasks at the same time. The app creates a separate thread for each task. Once the new thread is running the app can return to performing other tasks without waiting for that thread to complete.
    Figure \(\PageIndex{1}\): Example of Multithreading. ("Multithreading" by Darshan L.Geeks for Geeks is licensed under CC BY-SA 4.0)

    Advantages of multithreading 

    • Benefits of multithreading include increased responsiveness. Since there are multiple threads in a program, so if one thread is taking too long to execute or if it gets blocked, the rest of the threads keep executing without any problem. Thus the whole program remains responsive to the user by means of remaining threads.
    • Another advantage of multithreading is that it is less costly. Creating brand new processes and allocating resources is a time consuming task, but since threads share resources of the parent process, creating threads and switching between them is comparatively easy. Hence multithreading is the need of modern Operating Systems.

    Adapted from:
    "Difference between Multiprogramming, multitasking, multithreading and multiprocessing" by Darshan L.Geeks for Geeks is licensed under CC BY-SA 4.0