天天看點

多線程累加程式

多線程累加程式

#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;

多線程累加程式

}

多線程累加程式
多線程累加程式

繼續閱讀