#include <io.h>  
#include <sys/types.h> 
#include <errno.h>      
#include <stdio.h>     
#include <stdlib.h>   
#include <pthread.h>  
#include <string> 

// struct to hold data to be passed to a thread
// shows how multiple data items can be passed to a thread
typedef struct str_thdata
{
    int thread_no;
    std::string message;
} thdata;


// print_message_function is used as the start routine for the threads used
// it accepts a void pointer 
void print_message_function( void *ptr )
{
    thdata *data;            
    data = (thdata *) ptr; 
    
    /* do the work */
    printf("Thread %d says %s \n", data->thread_no, data->message);
    
    pthread_exit(0); /* exit */
} 


int main()
{
    pthread_t thread1, thread2;  // thread variables
    thdata data1, data2;         // data to be passed to threads
    
    // data to pass to thread 1
    data1.thread_no = 1;
    data1.message = "Hello!";

    // data to pass to thread 2
    data2.thread_no = 2;
    data2.message = "Hi!";
    
    // create threads 1 and 2
    pthread_create (&thread1, NULL, (void *(__cdecl *)(void *)) &print_message_function, (void *) &data1);
    pthread_create (&thread2, NULL, (void *(__cdecl *)(void *)) &print_message_function, (void *) &data2);

    /* 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);
    pthread_join(thread2, NULL);
              
    exit(0);
}


