/* Includes */ #include /* Symbolic Constants */ #include /* Primitive System Data Types */ #include /* Errors */ #include /* Input/Output */ #include /* General Utilities */ #include /* POSIX Threads */ #include /* String handling */ int global; int status; /* parent process: child's exit status */ /* struct to hold data to be passed to a thread this shows how multiple data items can be passed to a thread */ typedef struct str_thdata { int thread_no; char message[100]; } thdata; /* prototype for thread routine */ void childfoo(void* ptr) { thdata *data; int local = 200; data = (thdata *) ptr; /* type cast to a pointer to thdata */ global = 100; printf("CHILD: I am the child thread! PID: %d\n", getpid()); printf("CHILD: My Value of global : %d\n", global); printf("CHILD: My Value of local : %d\n", local); } int main() { pthread_t thread1; /* thread variables */ thdata data1; /* structs to be passed to threads */ int local = 300; /* initialize data to pass to thread 1 */ data1.thread_no = 1; strcpy(data1.message, "Hello!"); /* create threads 1 and 2 */ pthread_create (&thread1, NULL, (void *) &childfoo, (void *) &data1); /* Main block now waits for both threads to terminate, before it exits If main block exits, both threads exit, even if the threads have not finished their work */ pthread_join(thread1, NULL); printf("PARENT: I am the parent process %d\n", getpid()); printf("PARENT: My Value of global : %d\n", global); printf("PARENT: My Value of local : %d\n", local); exit(0); /* parent exits */ } /* main() */