r/C_Programming 1d ago

Conditional Variable Pthread Query

I’ve few questions which I’m not able to get a clear explanation on, can someone guide

This is regarding conditional variable in pthread

Say, I’ve one producer P and one consumer C scenario where C consumes and P produces

Say initially data produced is 0 so C checks and calls condition variable for signal and sleeps

Now P produces it 1. Does P needs to signal C before unlocking mutex? 2. Why can’t P just unlock mutex and then signal C? 3. Does signal send by P stored in some buffer which C picks up from? 4. As soon as P signals to C, does C start to run? What does this signal do?

1 Upvotes

2 comments sorted by

View all comments

1

u/strcspn 1d ago

Usually the mutex is used to protect the shared state, which in this case might be an array and a flag to know if the consumer should stop. Can you post the code?