The pthread tutorial https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview gives a concise but clear description about condition variable:
- While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data.
- A condition variable is always used in conjunction with a mutex lock.
And a representative sequence for using condition variables is shown below.
Two important points from above illustration:
- pthread_cond_wait unlocks the mutex so another thread can have the chance to signal the condition variable.
- When signaled, pthread_cond_wait locks the mutex. (If the mutex is locked by another thread, pthread_cond_wait has to wait for the mutex before returning.)
To better understand condition variable, let us take a look at the answer to a Google interview question: how to measure the speed of context switch. The answer was given in:
http://forum.codecall.net/c-c/31589-c-program-measures-speed-context-switch-unix-linux.html
Ah, UNIX stuff. Essentially you would create a function to alternate between persistant thread states, set lock and start/wakeup/sleep conditions plus a counter.
uint32_t COUNTER;
pthread_mutex_t LOCK;
pthread_mutex_t START;
pthread_cond_t CONDITION;
void * threads (void * unused) {
pthread_mutex_lock(&START);
pthread_mutex_unlock(&START);
pthread_mutex_lock(&LOCK);
if (COUNTER > 0) {
pthread_cond_signal(&CONDITION);
}
for (;;) {
COUNTER++;
pthread_cond_wait(&CONDITION, &LOCK);
pthread_cond_signal(&CONDITION);
}
pthread_mutex_unlock(&LOCK);
}
Next we create persistant threads t1 and t2 which are unlocked (awoken), and we roughly sleep one second to let them do their firing. (rough approximate of one second, granularity is different among platforms/clocks)
pthread_mutex_lock(&START);
COUNTER = 0;
pthread_create(&t1, NULL, threads, NULL);
pthread_create(&t2, NULL, threads, NULL);
pthread_detach(t1);
pthread_detach(t2);
myTime = tTimer();
pthread_mutex_unlock(&START);
sleep(1);
// Lock both simulaneous threads
pthread_mutex_lock(&LOCK);
//Normalize the result in second precision
myTime = tTimer() - myTime / 1000;

Posted by cxwangyi