Threads and Concurrency
Engineering Concurrency Models
Engineering concurrency models describe how multiple activities make progress and coordinate their work. You encounter them when software acquires sensor readings, processes independent simulation cases, or handles incoming measurements while saving earlier results. Choosing a model involves three questions: which activities can overlap, how they exchange information, and what prevents an incorrect result when their execution order changes.
Concurrency and execution models
Concurrency means that multiple activities make progress during overlapping periods. They need not execute at the same instant. On one processor core, a scheduler can alternate between tasks. Parallel execution occurs when tasks actually execute simultaneously, such as on different cores. Consequently, a concurrent program may remain correct on one core even though it gains little computational speed.
A thread is a sequence of execution within a process. Threads in the same process normally share its memory, although each has its own execution state. Other models organize concurrency around separate processes, messages, or tasks that suspend while waiting.
Model | How activities communicate | Main coordination concern |
|---|---|---|
Shared-memory threads | Read and write shared objects | Protect conflicting access and establish visibility |
Message passing | Send values through communication channels | Match messages and manage waiting or buffering |
Asynchronous tasks | Suspend at supported waiting operations and resume later | Respect dependencies and avoid blocking the task scheduler |
These models can be combined. For example, asynchronous tasks might collect measurements while worker threads perform calculations. Asynchronous execution is especially useful for overlapping waits; it does not by itself make processor-intensive calculations execute in parallel. Messages reduce direct sharing, but a full communication buffer can still make a sender wait.
Correctness depends on coordination
A shared update can contain several operations even when its source code occupies one line. Incrementing a counter may require reading its current value, adding one, and writing the result. If two threads both read zero before either writes, each may write one. Two completed increments have then produced a count of one.
A critical section is a region that accesses a shared resource and requires coordinated access. One common solution is mutual exclusion: every conflicting access uses the same mutex, and the mutex protects the complete update. Protecting only the final write leaves the earlier read exposed. A suitable atomic increment can also protect this particular operation, but separate atomic operations do not automatically protect a larger multi-operation invariant.
Correctness also includes progress. Deadlock occurs when activities wait indefinitely for conditions that their own waiting prevents. A consistent order for acquiring multiple locks can prevent circular lock waiting. Bounded queues introduce another dependency: a producer may need a consumer to remove an item before it can continue. Trace both data dependencies and waiting conditions.
Estimating performance
For a fixed workload, a useful simplified model is
Here, Ts is serial work, Tparallel is parallelizable work measured on one worker, p is the worker count, and Hp is additional elapsed-time overhead. All times use matching units. Speedup Sp and efficiency Ep are dimensionless; multiply efficiency by 100% to express it as a percentage.
This model assumes enough execution resources, perfectly balanced parallel work, and no overlap between the listed time components. Actual overhead can include starting workers, exchanging messages, and waiting at synchronization points. With negligible overhead and serial fraction f, the model gives Amdahl's relation:
For nonzero f, increasing the worker count leaves a serial bottleneck. Growing overhead can even make additional workers slower.
What to remember
- Concurrency permits overlapping progress; parallelism requires simultaneous execution.
- Match communication and synchronization to the model you choose.
- Protect complete shared updates and check whether waiting activities can still progress.
- Estimate elapsed time using serial work, divided parallel work, and explicit overhead.
Example: Choosing workers for a simulation run
An engineering program evaluates independent operating cases. Its one-worker baseline has 1.20 s of serial setup and reporting, followed by 6.00 s of parallelizable calculation. The baseline has no additional worker-management overhead.
A four-worker configuration adds 0.30 s of total overhead. An eight-worker configuration adds 0.75 s of total overhead. Assume enough cores for simultaneous execution, equal division of the calculation, and no overlap between serial work, calculation, and overhead. Use these supplied times as exact model inputs and round final times to two decimal places.
a) Calculate the one-worker baseline elapsed time.
The baseline includes the serial work and the complete calculation on one worker. There is no additional overhead in this reference configuration, so add the two supplied durations directly.
The baseline elapsed time is 7.20 s. This complete baseline, including serial work, is the numerator used when calculating speedup.
b) Calculate the four-worker elapsed time, speedup, and efficiency. Report speedup to two decimal places and efficiency to one decimal place as a percentage.
Only the parallelizable calculation divides among the workers. The four-worker calculation time is
Add the unchanged serial time and the four-worker overhead:
Speedup compares the complete baseline with the complete four-worker elapsed time:
Efficiency compares that speedup with the worker count:
The four-worker configuration takes 3.00 s, achieves a speedup of 2.40, and has efficiency 60.0%. Serial work and overhead explain why four workers do not provide a speedup of four.
c) Calculate the eight-worker elapsed time and its percentage time reduction relative to four workers. Which configuration meets a requirement to finish within 2.80 s?
The eight workers divide the same calculation into equal shares:
Use the eight-worker overhead, which replaces the four-worker overhead:
The elapsed-time saving relative to four workers is
Use the four-worker time as the denominator because it is the comparison baseline:
Doubling the worker count reduces the calculation time by 0.75 s, but overhead increases by 0.45 s, leaving a net saving of 0.30 s. Among the two configurations, eight workers meet the 2.80 s requirement because 2.70 s<2.80 s; four workers take 3.00 s and exceed it.
An engineering application acquires sensor readings while analysing earlier measurements. Match each programming term to its definition, distinguishing how execution is organized from how shared access is controlled.
Simultaneous execution of operations on separate processing resources
Execution path within a process that shares the process's memory with other such paths
Organization of multiple activities so their progress can overlap in time, including through interleaved execution on one processor
Synchronization object that allows at most one execution path at a time to hold ownership of a lock
Concurrency
Thread
Mutex
Parallelism
Four worker threads process measurement batches in one process. After each completed batch, a worker increments a shared integer counter. Assume each increment consists of a separate read, addition, and write; none of these compound increments is automatically atomic. All counter accesses occur in these updates, and the main thread reads the result only after joining every worker.
Which strategy guarantees that every completed batch is counted, assuming the synchronization operations provide their documented memory-visibility guarantees?