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
- #include"thread.h"
- #include<stdio.h>
- #include<unistd.h>
- #include<pthread.h>
- #include<stdbool.h>
- #include<stdlib.h>
- extern int status_1;
- extern int status_2;
- void thread_0(char*pstr)//帶參數(shù)
- {
- int counter = 10;
- while(counter--)
- {
- sleep(1);
- printf("%s\n",pstr);
- }
- pthread_exit(&status_1);//線程退出專用函數(shù)
- }
- void thread_1(void)//不帶參數(shù)
- {
- int counter = 10;
- while(counter--)
- {
- sleep(1);
- printf("thread_1\n");
- }
- pthread_exit(&status_2);//線程退出專用函數(shù)
- }
- bool _creat_thread(pthread_t *thread, void *(*start_routine)(void *), void *arg, int SCOPE, int DETACH, int policy, int priority_val)
- {
- struct sched_param param;
- pthread_attr_t attr;
- if(0 != pthread_attr_init(&attr))//初始化 結(jié)構(gòu)體attr, 如果不需要attr需要取消attr, pthread_attr_destroy
- {
- perror("pthread_attr_init\n");
- return false;
- }
- if(0 != pthread_attr_setscope(&attr, SCOPE))//設(shè)置線程屬性 是否綁定 綁定(PTHREAD_SCOPE_SYSTEM) 非綁定(PTHREAD_SCOPE_PROCESS)
- {
- perror("pthread_attr_setscope\n");
- return false;
- }
- if(0 != pthread_attr_setdetachstate(&attr, DETACH))//設(shè)置線程屬性 是否分離 分離(PTHREAD_CREATE_DETACHED) 非分離(PTHREAD_CREATE_JOINABLE)
- {
- perror("pthread_attr_setdetachstate\n");
- return false;
- }
- if(0 != pthread_attr_setschedpolicy(&attr, policy))//三種優(yōu)先級(jí)策略選擇 : SCHED_FIFO(值1-99), SCHED_RR(值1-99), and SCHED_OTHER
- {
- perror("pead_attr_setschedpolicy\n");
- return false;
- }
- if(priority_val>0 && priority_val<100)//判斷優(yōu)先級(jí)值是否在1-99范圍
- {
- param.sched_priority = priority_val;
- }
- else
- {
- perror("priority_val_ wrong value range!!\n");
- }
- if(0 != pthread_attr_setschedparam(&attr, ¶m))//設(shè)置設(shè)置線程屬性 優(yōu)先級(jí) 通過(guò) 策略與值來(lái)判斷如何調(diào)度
- {
- perror("pthread_attr_setschedparam\n");
- return false;
- }
- if(0 != pthread_create(thread, &attr, (void*)start_routine,arg))// 建立一個(gè)含有以上屬性的線程
- {
- perror("pthread_create\n");
- return false;
- }
- if(0 != pthread_attr_destroy(&attr))//使用完后取消attr,
- {
- perror("pthread_attr_destroy\n");
- return false;
- }
- return true;
- }
thread.h
- #ifndef THREAD_H
- #define THREAD_H
- #include<stdio.h>
- #include<pthread.h>
- #include<stdbool.h>
- void thread_0(char*);
- void thread_1(void);
- bool _creat_thread(pthread_t *,void *(*)(void *), void *, int, int, int, int);
- #endif //end THREAD_H
main.c
- #include"thread.h"
- #include<pthread.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- #define LOW_PRIO 1
- #define HIGH_PRIO 2
- int status_0 = 0;
- int status_1 = 1;
- int status_2 = 2;
- int main(void)
- {
- bool ret;
- char *str = "thread_0\n";
- pthread_t thd0,thd1;
- 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)屬性
- if(ret)
- {
- printf("create thread successfully!\n");
- }
- else
- {
- perror("fail to create thread!\n");
- exit(1);
- }
- ret = _creat_thread(&thd1, (void*)thread_1, NULL, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, HIGH_PRIO);// 創(chuàng)建一個(gè)線程 不帶參數(shù),含相應(yīng)屬性
- if(ret)
- {
- printf("create thread successfully!\n");
- }
- else
- {
- perror("fail to create thread!\n");
- exit(1);
- }
- int * thd_exit_status = NULL ;
- while(1)
- {
- sleep(1);
- printf("main\n");
- if(0 != pthread_join(thd0,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and the thd_exit_status = NULL
- {
- printf("thread_0 is not exist!!\n");
- //exit(1);
- }
- else
- {
- if(NULL== thd_exit_status && 1 != *thd_exit_status)
- {
- printf("pthd0 is runing\n");
- }
- else if(NULL!= thd_exit_status && 1 == *thd_exit_status)
- {
- printf("pthd0 has been terminated and return status:%d\n", *thd_exit_status);
- }
- }
- if(0 != pthread_join(thd1,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and the thd_exit_status = NULL
- {
- printf("thread_1 is not exist!!\n");
- //exit(1);
- }
- else
- {
- if(NULL == thd_exit_status && 2 != *thd_exit_status)
- {
- printf("pthd_1 is runing\n");
- }
- else if(NULL!= thd_exit_status && 2 == *thd_exit_status)
- {
- printf("pthd_1 has been terminated and return status:%d\n", *thd_exit_status);
- }
- }
- }
- return 0;
- }
文章題目: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)