4
I’m used to programming using this while in problems involving synchronization of threds
because that’s how API’s usually ask you to do it, but I’ve never wondered why it is. I am taking a course of operating systems to review the concepts I learned at university and I came across this doubt.
This brief video (in English) talks about mutual exclusion and the classic consumer producer.
In it, the teacher says that the consumer should wait in a loop while (condição)
until he is notified by a producer. Why not use a simple if
instead of a while
?
The problem addressed in the video is the following: Producers (competing threads) add values to the list and when this list fills up, the consimidor is notified. The consimidor should then display the contents of the list and then clear it.
In the video, she also says that the implementation of wait
must make a unlock
in the mutex
m and then make a lock
again. I didn’t understand the need for this unlock/lock
inside the Wait.
Can anyone clarify these two doubts? If possible, exemplify in C.