educative.io

Educative

Why do we use Semaphores to print sequential numbers?

In the question Printing Number Series (Zero, Even, Odd), what makes us to choose Semaphores instead of wait() and notifyAll() to print the sequence ? As the Semaphores only have 0 / 1 permits, they act as mutex.

Hi @Hilda, Thanks for reaching out & It’s great to see that you are thinking out of the box.

Yes, you are correct that mutex can be used in place of binary semaphore (used in our case). We use semaphore instead of mutex to clarify that binary semaphore is a replica of the mutex.

I suggest you solve the same problem with a mutex for better learning.
Happy Coding :slight_smile: