architect-handbook

Software Architect Handbook

View on GitHub

Mutlithreading

Mutlithreading is a programming and execution model that allows multiple threads to exist within the context of one process. These threads share the process’s resources, but are able to execute independently. Multithreading can also be applied to one process to enable parellel execution on a multiprocessing system.

The threaded programming model provides developers with a useful abstraction of concurrent execution.

Advantages

Drawbacks

Vs Multiprocessing

Multiprocessing differs in:

Implementation

Single processor systems

Systems with a single processor generally implement multithreading by time slicing.

The CPU (Central Processing Unit) switches between different software threads. This context switching generally happens very often and rapidly enough that users perceive the threads or tasks running in parallel.

On a processor or core with hardware threads separate software threads can also be executed concurrently by separate hardware threads.

Multiprocessor or multi-core systems

Multiple threads can execute in parallel, with every processor or core executing a separate thread simultaneously, and those processor/core can also have hardware threads.

OS Implementations

Process schedulers of many modern operating systems directly support both time-sliced and multiprocessor threading, and the operating system kernel allows programmers to manipulate threads by exposing required functionality through the system-call interface.

Some threading implementations are called kernel threads, whereas light-weight processes (LWP) are a specific type of kernel thread that share the same state and information. Furthermore, programs can have user-space threads when threading with timers, signals, or orther methods to interrupt their own execution, performing a sort of ad-hoc slicing.