/* file: basic.c author: Ted Baker version: $Revision$ last modified by: $Author: cop4610 $ on $Date: 2002/09/20 10:13:25 $ purpose: Demonstrate thread creation and termination. This is also useful as a starting point for other examples. */ #define _XOPEN_SOURCE 500 #define _REENTRANT #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; 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 = 0; 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 */ } for (i = 0; i < NTHREADS; i++) { PCHECK (pthread_join (thread_id[i], NULL)); } exit (0); }