Sat. Jan 21st, 2023

Multiprocessing and Multithreading

Multithreading

A process (what a program becomes when it is being run) can be composed of one or more threads, all of which run on the same processor.

This means that code in all threads are able to access the same data and results of calculations (remember that in a CPU cache isn’t always shared across all CPUs).

Having code in separate threads allows them to run concurrently (at the same time) without one having to wait for a task tom complete in another. A simple example is a Windows Forms application. When the application is launched, it uses a single thread, and that thread must drive both the UI and the program logic. This means that if a long processing task is begun, the interface will freeze (perhaps causing the window to fade and report ‘not responding’) until processing completes. If the processing is launched in a separate thread, the interface does not freeze, because its thread isn’t affected by the processing.

Concurrency introduces problems into software design. Because multiple threads can access the same information, sometimes it is difficult to predict whether you will obtain a result before or after an operation has completed. To avoid this, it is possible to issue ‘lock’ commands to force threads to wait to access information until processing is complete. However, care must be taken: imagine a scenario where two threads access the same information, and each thread is able to lock that information. It is possible to reach a condition where neither thread can access the data, and program execution halts indefinitely. This is known as a race condition.

Multiprocessing

Whereas multithreading allows a single process to be split into threads which run concurrently, multiprocessing refers to running multiple processes simultaneously.

Multiprocessing does not require multiple CPUs; rather, the CPU switches rapidly between different processes in order to give the appearance that multiple processes are running at the same time. However, this does not mean that a multiprocessing system can’t have more than one CPU!

Comparison