Method for Handling Deadlocks

In order for a deadlock to occur, you must have all four of the following conditions met:

  1. Mutual Exclusion.
  2. Hold and Wait: Processes already holding a resource can request additional resources, without relinquishing their current resources.
  3. No preemption: One process cannot forcibly remove another process’ resource.
  4. Circular wait.

Most deadlock prevention algorithms focus on avoiding condition #4: circular wait.

Deadlock Prevention:

– Disallow one of the four necessary conditions for deadlock

Deadlock Avoidance:

– Do not grant a resource request if this allocation have the potential to lead to a deadlock

Deadlock Prevention:

  • Mutual Exclusion
    • Not here
  • Hold and wait
    • No wait, require a thread to request and be allocated all its resources before it begins execution, or it can only request resources when it has none
  • No preemption
    • If a thread is holding some resources and requesting a resource that is held, then all resources currently being held are released. But need to save the state of preempted resources.
  • Circular wait
    • Locks (resources) are assigned numerical values, and threads must only acquired locks that have higher numbers than locks that have already acquired.
    • Let different threads acquire resources in the same order.
Advertisements

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.

Exceptions in C++

The notion of exception is provided to help get information from the point where an error is detected to a point where this error could be handled.

A called function cannot just return with an indication that an error happened. If the program is to continue working, the returning function must leave the program in a good state and not leak any resources.

An exception is an object thrown to represent the occurrence of an error.

An exception is caught by code that has expressed interest in handling this particular type of exception.

Exceptions make the complexity of error handling visible. However, exceptions are not the cause of that complexity. Be careful not to blame the messenger for bad news.

Lambda Expression in C++

A lambda expression, also referred to as lambda, is a simplified notation for defining and using anonymous function object. This is particularly useful when we want to pass a operation as argument to an algorithm.

A lambda expression consists of a sequence of parts;

  • capture list []
  • optional parameter list ()
  • optional mutable specifier
  • optional noexcept specifier
  • optional return type
  • and a body, specifying the code to be executed {}

Usually, we have to base our choice between “algorithm plus lambda” and “for-statement with body”. And of course, for-statement is more easily understood for beginners.

Abstract Classes in C++

Abstract classes act as expressions of general concepts from which more specific classes can be derived. A class that contains at least one pure virtual function is considered an abstract class. Classes that derived from the abstract class must implement the pure virtual function, otherwise they are also considered abstract classes.