Concurrency and Synchronisation

Concurrency

Concurrency means multiple tasks make progress during overlapping periods. On a single-core CPU, the OS can switch between tasks so quickly that they appear to run at the same time. On a multi-core CPU, tasks may also run in parallel on different cores.

Concurrency is useful for responsiveness and sharing work, but it creates risks when tasks access shared data.

Race conditions

A race condition happens when a program’s result depends on the timing or order of concurrent operations.

For example, suppose two threads both add 1 to a shared counter that starts at 5. Each thread may:

  1. Read the value 5.

  2. Add 1.

  3. Write back 6.

The final value could be 6 instead of the expected 7 because one update overwrote the other.

The sequence of read, change and write needs protection.

Critical sections

A critical section is a part of a program that accesses shared data or resources. To avoid race conditions, a synchronisation method should ensure that only the permitted task or tasks access the shared resource at a time.

A good solution aims for:

  • Mutual exclusion: only one task at a time enters a protected section when exclusive access is required.

  • Progress: if no task is using the resource, a waiting task can eventually proceed.

  • Bounded waiting: a task should not wait forever while others repeatedly go first.

Synchronisation tools

Mutex (lock)

A mutex allows one thread at a time to access a protected resource. A thread locks it before entering the critical section and unlocks it when finished.

If a thread forgets to unlock, other threads may wait indefinitely.

Semaphore

A semaphore is a counter used to control access to a resource or coordinate tasks.

  • A binary semaphore has two states and can be used similarly to a lock.

  • A counting semaphore allows a limited number of tasks to use a group of resources.

Atomic operation

An atomic operation happens as one indivisible step: other threads cannot observe it halfway through. Atomic operations can safely update some shared values, though they do not solve every coordination problem.

Deadlock

A deadlock occurs when tasks wait forever for resources held by one another.

A common example: Thread A holds Lock 1 and waits for Lock 2, while Thread B holds Lock 2 and waits for Lock 1.

Ways to reduce deadlock risk include:

  • Acquire locks in a consistent order.

  • Keep critical sections short.

  • Avoid holding locks while waiting for unrelated events.

  • Use timeouts or carefully designed locking strategies where appropriate.

Starvation

Starvation occurs when a task waits indefinitely because other tasks keep getting access to the resource first. Fair scheduling or fair lock policies can reduce this risk.

Concurrency and security

Concurrency bugs can cause incorrect results, crashes or security weaknesses. If shared data is changed unexpectedly, access checks or program logic may be bypassed. Synchronisation helps make shared operations predictable.

Key terms to remember

Concurrency · parallelism · race condition · shared data · critical section · mutual exclusion · mutex · semaphore · atomic operation · deadlock · starvation

Check your understanding

  1. How does concurrency differ from parallelism?

  2. What is a race condition?

  3. What does a mutex protect?

  4. How can deadlock happen?

  5. What is starvation?

Answers:

  1. Concurrency means tasks make overlapping progress; parallelism means tasks execute at the same time on different processing units.

  2. A result depends on the timing or order of concurrent operations.

  3. It provides exclusive access to a shared resource or critical section.

  4. Tasks can each hold a resource while waiting for another resource held by a different task.

  5. A task waits indefinitely because others repeatedly get access first.