Jay Taylor's notes

back to listing index

Condition Variable and Measure the Speed of Context Switching

[web search]
Original source (cxwangyi.wordpress.com)
Tags: unix posix multithreading context-switching cxwangyi.wordpress.com
Clipped on: 2016-08-09

Yi Wang's Tech Notes

A blog ported from http://cxwangyi.blogspot.com

Image (Asset 1/4) alt=
cxwangyi 5:38 pm on December 27, 2010  

Condition Variable and Measure the Speed of Context Switching

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.

Image (Asset 2/4) alt=

Two important points from above illustration:

  1. pthread_cond_wait unlocks the mutex so  another thread can have the chance to signal the condition variable.
  2. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
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;
Here follows my explanation on how above code works:
At first, only one persistent thread (say t1) can lock LOCK, so t2 waits. t1 then fulfills the condition (COUNTER++) but waits.  Since the main thread sleeps once it lets persistent threads go, so only the other persistent thread (t2) can be waken up (one switching). t2 signals CONDITION, but t1 cannot wait right now because t2 holds LOCK, and t1’s wait invocation have to wait until t2 unlocks LOCK.  Therefore, t2 goes on increase COUNTER and waits.  Since the wait invocation unlocks LOCK, so t1 now wakes up and signals CONDITION.  Similarly, t2 cannot wake up right now but has to wait until t1 increase COUNTER and actively waits again.
  1. Image (Asset 3/4) alt=
    skidki-goroda.Com 6:08 am on December 22, 2012

    Thanks for writing Condition Variable and Measure the Speed of Context Switching
    Yi Wang’s Tech Notes, I just actually had been seeking for something similar and was thrilled to obtain the advice through this particular content.

  2. Image (Asset 4/4) alt=
    your site 2:22 pm on July 29, 2013

    Thanks for another informative site. The place else may just I get that type of
    info written in such an ideal means? I’ve a challenge that I am simply now operating on, and I’ve been on the look out for such information.

Leave a Reply

Follow

Follow “Yi Wang's Tech Notes”

Get every new post delivered to your Inbox.

Join 40 other followers