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.

Threads and processes<br />

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

pthread_attr_t attr;<br />

// initialize the attribute structure<br />

pthread_attr_init (&attr);<br />

// set the detach state to "detached"<br />

pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);<br />

// override the default of INHERIT_SCHED<br />

pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);<br />

pthread_attr_setschedpolicy (&attr, SCHED_RR);<br />

attr.param.sched_priority = 15;<br />

// finally, create the thread<br />

pthread_create (NULL, &attr, new_thread, NULL);<br />

To see what a multithreaded program “looks like,” you could run the pidin command<br />

from the shell. Say our program was called spud. Ifwerunpidin once before spud<br />

created a thread and once after spud created two more threads (for three total), here’s<br />

what the output would look like (I’ve shortened the pidin output to show only spud):<br />

Where a thread is a good idea<br />

# pidin<br />

pid tid name prio STATE Blocked<br />

12301 1 spud 10r READY<br />

# pidin<br />

pid tid name prio STATE Blocked<br />

12301 1 spud 10r READY<br />

12301 2 spud 10r READY<br />

12301 3 spud 10r READY<br />

As you can see, the process spud (process ID 12301) has three threads (under the “tid”<br />

column). The three threads are running at priority 10 <strong>with</strong> a scheduling algorithm of<br />

round robin (indicated by the “r” after the 10). All three threads are READY, meaning<br />

that they’re able to use CPU but aren’t currently running on the CPU (another,<br />

higher-priority thread, is currently running).<br />

Now that we know all about creating threads, let’s take a look at how and where we’d<br />

use them.<br />

There are two classes of problems where the application of threads is a good idea.<br />

Threads are like overloading operators in C++ — it may seem like a good idea (at the<br />

time) to overload every single operator <strong>with</strong> some interesting use, but it makes the<br />

code hard to understand. Similarly <strong>with</strong> threads, you could create piles of threads, but<br />

the additional complexity will make your code hard to understand, and therefore hard<br />

to maintain. Judicious use of threads, on the other hand, will result in code that is<br />

functionally very clean.<br />

Threads are great where you can parallelize operations — a number of mathematical<br />

problems spring to mind (graphics, digital signal processing, etc.). Threads are also<br />

great where you want a program to perform several independent functions while<br />

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