欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

unix多線程控制應(yīng)用

1.線程三個(gè)屬性的學(xué)習(xí)

新賓ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!

綁定

分離

優(yōu)先級(jí)


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)//帶參數(shù) 
  12.     int counter = 10; 
  13.     while(counter--) 
  14.     { 
  15.         sleep(1); 
  16.         printf("%s\n",pstr); 
  17.     } 
  18.  
  19.     pthread_exit(&status_1);//線程退出專用函數(shù) 
  20.  
  21. void thread_1(void)//不帶參數(shù) 
  22.     int counter = 10; 
  23.     while(counter--) 
  24.     { 
  25.         sleep(1); 
  26.         printf("thread_1\n"); 
  27.     } 
  28.      
  29.  
  30.     pthread_exit(&status_2);//線程退出專用函數(shù) 
  31.  
  32.  
  33.  
  34. bool _creat_thread(pthread_t *thread, void *(*start_routine)(void *), void *arg, int SCOPE, int DETACH, int policy, int priority_val) 
  35.     struct sched_param param; 
  36.     pthread_attr_t attr;     
  37.     if(0 != pthread_attr_init(&attr))//初始化 結(jié)構(gòu)體attr, 如果不需要attr需要取消attr, pthread_attr_destroy 
  38.     { 
  39.         perror("pthread_attr_init\n"); 
  40.         return false;        
  41.     } 
  42.      
  43.     if(0 != pthread_attr_setscope(&attr, SCOPE))//設(shè)置線程屬性 是否綁定 綁定(PTHREAD_SCOPE_SYSTEM)  非綁定(PTHREAD_SCOPE_PROCESS) 
  44.     { 
  45.         perror("pthread_attr_setscope\n"); 
  46.         return false;        
  47.     } 
  48.      
  49.     if(0 != pthread_attr_setdetachstate(&attr, DETACH))//設(shè)置線程屬性 是否分離 分離(PTHREAD_CREATE_DETACHED)  非分離(PTHREAD_CREATE_JOINABLE) 
  50.     { 
  51.         perror("pthread_attr_setdetachstate\n"); 
  52.         return false;    
  53.     } 
  54.      
  55.     if(0 != pthread_attr_setschedpolicy(&attr, policy))//三種優(yōu)先級(jí)策略選擇 : SCHED_FIFO(值1-99), SCHED_RR(值1-99),  and SCHED_OTHER 
  56.     { 
  57.         perror("pead_attr_setschedpolicy\n"); 
  58.         return false;        
  59.     } 
  60.          
  61.     if(priority_val>0 && priority_val<100)//判斷優(yōu)先級(jí)值是否在1-99范圍 
  62.     { 
  63.         param.sched_priority = priority_val;  
  64.     } 
  65.     else 
  66.     { 
  67.         perror("priority_val_ wrong value range!!\n"); 
  68.     } 
  69.      
  70.      
  71.     if(0 != pthread_attr_setschedparam(&attr, &param))//設(shè)置設(shè)置線程屬性 優(yōu)先級(jí) 通過(guò) 策略與值來(lái)判斷如何調(diào)度 
  72.     { 
  73.         perror("pthread_attr_setschedparam\n"); 
  74.         return false;    
  75.     } 
  76.      
  77.     if(0 != pthread_create(thread, &attr, (void*)start_routine,arg))// 建立一個(gè)含有以上屬性的線程 
  78.     { 
  79.         perror("pthread_create\n"); 
  80.         return false; 
  81.     } 
  82.      
  83.     if(0 != pthread_attr_destroy(&attr))//使用完后取消attr,  
  84.     { 
  85.         perror("pthread_attr_destroy\n"); 
  86.         return false;        
  87.     } 
  88.      
  89.     return true; 



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.     bool ret; 
  17.     char *str = "thread_0\n"; 
  18.     pthread_t thd0,thd1; 
  19.  
  20.      
  21.     ret = _creat_thread(&thd0, (void*)thread_0, str, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, LOW_PRIO);// 創(chuàng)建一個(gè)線程 帶參數(shù)級(jí)相應(yīng)屬性 
  22.     if(ret) 
  23.     { 
  24.         printf("create thread successfully!\n"); 
  25.     } 
  26.     else 
  27.     { 
  28.         perror("fail to create thread!\n"); 
  29.         exit(1); 
  30.     } 
  31.     ret = _creat_thread(&thd1, (void*)thread_1, NULL, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, HIGH_PRIO);// 創(chuàng)建一個(gè)線程 不帶參數(shù),含相應(yīng)屬性 
  32.     if(ret) 
  33.     { 
  34.         printf("create thread successfully!\n"); 
  35.     } 
  36.     else 
  37.     { 
  38.         perror("fail to create thread!\n"); 
  39.         exit(1); 
  40.     } 
  41.  
  42.     int * thd_exit_status = NULL ; 
  43.      
  44.     while(1) 
  45.     { 
  46.         sleep(1); 
  47.         printf("main\n"); 
  48.          
  49.         if(0 != pthread_join(thd0,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and  the thd_exit_status = NULL 
  50.         { 
  51.             printf("thread_0 is not exist!!\n"); 
  52.             //exit(1); 
  53.         } 
  54.         else  
  55.         { 
  56.             if(NULL== thd_exit_status && 1 != *thd_exit_status) 
  57.             { 
  58.                 printf("pthd0 is runing\n"); 
  59.             } 
  60.             else if(NULL!= thd_exit_status && 1 == *thd_exit_status) 
  61.             { 
  62.                 printf("pthd0 has been terminated and return status:%d\n", *thd_exit_status); 
  63.             } 
  64.          
  65.         } 
  66.          
  67.         if(0 != pthread_join(thd1,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and  the thd_exit_status = NULL 
  68.         { 
  69.             printf("thread_1 is not exist!!\n"); 
  70.             //exit(1); 
  71.         } 
  72.         else 
  73.         { 
  74.             if(NULL == thd_exit_status && 2 != *thd_exit_status) 
  75.             { 
  76.                 printf("pthd_1 is runing\n"); 
  77.             } 
  78.              
  79.             else if(NULL!= thd_exit_status && 2 == *thd_exit_status) 
  80.             { 
  81.                 printf("pthd_1 has been terminated and return status:%d\n", *thd_exit_status); 
  82.             } 
  83.         } 
  84.     } 
  85.      
  86.     return 0; 

 

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

文章題目:unix多線程控制應(yīng)用
瀏覽地址:http://chinadenli.net/article42/joipec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、全網(wǎng)營(yíng)銷推廣網(wǎng)站設(shè)計(jì)、建站公司、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
黄色在线免费高清观看| 激情丁香激情五月婷婷| 大香蕉精品视频一区二区| 欧美国产日本高清在线| 国产乱人伦精品一区二区三区四区| 免费观看潮喷到高潮大叫| 久久99热成人网不卡| 日韩精品人妻少妇一区二区| 日韩欧美精品一区二区三区| 国产精品一区日韩欧美| 国产欧美日韩不卡在线视频| 成人精品日韩专区在线观看| 国产一区二区三区四区中文| 成人精品亚洲欧美日韩| 亚洲中文字幕综合网在线| 亚洲一区二区三区免费的视频| 国产不卡的视频在线观看| 欧美日韩精品一区免费| 欧美区一区二在线播放| 自拍偷女厕所拍偷区亚洲综合| 日本理论片午夜在线观看| 日本乱论一区二区三区| 伊人久久五月天综合网| 久久夜色精品国产高清不卡| 日本高清一道一二三区四五区| 视频在线免费观看你懂的| 日韩欧美国产亚洲一区| 精品香蕉一区二区在线| 欧美日韩精品久久亚洲区熟妇人| 能在线看的视频你懂的| 日本欧美三级中文字幕| 成年女人午夜在线视频| 伊人久久青草地婷婷综合| 亚洲精选91福利在线观看| 日本一区二区三区久久娇喘| 亚洲免费观看一区二区三区| 少妇毛片一区二区三区| 91偷拍与自偷拍精品| 尹人大香蕉中文在线播放| 国产精品涩涩成人一区二区三区| 日本午夜精品视频在线观看|