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 />

(Used for access and testing)<br />

MutexXYZ<br />

x y z<br />

CondvarXYZ<br />

(Used for waiting and waking)<br />

One-to-one mutex and condvar associations.<br />

One interesting note. Since there is no checking, you can do things like associate one<br />

set of variables <strong>with</strong> mutex “ABC,” and another set of variables <strong>with</strong> mutex “DEF,”<br />

while associating both sets of variables <strong>with</strong> condvar “ABCDEF:”<br />

(Used for access and testing)<br />

MutexABC<br />

MutexDEF<br />

a b c d e f<br />

CondvarABCDEF<br />

(Used for waiting and waking)<br />

Many-to-one mutex and condvar associations.<br />

Additional <strong>Neutrino</strong> services<br />

This is actually quite useful. Since the mutex is always to be used for “access and<br />

testing,” this implies that I have to choose the correct mutex whenever I want to look at<br />

a particular variable. Fair enough — if I’m examining variable “C,” I obviously need<br />

to lock mutex “MutexABC.” What if I changed variable “E”? Well, before I change it,<br />

I had to acquire the mutex “MutexDEF.” Then I changed it, and hit condvar<br />

“CondvarABCDEF” to tell others about the change. Shortly thereafter, I would release<br />

the mutex.<br />

Now, consider what happens. Suddenly, I have a bunch of threads that had been<br />

waiting on “CondvarABCDEF” that now wake up (from their pthread_cond_wait()).<br />

The waiting function immediately attempts to reacquire the mutex. The critical point<br />

here is that there are two mutexes to acquire. This means that on an SMP system, two<br />

concurrent streams of threads can run, each examining what it considers to be<br />

independent variables, using independent mutexes. Cool, eh?<br />

<strong>Neutrino</strong> lets you do something else that’s elegant. POSIX says that a mutex must<br />

operate between threads in the same process, and lets a conforming implementation<br />

extend that. <strong>Neutrino</strong> extends this by allowing a mutex to operate between threads in<br />

different processes. To understand why this works, recall that there really are two parts<br />

to what’s viewed as the “operating system” — the kernel, which deals <strong>with</strong> scheduling,<br />

68 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!