Threads and Tasksme

We call an activity potentially executed concurrently with other activities a task. A thread is the system-level representation of a computer’s facilities for executing a task. A thread can execute a task. A thread may share an address space with other threads. One of the central challenges of the programmer of a concurrent system is to make sure that threads access memory in a sensible manner.

Avoiding Data Races

The best way to avoid data races is not to share data. Keep interesting data in local variables, not shared with other threads. Where they cannot be used, for example, because lots of data need to be shared, use some form of locking:

  1. Mutexes: A mutex is an object used to represent the exclusive right to access some resource.
  2. Condition variables: A condition variable is a variable used by a thread to wait for an event generated by another thread or a timer.

Multiple Locks

It is fairly common to want to acquire multiple resources to do some task. Unfortunately, acquiring two locks implies the opportunity of deadlock.