多线程累加程序

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值