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.

More on synchronization<br />

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

producer (void *notused)<br />

{<br />

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

while (1) {<br />

// get data from hardware<br />

// we’ll simulate this <strong>with</strong> a sleep (1)<br />

sleep (1);<br />

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

pthread_mutex_lock (&mutex);<br />

while (data_ready) {<br />

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

}<br />

data_ready = 1;<br />

pthread_cond_signal (&condvar);<br />

pthread_mutex_unlock (&mutex);<br />

}<br />

}<br />

main ()<br />

{<br />

printf ("Starting consumer/producer example...\n");<br />

// create the producer and consumer threads<br />

pthread_create (NULL, NULL, producer, NULL);<br />

pthread_create (NULL, NULL, consumer, NULL);<br />

}<br />

// let the threads run for a bit<br />

sleep (20);<br />

Signal versus broadcast<br />

Pretty much identical to the sleepon example we just saw, <strong>with</strong> a few variations (we<br />

also added some printf() functions and a main() so that the program would run!).<br />

Right away, the first thing that we see is a new data type: pthread_cond_t. This is<br />

simply the declaration of the condition variable; we’ve called ours condvar.<br />

Next thing we notice is that the structure of the consumer is identical to that of the<br />

consumer in the previous sleepon example. We’ve replaced the<br />

pthread_sleepon_lock() and pthread_sleepon_unlock() <strong>with</strong> the standard mutex<br />

versions (pthread_mutex_lock() and pthread_mutex_unlock()). The<br />

pthread_sleepon_wait() was replaced <strong>with</strong> pthread_cond_wait(). The main difference<br />

is that the sleepon library has a mutex buried deep <strong>with</strong>in it, whereas when we use<br />

condvars, we explicitly pass the mutex. We get a lot more flexibility this way.<br />

Finally, we notice that we’ve got pthread_cond_signal() instead of<br />

pthread_sleepon_signal() (again <strong>with</strong> the mutex passed explicitly).<br />

In the sleepon section, we promised to talk about the difference between the<br />

pthread_sleepon_signal() and pthread_sleepon_broadcast() functions. In the same<br />

breath, we’ll talk about the difference between the two condvar functions<br />

pthread_cond_signal() and pthread_cond_broadcast().<br />

The short story is this: the “signal” version will wake up only one thread. So, if there<br />

were multiple threads blocked in the “wait” function, and a thread did the “signal,”<br />

then only one of the threads would wake up. Which one? The highest priority one. If<br />

64 Chapter 1 • Processes and Threads April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!