CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

The motivation for threads

Threads are very useful in modern programming whenever a process has multiple 'tasks' to perform independently of the others. This is particularly true when one of the tasks may block, but allows the other tasks can proceed without blocking.

  1. Consider a programming IDE. One background thread may check syntax while a foreground thread receives user input (keystrokes), a third thread may even compile the (incomplete?) program, while a fourth thread periodically saves the code so that we may revert to earlier versions.

  2. Consider a web client (browser). One thread renders a webpage on a graphical interface, another waits for keyboard input, another for mouse input, while other threads download embedded images from different remote websites, and even download the content from the 'top' links on the current webpage - anticipating that we will soon click on them.

And the benefits

There are four core benefits to multi-threading:

  1. Responsiveness - one thread may provide rapid response while other threads are blocked or slowed down undertaking I/O or intensive calculations.

  2. Resource sharing - by default threads share common code, data, and other resources, allowing multiple tasks to be performed simultaneously in a single, smaller, address space.

  3. Efficiency - creating and managing threads (and context switching between them) is very much faster than performing the same tasks for processes. Because per-process resources are shared amongst threads, they don't need to be saved and restored when threads are suspended, scheduled, and resumed.

  4. Scalability (utilization of multiprocessor architectures) - a single threaded process can only run on one CPU (one core), no matter how many may be available. In contrast, the execution of a multi-threaded application may be split amongst available cores. Note that single threaded processes still benefit from multi-processor architectures when other processes contend for other cores.

 


CITS2002 Systems Programming, Lecture 20, p3, 9th October 2023.