Thinking Concurrently
- Thinking concurrently is a method of computation where various calculations are executed simultaneously.
- This concept arises in computer science when executing instructions or jobs on a computer concurrently - either physically executing them at the same time or giving the appearance of simultaneous execution.
- The fundamental objective of concurrent computing is to maximise efficiency, improve throughput and improve response times.
- Within concurrent computing, you will encounter ‘threads’ and ‘processes’. A process is a running programme, containing its own memory space, while a thread is a unit of execution within a process. A process can contain multiple threads.
- Processes do not share memory spaces and they maintain their own ‘state’ information. Each process grows in its own private memory area. Threads within a process share their parent’s memory space.
- Threads function as independent paths of a process’s execution. This allows the independent threads of a process to work concurrently but with the advantage of having access to shared memory.
- Concurrency can lead to complex problems such as deadlocks, race conditions, and resource starvation. These issues occur when processes or threads conflict over resources or operations.
- A deadlock is a state where two or more processes are unable to proceed because each is waiting for the other to release a resource.
- A race condition occurs when the behaviour of a programme depends on the relative order of execution of threads. Unintended results can occur if the order changes.
- Resource starvation happens when a process is perpetually denied necessary resources to process its work, which can lead to the system’s underperformance.
- Synchronization is a key method used in concurrent computing. It ensures that concurrent processes or threads do not interfere with each other, or access shared data in a way that can cause errors or incorrect results.
- Key synchronisation methods include semaphores, locks, and condition variables. These tools manage shared resources, access to critical sections of code, and the coordination of process execution for effective concurrency.
- Both hardware and software can support concurrent computing. This can include multiple processors, multithreading within a single application, or distributed systems where different machines perform different tasks simultaneously.
Remember the key principle: in a concurrent system, multiple tasks are being started, run, and completed during the same period, but they might not be executed simultaneously.