Versions Compared

Key

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

...

This noncompliant code example uses flag as a synchronization primitive.:

Code Block
bgColor#ffcccc
langc
bool flag = false;

void test() {
  while (!flag) {
    sleep(1000);
  }
}

void wakeup(){
  flag = true;
}

void debit(unsigned int amount){
  test();
  account_balance -= amount;
}

...

This noncompliant code example uses flag as a synchronization primitive but qualifies flag as a volatile type.:

Code Block
bgColor#ffcccc
langc
volatile bool flag = false;

void test() {
  while (!flag){
    sleep(1000);
  }
}

void wakeup(){
  flag = true;
}

void debit(unsigned int amount) {
  test();
  account_balance -= amount;
}

...

This code uses a mutex to protect critical sections.:

Code Block
bgColor#ccccff
langc
#include <threads.h>

int account_balance;
mtx_t flag;

/* Initialize flag */

void debit(unsigned int amount) {
  mtx_lock(&flag);
  account_balance -= amount; /* Inside critical section */
  mtx_unlock(&flag);
}

...