天天看点

多线程累加程序

多线程累加程序

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

多线程累加程序

}

多线程累加程序
多线程累加程序

继续阅读