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

time (&now);<br />

printf ("thread1 starting at %s", ctime_r (&now, buf));<br />

}<br />

// do the computation<br />

// let’s just do a sleep here...<br />

sleep (20);<br />

pthread_barrier_wait (&barrier);<br />

// after this point, all three threads have completed.<br />

time (&now);<br />

printf ("barrier in thread1() done at %s", ctime_r (&now, buf));<br />

void *<br />

thread2 (void *not_used)<br />

{<br />

time_t now;<br />

char buf [27];<br />

time (&now);<br />

printf ("thread2 starting at %s", ctime_r (&now, buf));<br />

}<br />

// do the computation<br />

// let’s just do a sleep here...<br />

sleep (40);<br />

pthread_barrier_wait (&barrier);<br />

// after this point, all three threads have completed.<br />

time (&now);<br />

printf ("barrier in thread2() done at %s", ctime_r (&now, buf));<br />

main () // ignore arguments<br />

{<br />

time_t now;<br />

char buf [27];<br />

// create a barrier object <strong>with</strong> a count of 3<br />

pthread_barrier_init (&barrier, NULL, 3);<br />

// start up two threads, thread1 and thread2<br />

pthread_create (NULL, NULL, thread1, NULL);<br />

pthread_create (NULL, NULL, thread2, NULL);<br />

// at this point, thread1 and thread2 are running<br />

// now wait for completion<br />

time (&now);<br />

printf ("main () waiting for barrier at %s", ctime_r (&now, buf));<br />

pthread_barrier_wait (&barrier);<br />

}<br />

// after this point, all three threads have completed.<br />

time (&now);<br />

printf ("barrier in main () done at %s", ctime_r (&now, buf));<br />

The main thread created the barrier object and initialized it <strong>with</strong> a count of how many<br />

threads (including itself !) should be synchronized to the barrier before it “breaks<br />

through.” In our sample, this was a count of 3 — one for the main() thread, one for<br />

thread1(), and one for thread2(). Then the graphics computational threads (thread1()<br />

and thread2() in our case here) are started, as before. For illustration, instead of<br />

showing source for graphics computations, we just stuck in a sleep (20); and<br />

sleep (40); to cause a delay, as if computations were occurring. To synchronize,<br />

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