unix 多线程控制应用
admin
2023-02-04 20:00:13
0次

1.线程三个属性的学习

绑定

分离

优先级


thread.c



  1. #include"thread.h" 
  2. #include<stdio.h> 
  3. #include<unistd.h> 
  4. #include<pthread.h> 
  5. #include<stdbool.h> 
  6. #include<stdlib.h> 
  7.  
  8. extern int status_1; 
  9. extern int status_2; 
  10.  
  11. void thread_0(char*pstr)//带参数 
  12. { 
  13.     int counter = 10; 
  14.     while(counter--) 
  15.     { 
  16.         sleep(1); 
  17.         printf("%s\n",pstr); 
  18.     } 
  19.  
  20.     pthread_exit(&status_1);//线程退出专用函数 
  21. } 
  22.  
  23. void thread_1(void)//不带参数 
  24. { 
  25.     int counter = 10; 
  26.     while(counter--) 
  27.     { 
  28.         sleep(1); 
  29.         printf("thread_1\n"); 
  30.     } 
  31.      
  32.  
  33.     pthread_exit(&status_2);//线程退出专用函数 
  34. } 
  35.  
  36.  
  37.  
  38. bool _creat_thread(pthread_t *thread, void *(*start_routine)(void *), void *arg, int SCOPE, int DETACH, int policy, int priority_val) 
  39. { 
  40.     struct sched_param param; 
  41.     pthread_attr_t attr;     
  42.     if(0 != pthread_attr_init(&attr))//初始化 结构体attr, 如果不需要attr需要取消attr, pthread_attr_destroy 
  43.     { 
  44.         perror("pthread_attr_init\n"); 
  45.         return false;        
  46.     } 
  47.      
  48.     if(0 != pthread_attr_setscope(&attr, SCOPE))//设置线程属性 是否绑定 绑定(PTHREAD_SCOPE_SYSTEM)  非绑定(PTHREAD_SCOPE_PROCESS) 
  49.     { 
  50.         perror("pthread_attr_setscope\n"); 
  51.         return false;        
  52.     } 
  53.      
  54.     if(0 != pthread_attr_setdetachstate(&attr, DETACH))//设置线程属性 是否分离 分离(PTHREAD_CREATE_DETACHED)  非分离(PTHREAD_CREATE_JOINABLE) 
  55.     { 
  56.         perror("pthread_attr_setdetachstate\n"); 
  57.         return false;    
  58.     } 
  59.      
  60.     if(0 != pthread_attr_setschedpolicy(&attr, policy))//三种优先级策略选择 : SCHED_FIFO(值1-99), SCHED_RR(值1-99),  and SCHED_OTHER 
  61.     { 
  62.         perror("pead_attr_setschedpolicy\n"); 
  63.         return false;        
  64.     } 
  65.          
  66.     if(priority_val>0 && priority_val<100)//判断优先级值是否在1-99范围 
  67.     { 
  68.         param.sched_priority = priority_val;  
  69.     } 
  70.     else 
  71.     { 
  72.         perror("priority_val_ wrong value range!!\n"); 
  73.     } 
  74.      
  75.      
  76.     if(0 != pthread_attr_setschedparam(&attr, ¶m))//设置设置线程属性 优先级 通过 策略与值来判断如何调度 
  77.     { 
  78.         perror("pthread_attr_setschedparam\n"); 
  79.         return false;    
  80.     } 
  81.      
  82.     if(0 != pthread_create(thread, &attr, (void*)start_routine,arg))// 建立一个含有以上属性的线程 
  83.     { 
  84.         perror("pthread_create\n"); 
  85.         return false; 
  86.     } 
  87.      
  88.     if(0 != pthread_attr_destroy(&attr))//使用完后取消attr,  
  89.     { 
  90.         perror("pthread_attr_destroy\n"); 
  91.         return false;        
  92.     } 
  93.      
  94.     return true; 
  95. } 



thread.h




  1. #ifndef THREAD_H 
  2. #define THREAD_H 
  3. #include<stdio.h> 
  4. #include<pthread.h> 
  5. #include<stdbool.h> 
  6.  
  7.  
  8. void thread_0(char*); 
  9.  
  10. void thread_1(void); 
  11.  
  12. bool _creat_thread(pthread_t *,void *(*)(void *), void *, int, int, int, int); 
  13.  
  14. #endif //end THREAD_H 

main.c


  1. #include"thread.h" 
  2. #include<pthread.h> 
  3. #include<unistd.h> 
  4. #include<stdio.h> 
  5. #include<stdlib.h> 
  6. #include<stdbool.h> 
  7.  
  8. #define LOW_PRIO 1 
  9. #define HIGH_PRIO 2 
  10.  
  11. int status_0 = 0; 
  12. int status_1 = 1; 
  13. int status_2 = 2; 
  14.  
  15. int main(void) 
  16. { 
  17.     bool ret; 
  18.     char *str = "thread_0\n"; 
  19.     pthread_t thd0,thd1; 
  20.  
  21.      
  22.     ret = _creat_thread(&thd0, (void*)thread_0, str, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, LOW_PRIO);// 创建一个线程 带参数级相应属性 
  23.     if(ret) 
  24.     { 
  25.         printf("create thread successfully!\n"); 
  26.     } 
  27.     else 
  28.     { 
  29.         perror("fail to create thread!\n"); 
  30.         exit(1); 
  31.     } 
  32.     ret = _creat_thread(&thd1, (void*)thread_1, NULL, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, HIGH_PRIO);// 创建一个线程 不带参数,含相应属性 
  33.     if(ret) 
  34.     { 
  35.         printf("create thread successfully!\n"); 
  36.     } 
  37.     else 
  38.     { 
  39.         perror("fail to create thread!\n"); 
  40.         exit(1); 
  41.     } 
  42.  
  43.     int * thd_exit_status = NULL ; 
  44.      
  45.     while(1) 
  46.     { 
  47.         sleep(1); 
  48.         printf("main\n"); 
  49.          
  50.         if(0 != pthread_join(thd0,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and  the thd_exit_status = NULL 
  51.         { 
  52.             printf("thread_0 is not exist!!\n"); 
  53.             //exit(1); 
  54.         } 
  55.         else  
  56.         { 
  57.             if(NULL== thd_exit_status && 1 != *thd_exit_status) 
  58.             { 
  59.                 printf("pthd0 is runing\n"); 
  60.             } 
  61.             else if(NULL!= thd_exit_status && 1 == *thd_exit_status) 
  62.             { 
  63.                 printf("pthd0 has been terminated and return status:%d\n", *thd_exit_status); 
  64.             } 
  65.          
  66.         } 
  67.          
  68.         if(0 != pthread_join(thd1,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and  the thd_exit_status = NULL 
  69.         { 
  70.             printf("thread_1 is not exist!!\n"); 
  71.             //exit(1); 
  72.         } 
  73.         else 
  74.         { 
  75.             if(NULL == thd_exit_status && 2 != *thd_exit_status) 
  76.             { 
  77.                 printf("pthd_1 is runing\n"); 
  78.             } 
  79.              
  80.             else if(NULL!= thd_exit_status && 2 == *thd_exit_status) 
  81.             { 
  82.                 printf("pthd_1 has been terminated and return status:%d\n", *thd_exit_status); 
  83.             } 
  84.         } 
  85.     } 
  86.      
  87.     return 0; 
  88. } 

 

附件:http://down.51cto.com/data/2362156

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...