/* file: basic.c author: Ted Baker version: $Revision$ last modified by: $Author: cop4610 $ on $Date: 2002/09/20 10:13:25 $ purpose: Demonstrate what happens to other threads when the main thread exits. */ #define _XOPEN_SOURCE 500 #define _POSIX_C_SOURCE 199506 #define _REENTRANT #include #include #include #include #include #define PCHECK(CALL){int result;\ if ((result = (CALL)) != 0) {\ fprintf (stderr, "FATAL: %s (%s)", strerror(result), #CALL);\ exit (-1);}} #define NTHREADS 10 void * my_thread_body (void * arg) { int id = (int) arg; int i; /* struct timespec mytime; mytime.tv_nsec = 1000; mytime.tv_sec = 0; if (nanosleep (&mytime, NULL)) { perror ("basic_nojoin"); exit (-1); } */ for (i = 0; i < 10000000; i++) /* do nothing */; fprintf (stderr, "This is thread %d\n", id); return NULL; } int main (int argc, char **argv) { pthread_t thread_id[NTHREADS]; int i; PCHECK (pthread_setconcurrency (2)); for (i = 1; i < NTHREADS; i++) { PCHECK (pthread_create ( &thread_id[i], /* place to store the id of new thread */ NULL, /* use default thread creation attributes */ my_thread_body, /* function for thread to execute */ (void *) i)); /* pass index of thread */ } exit (0); }