Explanation and Code Example of a Livelock

103 views Asked by At

I'm having a hard time understanding livelock.

For example, is this a livelock?

// Task 1
void task1() {
  // Wait for event A with a timeout
  while (getEventUntil(A, 1000)) {
    // Fill the buffer
    fillBuf();
    // Set event B
    setEvent(B);
  }
}

// Task 2
void task2() {
  // Wait for event B with a timeout
  while (getEventUntil(B, 1000)) {
    // Consume the buffer
    consumeBuf();
    // Set event A
    setEvent(A);
  }
}

Here fillBuf and consumeBuf will never be executed, instead the while loop will loop endlessly for both tasks.

0

There are 0 answers