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

sleepons, the underlying condvars (on top of which sleepons are implemented) are<br />

allocated dynamically as threads wait for a particular object. Therefore, using sleepons<br />

<strong>with</strong> M objects and N threads blocked, you’d have (at most) N condvars (instead of<br />

M).<br />

However, condvars are more flexible than sleepons, because:<br />

1 Sleepons are built on top of condvars anyway.<br />

2 Sleepons have the mutex buried in the library; condvars allow you to specify it<br />

explicitly.<br />

The first point might just be viewed as being argumentative. :-) The second point,<br />

however, is significant. When the mutex is buried in the library, this means that there<br />

can be only one per process — regardless of the number of threads in that process, or<br />

the number of different “sets” of data variables. This can be a very limiting factor,<br />

especially when you consider that you must use the one and only mutex to access any<br />

and all data variables that any thread in the process needs to touch!<br />

A much better design is to use multiple mutexes, one for each data set, and explicitly<br />

combine them <strong>with</strong> condition variables as required. The true power and danger of this<br />

approach is that there is absolutely no compile time or run time checking to make sure<br />

that you:<br />

• have locked the mutex before manipulating a variable<br />

• are using the correct mutex for the particular variable<br />

• are using the correct condvar <strong>with</strong> the appropriate mutex and variable<br />

The easiest way around these problems is to have a good design and design review,<br />

and also to borrow techniques from object-oriented programming (like having the<br />

mutex contained in a data structure, having routines to access the data structure, etc.).<br />

Of course, how much of one or both you apply depends not only on your personal<br />

style, but also on performance requirements.<br />

The key points to remember when using condvars are:<br />

1 The mutex is to be used for testing and accessing the variables.<br />

2 The condvar is to be used as a rendezvous point.<br />

Here’s a picture:<br />

April 30, 2009 Chapter 1 • Processes and Threads 67

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

Saved successfully!

Ooh no, something went wrong!