
#include < stdio.h >

#include < pthread.h >

#include < sys / time.h >

#include < string .h >


#define MAX 30


pthread_t thrd1,thrd2,thrd3,thrd4,thrd5;

pthread_mutex_t mut;

int number = 0 ,i;


void thread1( void )
... {
printf("Thread1: this is thread1. ");
for(i=0;i<MAX;i++)
...{
printf("Thread1:number = %d ",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(2);
}
printf("Thread1:is main process waiting for me? ");
pthread_exit(NULL);
}


void thread2( void )
... {
printf("Thread2: this is thread2. ");
for(i=0;i<MAX;i++)
...{
printf("Thread2:number = %d ",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("Thread2:is main process waiting for me? ");
pthread_exit(NULL);
}


void thread3( void )
... {
printf("Thread3: this is thread3. ");
for(i=0;i<MAX;i++)
...{
printf("Thread3:number = %d ",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(2);
}
printf("Thread3:is main process waiting for me? ");
pthread_exit(NULL);
}


void thread4( void )
... {
printf("Thread4: this is thread4. ");
for(i=0;i<MAX;i++)
...{
printf("Thread4:number = %d ",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("Thread4:is main process waiting for me? ");
pthread_exit(NULL);
}


void thread5( void )
... {
printf("Thread5: I am the reporter~~~~ ");
while(number<30)
...{
printf("Reporter: The current number is %d ~~~~ ",number);
sleep(4);
}
}


void thread_create( void )
... {
int temp;
if((temp=pthread_create(&thrd1,NULL,(void *)thread1,NULL))!=0)
printf("Fail to create thread1 ");
else
printf("Create thread1--- ");
if((temp=pthread_create(&thrd2,NULL,(void *)thread2,NULL))!=0)
printf("Fail to create thread2 ");
else
printf("Create thread2--- ");
if((temp=pthread_create(&thrd3,NULL,(void *)thread3,NULL))!=0)
printf("Fail to create thread3 ");
else
printf("Create thread3--- ");
if((temp=pthread_create(&thrd4,NULL,(void *)thread4,NULL))!=0)
printf("Fail to create thread4 ");
else
printf("Create thread4--- ");
if((temp=pthread_create(&thrd5,NULL,(void *)thread5,NULL))!=0)
printf("Fail to create thread5 ");
else
printf("Create thread5--- ");
}


void thread_wait( void )
... {
if(thread1 !=0)
...{
pthread_join(thrd1,NULL);
printf("THread1 has terminated. ");
}
if(thread2 !=0)
...{
pthread_join(thrd2,NULL);
printf("Thread2 has terminated. ");
}
if(thread3 !=0)
...{
pthread_join(thrd3,NULL);
printf("Thread3 has terminated. ");
}
if(thread4 !=0)
...{
pthread_join(thrd4,NULL);
printf("Thread4 has terminated. ");
}
if(thread5 !=0)
...{
pthread_join(thrd5,NULL);
printf("Thread5 has terminated. ");
}
}


int main( void )
... {
struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,0);
pthread_mutex_init(&mut,NULL);
printf("Main process is creating thread...... ");
thread_create();
printf("Main process is waiting for thread's task...... ");
thread_wait();
gettimeofday(&tpend,0);
timeuse = 1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("###############used time: %f ",timeuse);
return 0;
}

