26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG. More on synchronization<br />

Condition variables<br />

• It’s the status flag between the consumer and the producer that indicates the state of<br />

the system. If it’s set to a 1, it means that data is available for processing; if it’s set<br />

to a 0, it means that no data is available, and the consumer should block.<br />

• It serves as “the place where sleepon synchronization occurs.” More formally, the<br />

address of data_ready is used as a unique identifier, that serves as the rendezvous<br />

object for sleepon locks. We just as easily could have used “(void *) 12345”<br />

instead of “&data_ready” — so long as the identifier is unique and used<br />

consistently, the sleepon library really doesn’t care. Actually, using the address of a<br />

variable in a process is a guaranteed way to generate a process-unique number —<br />

after all, no two variables in a process will have the same address!<br />

We’ll defer the discussion of “What’s the difference between<br />

pthread_sleepon_signal() and pthread_sleepon_broadcast() ” to the discussion of<br />

condition variables next.<br />

Condition variables (or “condvars”) are remarkably similar to the sleepon locks we<br />

just saw above. In fact, sleepon locks are built on top of condvars, which is why we<br />

had a state of CONDVAR in the explanation table for the sleepon example. It bears<br />

repeating that the pthread_cond_wait() function releases the mutex, waits, and then<br />

reacquires the mutex, just like the pthread_sleepon_wait() function did.<br />

Let’s skip the preliminaries and redo the example of the producer and consumer from<br />

the sleepon section, using condvars instead. Then we’ll discuss the calls.<br />

/*<br />

* cp1.c<br />

*/<br />

#include <br />

#include <br />

int data_ready = 0;<br />

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;<br />

pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;<br />

void *<br />

consumer (void *notused)<br />

{<br />

printf ("In consumer thread...\n");<br />

while (1) {<br />

pthread_mutex_lock (&mutex);<br />

while (!data_ready) {<br />

pthread_cond_wait (&condvar, &mutex);<br />

}<br />

// process data<br />

printf ("consumer: got data from producer\n");<br />

data_ready = 0;<br />

pthread_cond_signal (&condvar);<br />

pthread_mutex_unlock (&mutex);<br />

}<br />

}<br />

void *<br />

April 30, 2009 Chapter 1 • Processes and Threads 63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!