I have questions about pthread_cond_signal and pthread_cond_wait. For example, in the code below, According to my understanding, when inc_count calls pthread_cond_signal, count += 125 in watch_count can be executed only after count_mutex is unlocked in inc_count.
The count_mutex was unlocked in watch_count when pthread_cond_wait executed and only gets locked after pthread_mutex_unlock in inc_count is executed. Am I right?
void *inc_count(void *t)
{
int i;
long my_id = (long)t;
for (i = 0; i < TCOUNT; i++)
{
pthread_mutex_lock(&count_mutex);
count++;
if (count == COUNT_LIMIT)
{
pthread_cond_signal(&count_threshold_cv);
}
pthread_mutex_unlock(&count_mutex);
}
pthread_exit(NULL);
}
void *watch_count(void *t)
{
long my_id = (long)t;
pthread_mutex_lock(&count_mutex);
while (count < COUNT_LIMIT)
{
pthread_cond_wait(&count_threshold_cv, &count_mutex);
count += 125;
}
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}