五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Linux 多線程編程實例

發(fā)布日期:2022-02-07 19:18 | 文章來源:腳本之家

一、多線程 VS 多進程

和進程相比,線程有很多優(yōu)勢。在Linux系統(tǒng)下,啟動一個新的進程必須分配給它獨立的地址空間,建立眾多的數(shù)據(jù)表來維護代碼段和數(shù)據(jù)。而運行于一個進程中的多個線程,他們之間使用相同的地址空間。正是這樣,同一進程下的線程之間共享數(shù)據(jù)空間,數(shù)據(jù)可以相互使用,并且線程間切換也要更快些,可以更有效的利用CPU。

二、程序設計

[注] 頭文件<pthread.h> 編譯時要加載動態(tài)庫 libpthread.a,使用 -lpthread

1、創(chuàng)建線程
2、等待線程
3、關(guān)閉線程
4、退出清除

1、創(chuàng)建線程

復制代碼
代碼如下:

int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, void *(*start_rtn)(void), void *arg)

tidp為線程id,是函數(shù)分配的值,所以要傳一個 pthread_t 的地址。
attr線程屬性,通常為空。
start_rtn為線程要執(zhí)行的函數(shù),返回值為空指針,參數(shù)為后面的*arg
若成功則返回0,否則返回出錯編號。

例:


復制代碼
代碼如下:

#include<stdio.h>
#include<pthread.h></p> <p>void *func1(void *arg){ //原函數(shù)聲明
int i;
for(i=0;i<5;i++){
printf("this is func1! The num is %d\n",*(int*)arg); //將空指針轉(zhuǎn)換為int型指針
sleep(1);
}
}</p> <p>void *func2(int *m){ //自定義類型聲明,也可以定義非指針類型,但是在create時會有警告,因為非地址并不能改變傳入的值
int i;
for(i=0;i<5;i++){
printf("this is func2! The num is %d\n",*m);
(*m)++;
sleep(1);
}
}</p> <p>int main(){
pthread_t id1,id2;
int num = 5;
int *p = &num;
if(pthread_create(&id1,NULL,(void *)func1,(void *)p) != 0){
printf("thread1 create error!\n");
return -1;
}
if(pthread_create(&id2,NULL,(void *)func2,&num) != 0){
printf("thread2 create error!\n");
return -1;
}
pthread_join(id1,NULL); //等待線程結(jié)束
pthread_join(id2,NULL);
printf("Running complete!\n");
return 0;</p> <p>
}

運行結(jié)果:


復制代碼
代碼如下:

[fsy@localhost process]$ gcc thC.c -o thC -lpthread -g
[fsy@localhost process]$ ./thC
this is func2! The num is 5
this is func1! The num is 6
this is func2! The num is 6
this is func1! The num is 7
this is func2! The num is 7
this is func1! The num is 8
this is func2! The num is 8
this is func1! The num is 9
this is func2! The num is 9
this is func1! The num is 10
Running complete!
[fsy@localhost process]$


2、等待線程

[注]當調(diào)用pthread_create函數(shù)時,線程并沒有開始執(zhí)行,主進程應有等待,比如用sleep,或者用更專業(yè)的函數(shù):pthread_join

復制代碼
代碼如下:

int pthread_join(pthread_t tid, void **rval_ptr)

調(diào)用函數(shù)可以阻塞調(diào)用線程,直到指定的線程終止。
tid為等待退出線程的id,rval_ptr為函數(shù)的返回值。是指向指針的指針,可以置空。

例:

復制代碼
代碼如下:

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h></p> <p>void *func(int *p){
int *num=(int *)malloc(sizeof(int)); //必須動態(tài)創(chuàng)建,原因可以參考我動態(tài)分配內(nèi)存的博客
printf("Please input the number: ");
scanf("%d",num);
return (void *)num; //類型是pthread_create的參數(shù)規(guī)定的
}</p> <p>int main(){
pthread_t pth;
void *a;
if(pthread_create(&pth,NULL,(void *)func,NULL) != 0){
printf("create thread error!\n");
return 1;
}
pthread_join(pth,&a); //指向空指針的指針
printf("get the num from the thread, it's %d\n",*(int *)a);
return 0;
}

3、終止線程

線程終止有以下三種方式:

1、線程從函數(shù)中返回
2、線程可以別其他函數(shù)終止
3、線程自己調(diào)用pthread_exit函數(shù)

復制代碼
代碼如下:

void pthread_exit(void *rval_ptr)

rval_ptr為線程退出返回值的指針,即函數(shù)返回值。

4、退出清除

復制代碼
代碼如下:

void pthread_cleanup_push(void (*rtn)(void*), void *arg)

rtn為清除函數(shù),arg是清除函數(shù)的參數(shù)

復制代碼
代碼如下:

void pthread_cleanup_pop(int execute)

當execute 非0時執(zhí)行清除函數(shù)。為0時不執(zhí)行。

從pthread_cleanup_push的調(diào)用點到pthread_cleanup_pop之間的程序段中,如果有終止進程的動作,如調(diào)用pthread_exit或異常終止(不包括return),就會執(zhí)行pthread_cleanup_push()所指定的清理函數(shù)。多個嵌套匹配時,就近匹配。

例:

復制代碼
代碼如下:

#include<stdio.h>
#include<pthread.h></p> <p>void *clean(char *argv){
printf("clean is called by %s\n",argv);
return NULL;
}
void *func1(void *argv){
printf("welcome enter the func1!\n");
pthread_cleanup_push((void*)clean,"the first time call!");
pthread_cleanup_push((void*)clean,"the second time call!");
if(argv){
return (void *)1;//第二次運行將此句注掉
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(1);
return (void *)0;
}</p> <p>void *func2(void *argv){
sleep(1); //兩個線程運行先后不確定
printf("welcome enter the func2!\n");
pthread_cleanup_push((void*)clean,"the first time call!");
pthread_cleanup_push((void*)clean,"the second time call!");
if(argv){
pthread_exit(NULL);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)0;
}</p> <p>
int main(){
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,(void *)func1,(void *)1) != 0){
printf("thread1 create error!\n");
return 1;
}</p> <p>if(pthread_create(&tid2,NULL,(void *)func2,(void *)1) != 0){
printf("thread2 create error!\n");
return 1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}

運行結(jié)果:

復制代碼
代碼如下:

[fsy@localhost process]$ gcc thClean.c -o thclean -lpthread
[fsy@localhost process]$ ./thclean
welcome enter the func1!
welcome enter the func2!
clean is called by the second time call! //此處先2后1
clean is called by the first time call!
[fsy@localhost process]$ vim thClean.c
[fsy@localhost process]$ gcc thClean.c -o thclean -lpthread
[fsy@localhost process]$ ./thclean
welcome enter the func1!
clean is called by the first time call! //second已經(jīng)被pop
welcome enter the func2!
clean is called by the second time call!
clean is called by the first time call!
[fsy@localhost process]$

香港服務器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關(guān)注
微信

關(guān)注官方微信
頂部