pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
void thread_1() {
pthread_mutex_lock(&mutex);
some_cond = true;
pthread_cond_signal(&cv);
pthread_mutex_unlock(&mutex);
}
void thread_2() {
pthread_mutex_lock(&mutex);
while (!some_cond)
pthread_cond_wait(&cv, &mutex);
printf("test"); // After signaling from thread_1, does this get ran after?
pthread_mutex_unlock(&mutex);
}
Let's say that thread_2 calls pthread_cond_wait.
Thread_1 comes along then does pthread_cond_signal.
I understand that thread_2 will be blocked when pthread_cond_wait is called, and unlock its mutex. However, I am confuse on which line of code will run in thread_2 after thread_1 calls signal.
In thread_2 when its woken up, does it start from the beginning where thread_2 now has access to the mutex, then locks it, then checks the while condition again, and sees that its true now and prints test?
Or does thread_2 get access to its mutex, then locks it and then the print("test") is ran after (ignoring the while condition)?