Concurrency 1 Flashcards
Explain the difference between multi-tasking and multi-threading…
Multi-tasking is the concurrent running of processes on a machine.
Multi-threading is the concurrent running of threads of execution over a program.
What makes multi-tasking expensive?
Context switching is needed whenever a core changes processes.
Why does multi-threading have less overhead than multi-tasking?
- All threads share the same address space.
- All threads share the same process.
- Threads can communicate with each other easily.
What is the main issue that multi-threading solves?
Idle time - Singular threading may lead to the CPU having idle time while waiting for operations to complete, for example, IO operations. Multi-threading enables other threads to continue other processes whilst waiting. This ensures efficient use of idle time.
Define Multi-tasking
Enables the CPU to perform multiple processes at once. Processes are assigned to cores. This is what enables systems to run multiple programs at any given time.
Define Non-Determinism… Why are multi-threaded programs non-deterministic?
From a flow of execution point of view, each time the multi-threaded program is run, a different output is given. In multi-threading, the sequence of execution is decided by the CPU running the threads. Thus, in a non-deterministic way.
Define Deterministic
The situation in which a program has a execution flow in which each method depends on an input from another method in a sequential way.
Define the race condition?
The situation in which multiple threads are reading from and writing to a shared resource simultaneously. This can lead to data inconsistency due to Thread A reading the value whilst Thread B writes an update value. Thus, Thread A now has incorrect data.
Why does the race condition occur?
Because all threads of a program share the same memory address space.
What is the solution to Race Condition?
Use the synchronised keyword. This locks the object that the thread is executing over, thus meaning only one thread can be running on the object. This can be used to prevent multiple threads operating on a shared resource.
Define cache incoherence…
When multiple threads pull the same value from main memory, and all operate on it independently before assigning it back to memory. Thus, all threads will be assigning different values back to the same variable.
What is the solution to cache incoherence?
Using the volatile keyword on a variable. This ensures that when the variable is modified, it is done so consistently across the program, and not just locally for that thread.
Define Atomic Actions
An Atomic Action is one that has no middle section of execution. Operations are either completed entirely, or not started.
Why are Atomic Actions useful?
They make it impossible for data inconsistency to occur.
Define Non-determinism…
A feature of multi-threading in which the OS determines the order of tasks being executed via a complex scheduling algorithm. As programmers, we don’t have much control (unless thread management is used).