Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Mutexes are often used for critical resources to prevent multiple threads from accessing them critical resources at the same time. Sometimes, when locking mutexes, deadlock will happen when multiple threads hold each other's lock, and the program consequently comes to a haltdeadlocks. There are four requirements for deadlock:

  • Mutual Exclusionmutual exclusion
  • Hold hold and Waitwait
  • No Preemption
  • Circular Wait
  • no preemption
  • circular wait

Deadlock Each deadlock requires all four conditions. Therefore, so, to prevent deadlock, prevent any one of the four conditions from being satisfied. This guideline recommends locking the mutexes in a predefined order to prevent circular wait.

...

The following code has behavior which is dependent that depends on the runtime environment and the platform's scheduler. However, with proper timing, the main() function will deadlock when running thr1 and thr2, where thr1 tries to lock ba2's mutex, while thr2 tries to lock on ba1's mutex in the deposit() function, and the program will not progress.

...

The solution to the deadlock problem is to use a predefined order for the locks in the deposit() function. In the following compliant solution, each thread will lock based on the id of bank_account id , defined in the struct initialization. This prevents the circular wait problem.

Code Block
bgColor#ccccff
typedef struct {
  int balance;
  pthread_mutex_t balance_mutex; 
  unsigned int id; /* should never be changed after initialized */
} bank_account;

unsigned int global_id = 1;

void create_bank_account(bank_account **ba, int initial_amount) {
  int result;
  bank_account *nba = malloc(sizeof(bank_account));
  if (nba == NULL) {
    /* Handle Error */
  }

  nba->balance = initial_amount;
  result = pthread_mutex_init(&nba->balance_mutex, NULL);
  if (result != 0) {
    /* Handle Error */
  }

  nba->id = global_id++;
  *ba = nba;
}


void *deposit(void *ptr) {
  deposit_thr_args *args = (deposit_thr_args *)ptr;
  int result;

  if (args->from->id == args->to->id) 
		return;

  /* ensure proper ordering for locking */
  if (args->from->id < args->to->id) {
    if ((result = pthread_mutex_lock(&(args->from->balance_mutex))) != 0) {
      /* Handle Error */
    }
    if ((result = pthread_mutex_lock(&(args->to->balance_mutex))) != 0) {
      /* Handle Error */
    }
  } else {
    if ((result = pthread_mutex_lock(&(args->to->balance_mutex))) != 0) {
      /* Handle Error */
    }
    if ((result = pthread_mutex_lock(&(args->from->balance_mutex))) != 0) {
      /* Handle Error */
    }
  }

  /* not enough balance to transfer */
  if (args->from->balance < args->amount) {
    if ((result = pthread_mutex_unlock(&(args->from->balance_mutex))) != 0) {
      /* Handle Error */
    }
    if ((result = pthread_mutex_unlock(&(args->to->balance_mutex))) != 0) {
      /* Handle Error */
    }
    return;
  }

  args->from->balance -= args->amount;
  args->to->balance += args->amount;

  if ((result = pthread_mutex_unlock(&(args->from->balance_mutex))) != 0) {
    /* Handle Error */
  }
  if ((result = pthread_mutex_unlock(&(args->to->balance_mutex))) != 0) {
    /* Handle Error */
  }


  free(ptr);
  return;
}

Risk Assessment

Deadlock causes prevents multiple threads to become unable to progressfrom progressing, thus halting the executing program. This is a potential denial-of-service attack because the attacker can force deadlock situations. It Deadlock is likely for deadlock to occur in multithreaded programs that manage multiple shared resources.

...