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. tt1.c<br />

}<br />

exit (EXIT_FAILURE);<br />

thread_pool_start (tpp);<br />

fprintf (stderr, "%s: thread_pool_start returned; errno %s\n",<br />

progname, strerror (errno));<br />

sleep (3000);<br />

exit (EXIT_FAILURE);<br />

}<br />

tt1.c<br />

For more information about this program, see “Kernel timeouts <strong>with</strong> pthread_join()”<br />

in the Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often chapter.<br />

/*<br />

* tt1.c<br />

*/<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#define SEC_NSEC 1000000000LL // 1 billion nanoseconds in a second<br />

void *<br />

long_thread (void *notused)<br />

{<br />

printf ("This thread runs for more than 10 seconds\n");<br />

sleep (20);<br />

}<br />

int<br />

main (void) // ignore arguments<br />

{<br />

uint64_t timeout;<br />

struct sigevent event;<br />

int<br />

rval;<br />

pthread_t thread_id;<br />

// set up the event -- this can be done once<br />

// This or event.sigev_notify = SIGEV_UNBLOCK:<br />

SIGEV_UNBLOCK_INIT (&event);<br />

// create a thread<br />

pthread_create (&thread_id, NULL, long_thread, NULL);<br />

// set up for 10 second timeout<br />

timeout = 10LL * SEC_NSEC;<br />

TimerTimeout (CLOCK_REALTIME, _NTO_TIMEOUT_JOIN, &event, &timeout, NULL);<br />

rval = pthread_join (thread_id, NULL);<br />

if (rval == ETIMEDOUT) {<br />

printf ("Thread %d is still running after 10 seconds!\n",<br />

April 30, 2009 Appendix: C • Sample Programs 323

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

Saved successfully!

Ooh no, something went wrong!