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.

Threads and processes<br />

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

Wait a minute! We’re wasting precious seconds waiting for the hardware to do its<br />

thing!<br />

If we made this multithreaded, we should be able to get much better use of our CPU,<br />

right?<br />

Thread 1<br />

C<br />

C<br />

C<br />

Thread 2<br />

X<br />

W<br />

X<br />

W<br />

X<br />

W<br />

Time<br />

Multithreaded, single CPU.<br />

This is much better, because now, even though the second thread spends a bit of its<br />

time waiting, we’ve reduced the total overall time required to compute.<br />

If our times were T compute to compute, T tx to transmit, and T wait to let the hardware<br />

do its thing, in the first case our total running time would be:<br />

(T compute +T tx +T wait ) × num_x_lines<br />

whereas <strong>with</strong> the two threads it would be<br />

(T compute +T tx ) × num_x_lines +T wait<br />

which is shorter by<br />

T wait × (num_x_lines -1)<br />

assuming of course that T wait ≤ T compute .<br />

Note that we will ultimately be constrained by:<br />

T compute +T tx × num_x_lines<br />

because we’ll have to incur at least one full computation, and we’ll have to transmit<br />

the data out the hardware — while we can use multithreading to overlay the<br />

computation cycles, we have only one hardware resource for the transmit.<br />

Now, if we created a four-thread version and ran it on an SMP system <strong>with</strong> 4 CPUs,<br />

we’d end up <strong>with</strong> something that looked like this:<br />

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