`
deepfuture
  • 浏览: 4321176 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79301
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:68099
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:101182
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:280241
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14534
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:65149
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31183
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45067
社区版块
存档分类
最新评论

LINUX-c-线程池-(条件信号量+互斥量)

阅读更多

knoppix@Microknoppix:/mnt-system/lx/test$ gcc -D_REENTRANT -lpthread -o testcondout testcondout.c
knoppix@Microknoppix:/mnt-system/lx/test$ ./testcondout

please input an integer:(<=1000)26
time out
time out
1/1 finished,push 1.0000000000
1/2 finished,push 0.5000000000
1/3 finished,push 0.3333333333
1/4 finished,push 0.2500000000
1/5 finished,push 0.2000000000
time out
1/6 finished,push 0.1666666667
1/7 finished,push 0.1428571429
1/8 finished,push 0.1250000000
1/9 finished,push 0.1111111111
1/10 finished,push 0.1000000000
time out
1/11 finished,push 0.0909090909
1/12 finished,push 0.0833333333
1/13 finished,push 0.0769230769
1/14 finished,push 0.0714285714
1/15 finished,push 0.0666666667
1/1:1.0000000000 added result 1.0000000000
1/2:0.5000000000 added result 1.5000000000
1/3:0.3333333333 added result 1.8333333333
1/4:0.2500000000 added result 2.0833333333
1/5:0.2000000000 added result 2.2833333333
1/6:0.1666666667 added result 2.4500000000
1/7:0.1428571429 added result 2.5928571429
1/8:0.1250000000 added result 2.7178571429
1/9:0.1111111111 added result 2.8289682540
1/10:0.1000000000 added result 2.9289682540
1/11:0.0909090909 added result 3.0198773449
1/12:0.0833333333 added result 3.1032106782
1/13:0.0769230769 added result 3.1801337551
1/14:0.0714285714 added result 3.2515623266
1/15:0.0666666667 added result 3.3182289932
*******1/1:1.0000000000 computed result 1.0000000000
*******1/2:0.5000000000 computed result 0.5000000000
*******1/3:0.3333333333 computed result 0.8333333333
*******1/4:0.2500000000 computed result 0.5833333333
*******1/5:0.2000000000 computed result 0.7833333333
*******1/6:0.1666666667 computed result 0.6166666667
*******1/7:0.1428571429 computed result 0.7595238095
*******1/8:0.1250000000 computed result 0.6345238095
*******1/9:0.1111111111 computed result 0.7456349206
*******1/10:0.1000000000 computed result 0.6456349206
*******1/11:0.0909090909 computed result 0.7365440115
*******1/12:0.0833333333 computed result 0.6532106782
*******1/13:0.0769230769 computed result 0.7301337551
*******1/14:0.0714285714 computed result 0.6587051837
*******1/15:0.0666666667 computed result 0.7253718504
===============compute finish!=========== result:0.7253718504
1/16 finished,push 0.0625000000
1/17 finished,push 0.0588235294
1/18 finished,push 0.0555555556
1/19 finished,push 0.0526315789
1/20 finished,push 0.0500000000
1/16:0.0625000000 added result 3.3807289932
1/17:0.0588235294 added result 3.4395525226
1/18:0.0555555556 added result 3.4951080782
1/19:0.0526315789 added result 3.5477396571
1/20:0.0500000000 added result 3.5977396571
1/21 finished,push 0.0476190476
1/22 finished,push 0.0454545455
1/23 finished,push 0.0434782609
1/24 finished,push 0.0416666667
1/25 finished,push 0.0400000000
time out
*******1/16:0.0625000000 computed result 0.6628718504
*******1/17:0.0588235294 computed result 0.7216953798
*******1/18:0.0555555556 computed result 0.6661398242
*******1/19:0.0526315789 computed result 0.7187714032
*******1/20:0.0500000000 computed result 0.6687714032
*******1/21:0.0476190476 computed result 0.7163904508
*******1/22:0.0454545455 computed result 0.6709359053
*******1/23:0.0434782609 computed result 0.7144141662
*******1/24:0.0416666667 computed result 0.6727474995
*******1/25:0.0400000000 computed result 0.7127474995
===============compute finish!=========== result:0.7127474995
1/21:0.0476190476 added result 3.6453587048
1/22:0.0454545455 added result 3.6908132502
1/23:0.0434782609 added result 3.7342915111
1/24:0.0416666667 added result 3.7759581778
1/25:0.0400000000 added result 3.8159581778
1/26 finished,push 0.0384615385
time out
time out
*******1/26:0.0384615385 computed result 0.6742859611
===============compute finish!=========== result:0.6742859611
1/26:0.0384615385 added result 3.8544197162
================add finish!============ result:3.8544197162
2个线程完成累加和累加减运算(其中一个采用超时等待条件信号,另一个采用等待条件信号),n个线程完成计算每个符点数

   #include <pthread.h>  
   #include <bits/pthreadtypes.h>  
   #include <stdio.h>  
   #include <stdlib.h>  
   #include <errno.h> 
   #define MAXS 1000   
   #define MAXTDS 5 //线程池大小
    
     
  double myjg[MAXS+1];//计算结果存放位置  
  int max;  
  pthread_mutex_t eventlock;   //互斥锁
  pthread_cond_t myevent;   //条件变量
  pthread_t threads[MAXTDS+2];   //线程池,完成1/n计算
  int isend=0;

  int done;    
    
 void *mycomp(void *x){//计算1/i的结果,计算结果放在一个数组中。  
   int i=0;
   int rc;
   while (1){ 
  	 pthread_mutex_lock(&eventlock);  
  	 if (isend){
             pthread_mutex_unlock(&eventlock); 
             break;          	 
  	 }	 
 	 i=myjg[0];//myjg[0]存放着线程已经计算到的i。  	 
 	 if (i<max){
 	     i++;
 	     myjg[0]=i; 	     
 	 }  
         if (i==max){//最后一个数
   	          myjg[i]=(1/(double)i);
              isend=1;	
 	          printf("1/%d finished,push %.10f\n",i,myjg[i]); 
              fflush(stdout);
              pthread_mutex_unlock(&eventlock); 
              sleep(3);
              rc=pthread_cond_signal(&myevent);//广播信号,多个任务不被阻塞,多个任务竞争互斥锁的所有权。也可以使用pthread_cond_signal(&event);发送信号,这样只有一个线程不被阻塞,其它线程都被阻塞。
              if (rc){
                  perror("pthread_cond_broadcast");
                  fflush(stdout);
              }  
              sleep(2);  
              break;     
         }
    
     //开始计算
     myjg[i]=(1/(double)i);  
     printf("1/%d finished,push %.10f\n",i,myjg[i]); 
     fflush(stdout);	
     pthread_mutex_unlock(&eventlock);   
     if (!(i%MAXTDS)){
        sleep(3); 
        pthread_cond_broadcast(&myevent);//广播信号,多个任务不被阻塞,多个任务竞争互斥锁的所有权。也可以使用pthread_cond_signal(&event);发送信号,这样只有一个线程不被阻塞,其它线程都被阻塞。    
        sleep(3); 
     }
  } 
  pthread_exit(NULL);
}  

    
 void *myprint1(void *xx){//读取数组,将计算结果累加,最终完成1/1+1/2+1/3+......+1/n的计算,使用超时等待  
   int maxi;  
   int curj=1;  
   double jg=0; 
   int rc; 
   struct timeval now;//使用微秒
   struct timespec timeout;  //使用纳秒


      
   while(curj<=max)  
     {  
      //取当前时间
      // 深未来技术http://deepfuture.iteye.com/   
         gettimeofday(&now);
      //准备时间间隔
        timeout.tv_sec=now.tv_sec+1;
        timeout.tv_nsec=now.tv_usec*1000;
         maxi=0;
        
         pthread_mutex_lock(&eventlock);//用于条件变量的互斥,条件变量就是一个用来发送事件发生信号的信号量
         rc=pthread_cond_timedwait(&myevent,&eventlock,&timeout);//在等待条件变量myevent的发生,超时就返回,不再等待。条件变量必须与一个互斥锁eventlock相关联,条件变量不提供锁定,必须有一个互斥锁eventlock配合。
        //互斥锁eventlock在调用wait前应锁定,然后在wait期间,互斥量eventlock被解锁。挂起线程执行,直到条件变量myevent收到信号             

         if (rc==0){  // 深未来技术http://deepfuture.iteye.com/  
              maxi=myjg[0]; 
              fflush(stdout); 
              pthread_mutex_unlock(&eventlock);
              for (;curj<=maxi;curj++)
              {    // 深未来技术http://deepfuture.iteye.com/  
                   jg+=myjg[curj];  
                   printf("1/%d:%.10f added result %.10f\n",curj,myjg[curj],jg);     
                   fflush(stdout);                
              }   

         } 
         else if (rc==ETIMEDOUT){//TIMEOUT
              printf("time out\n");
              fflush(stdout); 
              pthread_mutex_unlock(&eventlock); 
              continue;            
         } 
         else  {  // 深未来技术http://deepfuture.iteye.com/  
              perror("pthread_cond_wait");
              fflush(stdout); 
              pthread_mutex_unlock(&eventlock); 
              continue;              
         }
    }  
    printf("================add finish!============ result:%.10f\n",jg);//输出累加结果。  
    fflush(stdout);
    pthread_exit(NULL);
}  
    
void *myprint2(void *xx){//读取数组,将计算结果完成1/1+1/2-1/3+1/4-1/5......的计算  
   int maxi=0;  
   int curi=1;  
   double jg=0; 
   int fh=1;
   int rc; 
   while(curi<=max)  
     {  
         maxi=0;
         sleep(2); 
         pthread_mutex_lock(&eventlock);//用于条件变量的互斥,条件变量就是一个用来发送事件发生信号的信号量
         rc=pthread_cond_wait(&myevent,&eventlock);//在等待条件变量myevent的发生。条件变量必须与一个互斥锁eventlock相关联,条件变量不提供锁定,必须有一个互斥锁eventlock配合。
        //互斥锁eventlock在调用wait前应锁定,然后在wait期间,互斥量eventlock被解锁。挂起线程执行,直到条件变量myevent收到信号  
  // 深未来技术http://deepfuture.iteye.com/  
         if (rc==0){
              maxi=myjg[0]; 
              fflush(stdout); 
              pthread_mutex_unlock(&eventlock);
              while (curi<=maxi){              
                    jg+=fh*myjg[curi];  
                    printf("*******1/%d:%.10f computed result %.10f\n",curi,myjg[curi],jg);  
                    fflush(stdout);       
                    fh=-fh;
                    curi++;
             }                 
             printf("===============compute finish!=========== result:%.10f\n",jg);//输出累加结果 
             fflush(stdout);              
         } 
         else{//error
              perror("pthread_cond_wait");
              fflush(stdout); 
              pthread_mutex_unlock(&eventlock); 
              continue;            
         }  
   }      

    pthread_exit(NULL);
 }  
   
   int main(){  
   //计算1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......  

     pthread_mutex_init(&eventlock,NULL);
     pthread_cond_init(&myevent,NULL);         
     int i =0;
     
     printf("please input an integer:(<=%d)",MAXS);  
     while (scanf("%d",&max),max>MAXS){//n的最大值  
        printf("please input an integer:(<=%d)",MAXS);  
     };  
     //深未来技术http://deepfuture.iteye.com/  
     myjg[0]=0;
  
     pthread_create(&(threads[i]),NULL,myprint1,(void *)&i);
     sleep(1); 
     i++;      
     pthread_create(&(threads[i]),NULL,myprint2,(void *)&i);  
     sleep(1);  
     i++; 
     for (;i<=MAXTDS;i++){  
         pthread_create(&(threads[i]),NULL,mycomp,(void *)&i);  
         sleep(1);        
    }   
    sleep(MAXTDS*2*(i/10+1));  //wait......
    pthread_mutex_destroy(&eventlock);    
    return(0);  
 }   
     

 

 

 

分享到:
评论

相关推荐

    linux c线程池

    linux pthreadpool实现和线程池的用处 简单易懂 互斥和信号量使用

    c++教程网的linux网络编程视频下载

    用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34POSIX消息队列 POSIX消息队列相关函数 POSIX...

    Linux网络编程 视频 教程

    用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34POSIX消息队列 POSIX消息队列相关函数 POSIX...

    C++教程网《Linux网络编程》视频百度云地址

    用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34POSIX消息队列 POSIX消息队列相关函数 POSIX...

    [免费]2018年C++教程网的linux网络编程视频百度云下载链接.rar

    用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34POSIX消息队列 POSIX消息队列相关函数 POSIX...

    linux网络编程

    信号量集函数 信号量示例 31System V信号量(二) 用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34...

    cloud_system.rar

    在Linux中利用C++搭建了epoll+线程池的服务器程序,对线程池所需要的互斥锁和信号量进行封装。

    C++教程网视频:linux网络编程

    用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34POSIX消息队列 POSIX消息队列相关函数 POSIX...

    2018年C++教程网的linux网络编程视频共41集百度云下载链接.rar

    用信号量实现进程互斥示例 32System V信号量(三) 用信号集解决哲学家就餐问题 33System V共享内存与信号量综合 用信号量解决生产者消费者问题 实现shmfifo 34POSIX消息队列 POSIX消息队列相关函数 POSIX消息队列...

    ThreadPool:自己实现的一个简单Linux下的线程池,可以设置线程池线程个数,任务队列大小

    ThreadPool自己实现的一个简单Linux下的线程池,可以设置线程池线程个数,任务队列大小master 使用条件变量和互斥量实现同步队列,volatile变量作为线程池退出标志feature/semaphore 使用信号量维护同步队列feature/...

    Linux高性能服务器编程

    14.1 Linux线程概述 14.1.1 线程模型 14.1.2 Linux线程库 14.2 创建线程和结束线程 14.3 线程属性 14.4 POSIX信号量 14.5 互斥锁 14.5.1 互斥锁基础API 14.5.2 互斥锁属性 14.5.3 死锁举例 14.6 条件变量...

    Linux多线程服务端编程:使用muduo C++网络库

    2.3不要用读写锁和信号量. . . . . . . . . . . . . . . . .. . . . . . . . . . . 43 2.4封装MutexLock、MutexLockGuard、Condition. . . . . . . . . . . . . . 44 2.5线程安全的Singleton 实现.. . . . . . . . ....

    AIO:基于协程的Delphi多线程库

    通道允许避免使用信号量/互斥量/等或线程安全队列手动传输数据样本的必要性。 您可以自由地将状态机安排到线程/线程池中,或将其重新安排到线程/线程池中。 开发人员可以轻松地将精力集中在商务任务上,AIO引擎将...

Global site tag (gtag.js) - Google Analytics