#include #include #include // POSIX Threads using namespace std; // prototype for thread routine void *print_message_function1 ( void *ptr ); void *print_message_function2 ( void *ptr ); // 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; } thdata; int main() { pthread_t thread1, thread2; // thread variables // create threads 1 and 2 pthread_create (&thread1, NULL, print_message_function1, NULL); pthread_create (&thread2, NULL, print_message_function2, NULL); // Main block now waits for both threads to terminate, before it exits pthread_join(thread1, NULL); pthread_join(thread2, NULL); // exit exit(0); } // main() // Thread1 function void *print_message_function1 ( void *ptr ) { thdata *data; // do the work int i; for (i=0;i<10;i++) { printf("Thread %d says %s \n", 1, "Hello"); } pthread_exit(0); } // Thread2 function void *print_message_function2 ( void *ptr ) { thdata *data; // do the work int i; for (i=0;i<10;i++) { printf("Thread %d says %s \n", 2, "World"); } pthread_exit(0); }