#include #include #include #include using namespace std; // prototype for thread routine void *print_message_function1 ( void *ptr ); void *print_message_function2 ( void *ptr ); // to pass multiple arguments typedef struct str_thdata { int thread_no; char message[100]; } thdata; int main() { pthread_t thread1, thread2; thdata data1, data2; // initialize data to pass to thread 1 data1.thread_no = 1; strcpy(data1.message, "Hello"); // initialize data to pass to thread 2 data2.thread_no = 2; strcpy(data2.message, "World"); // create threads 1 and 2 pthread_create (&thread1, NULL, print_message_function1, (void *) &data1); pthread_create (&thread2, NULL, print_message_function2, (void *) &data2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); exit(0); } void *print_message_function1 ( void *ptr ) { thdata *data; data = (thdata *) ptr; // type cast to a pointer to thdata int i; for (i=0;i<10;i++) { printf("Thread %d says %s \n", data->thread_no, data->message); } pthread_exit(0); } void *print_message_function2 ( void *ptr ) { thdata *data; data = (thdata *) ptr; // type cast to a pointer to thdata int i; for (i=0;i<10;i++) { printf("Thread %d says %s \n", data->thread_no, data->message); } pthread_exit(0); }