Skip to content
Advertisement

Why is there a while loop in wait function of a semaphore, when if can be used too?

this is my code:

JavaScript

there is a while loop in the wait function of a semaphore, can’t I use an if statement simply?

Advertisement

Answer

Because we can’t assume that after a thread is woken up and it requires the lock another thread has not already come along and taken the resource this is guarding:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement