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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Process and thread fundamentals<br />

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

allocates memory, this new memory is available to all the threads as well. The trick<br />

here is to recognize whether the memory should be available to all the threads in the<br />

process. If it is, then you’ll need to have all the threads synchronize their access to it.<br />

If it isn’t, then we’ll assume that it’s specific to a particular thread. In that case, since<br />

only that thread has access to it, we can assume that no synchronization is required —<br />

the thread isn’t going to trip itself up!<br />

As we know from everyday life, things aren’t quite that simple. Now that we’ve seen<br />

the basic characteristics (summary: everything is shared), let’s take a look at where<br />

things get a little more interesting, and why.<br />

The diagram below shows the way that we’ll be representing threads and processes.<br />

The process is the circle, representing the “container” concept (the address space), and<br />

the three squigley lines are the threads. You’ll see diagrams like this throughout the<br />

book.<br />

A process as a container of threads.<br />

Mutual exclusion<br />

If you want to take a shower, and there’s someone already using the bathroom, you’ll<br />

have to wait. How does a thread handle this?<br />

It’s done <strong>with</strong> something called mutual exclusion. It means pretty much what you think<br />

— a number of threads are mutually exclusive when it comes to a particular resource.<br />

If you’re taking a shower, you want to have exclusive access to the bathroom. To do<br />

this, you would typically go into the bathroom and lock the door from the inside.<br />

Anyone else trying to use the bathroom would get stopped by the lock. When you’re<br />

done, you’d unlock the door, allowing someone else access.<br />

This is just what a thread does. A thread uses an object called a mutex (an acronym for<br />

MUTual EXclusion). This object is like the lock on a door — once a thread has the<br />

mutex locked, no other thread can get the mutex, until the owning thread releases<br />

(unlocks) it. Just like the door lock, threads waiting to obtain the mutex will be barred.<br />

Another interesting parallel that occurs <strong>with</strong> mutexes and door locks is that the mutex<br />

is really an “advisory” lock. If a thread doesn’t obey the convention of using the<br />

mutex, then the protection is useless. In our house analogy, this would be like<br />

someone breaking into the washroom through one of the walls ignoring the convention<br />

of the door and lock.<br />

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