




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、linux線程函數(shù)線程創(chuàng)建函數(shù)原型:ttinclude<pthread.h>intpthread_create(pthread_t"restricttidp,constpthread_attr_t*restrictattr,void*(*start_rtn)(void),void*restrictarg);返回值:若是成功建立線程返回0,否則返回錯誤的編號形式參數(shù):pthread_t*restricttidp要創(chuàng)建的線程的線程id指針constpthread_attr_trestrictattr創(chuàng)建線程時的線程屬性void*(start_rtn)(void)返回值是voi
2、d類型的指針函數(shù)vodi"restrictargstart_rtn的行參例題L功能:測試建立一個新的線程程序名稱:pthread_test.cttinclude<pthread.h>ttinclude<stdio.h>void*create(void*arg)啊.Iprintf(newthreadcreated");intmain(intargc,char*argv)啊Ipthread_ttidp;Iinterror;Ierror=pthread_create(&tidp,NULL,create,NULL);Iif(error!=0)鼬(Ip
3、rintf(,zpthread_createisnotcreated.");returnT;卜)printf(prthreadreateiscreated.);Ireturn0;編譯方法:#gcc-Wall-Ipthreadpthread_test.c因為pthread的庫不是linux系統(tǒng)的庫,所以在進行編譯的時候要加上-Ipthread,否則編譯不過,會出現(xiàn)下面錯誤thread_test.c:在函數(shù)'create'中:中,程序流程到達函數(shù)尾thread_test. c:7:警告:在有返回值的函數(shù)/tmp/ccOBJmuD.o:Infunction'main
4、':thread_test.c:(.text+0x4f):對pthread_create未定義的引用collect2:Id返回1現(xiàn)在我們能建立了一個線程了,我們可以從函數(shù)的原型看到,在創(chuàng)建線程的時候,是可以在對我們的函數(shù)進行傳遞參數(shù),在pthread_create的第四個行參。我們看一下例題23.例題2功能:向新的線程傳遞整形值程序名稱:pthread_int. cttinclude<stdio.h>include<pthread.h>include<unistd.h>void*create(void*arg)日凡.Iint*num;Inum=(in
5、t*)arg;Iprintf(createparameteris%d,*num);Ireturn(void*)0;L)intmain(intargc,char*argv)日國Ipthread_ttidp;Iinterror;IIinttest=4;Iint*attr=&test;error=pthread_create(&tidp,NULL,create,(void*)attr);IIif(error!=0)斡Iprintf(pthread-createiscreatedisnotcreated”);Ireturn-1;+Isleep(1);Iprintf(pthread-cr
6、eateiscreatediscreated");Ireturn0;4編譯方法:gcc-lpthreadthread_int.c-Wall執(zhí)行結(jié)果:createparameteris4pthread_createiscreatediscreated例題總結(jié):可以看出來,我們在main函數(shù)中傳遞的整行指針,傳遞到我們新建的線程函數(shù)中。在上面的例子可以看出來我們向新的線程傳入了另一個線程的int數(shù)據(jù),線程之間還可以傳遞字符串或是更復雜的數(shù)據(jù)結(jié)構(gòu)。例題3:程序功能:向新建的線程傳遞字符串程序名稱:thread_char. c#include<pthread.h>ttinclud
7、e<stdio.h>ttinclude<unistd.h>void"create(void*arg)臼國Ichar*name;Iname二(char*)arg;Iprintf("argis%sname);Ireturn(void*)0;L)intmain(intargc,char*argv)Ichar*a="wang"Iinterror;Ipthread_ttidp;IIerror=pthread_create(fttidp,NULL,create,(void*)a);Iif(error!=0)韓.Iprintf(pthreadi
8、snotcreatedIreturn-1;卜Isleep(1);Iprintf(pthreadiscreated.");Ireturn0;0編譯方法:gcc-Wallthread_char.c-Ipthread執(zhí)行結(jié)果:argiswangpthreadiscreated.例題總結(jié):可以看出來main函數(shù)中的字符串傳入了新建里的線程中。例題4thread_struct. cttinclude<stdio.h>Sincludepthread.h>#include<unistd.h>日#include<stdlib.h>/*/*malloc()*/
9、structtest臼國Iinta;Ichar*s;void*create(void*arg)臼國Istructtest*temp;Itemp=(structtest*)arg;Iprintf("test->a=%d”,temp->a);Iprintf("test->s=%s",temp->s);Ireturn(void*)0;Lintmain(intargc,char*argv)臼國Ipthread_ttidp;Iinterror;Istructtest*b;Ib=(structtest*)malloc(sizeof(structtest
10、);Ib->a=4;Ib->s="wang"error=pthread_create(&tidp,NULL,create,(void*)b);if(error!=0)施tIprintf(phreadisnotcreated.Ireturn-1;卜)Isleep(1);Iprintf(pthreadiscreated.");Ireturn0;4編譯方法:gcc-Wall-Ipthreadthread_struct.c執(zhí)行結(jié)果:test->a=4test->s=wangpthreadiscreated.線程包含了標識進程內(nèi)執(zhí)行環(huán)境必須的
11、信息。他集成了進程中的所有信息都是對線程進行共享的,包括文本程序、程序的全局內(nèi)存和堆內(nèi)存、棧以及文件描述符。例題5程序目的:驗證新建立的線程可以共享進程中的數(shù)據(jù)程序名稱:thread_share.cttinclude<stdio.h>ttinclude<pthread.h>ttinclude<unistd.h>staticinta=4;void*create(void*arg)臼國Iprintf("newpthread");Iprintf("a=%d",a);Ireturn(void*)0;L)intmain(inta
12、rgc,char*argv)日國Ipthread_ttidp;Iinterror;Ia=5;error=pthread_create(fttidp,NULL,create,NULL);IIif(error!=0)例Iprintf(newthreadisnotcreate");Ireturn-1;卜)Isleep(1);Iprintf(newthreadiscreated");Ireturn0;編譯方法:gcc-Wall-lpthreadthread_share.c叵|執(zhí)行結(jié)果:I回liewpthread.a=5newthreadiscreated例題總結(jié):可以看出來,我們在
13、主線程更改了我們的全局變量a的值的時候,我們新建立的線程則打印出來了改變的值,可以看出可以訪問線程所在進程中的數(shù)據(jù)信息。如果進程中任何一個線程中調(diào)用exit,_Exit,或者是_exit,那么整個進程就會終止,與此類似,如果信號的默認的動作是終止進程,那么,把該信號發(fā)送到線程會終止進程。線程的正常退出的方式:(1)線程只是從啟動例程中返回,返回值是線程中的退出碼(2)線程可以被另一個進程進行終止(3)線程自己調(diào)用pthread_exit函數(shù)兩個重要的函數(shù)原型:ttinclude<pthread.h>voidpthread_exit(void*rval_ptr);/*rval_ptr
14、線程退出返回的指針*/intpthread_join(pthread_tthread,void*rval_ptr);/*成功結(jié)束進程為0,否則為錯誤編碼*/例題6程序目的:線程正常退出,接受線程退出的程序名稱:exit_return.c#include<stdio.h>ttinclude<pthread.h>ttinclude<unistd.h>void*create(void*arg)臼國Iprintf("newthreadiscreated");Ireturn(void*)2;L)intmain(intargc,char*argv)啊
15、Ipthread_ttid;Iinterror;Ivoid*temp;error=pthread_create(&tid,NULL,create,NULD;IIif(error!=0)例Iprintf(threadisnotcreated);Ireturn-1;卜)Ierror=pthread_join(tid,&temp);IIif(error!=0)施.Iprintf("threadisnotexit”);0return-2;卜Iprintf("threadisexitcode%d二(int)temp);Isleep(1);IH1printf(threa
16、discreated.");Ireturn0;0編譯方法:gcc-Wallexit_return>c-lpthread執(zhí)行結(jié)果:newthreadiscreatedthreadisexitcode2threadiscreated.線程退出不僅僅可以返回線程的int數(shù)值,還可以返回一個復雜的數(shù)據(jù)結(jié)構(gòu)例題7程序目的:線程結(jié)束返回一個復雜的數(shù)據(jù)結(jié)exit_thread. cinclude<stdio.h>include<pthread.h>include<unistd.h>structtestIinta;Ichar*b;L);structtestt
17、emp=日團I.a=4,Ib=wangD;Wvoid"create(void*arg)s.dIprintf("newthread.");Ireturn(void*)fttemp;Sintmain(intargc,char*argv)回interror;Ipthread_ttid;Istructtest*c;0error=pthread_create(&tid,NULL,create,NULD;if(error!=0)Iprintf("newthreadisnotcreated");return-1;printf("main&q
18、uot;);Ierror=pthread_join(tid,(void*)&c);口if(error!=0)printf("newthreadisnotexitareturn-2;+Iprintf("c->a=%d",c->a);Iprintf("c->b=%s",c->b);Isleep(1);Ireturn0;0編譯方法:gcc-Wall-Ipthreadexit_structec執(zhí)行結(jié)果:main.newthreadc->a=4c->b=wang例題總結(jié):一定要記得返回的數(shù)據(jù)結(jié)構(gòu)要是在這個數(shù)據(jù)要
19、返回的結(jié)構(gòu)沒有釋放的時候應(yīng)用,如果數(shù)據(jù)結(jié)構(gòu)已經(jīng)發(fā)生變化,那返回的就不會是我們所需要的,而是藏數(shù)據(jù)阿。S、線程標識函數(shù)原型:ttinclude<pthread.h>pthread_tpthread_self(void);例題8程序目的:實現(xiàn)在新建立的線程中打印該線程的id和進程id程序名稱:thread_self.cinclude<stdio.h>ttinclude<pthread.h>s®#include<unistd.h>/*getpid()*/void"create(void*arg)ESIprintf("new
20、thread");Iprintf("threadid二二%u",(unsignedint)pthread_self();Iprintf("threadpidis%d”,getpid();Ireturn(void*)0;L)intmain(intargc,char*argv)Ipthread_ttid;Iinterror;Iprintf("Mainthreadisstarting”);Ierror=pthread_create(&tid,NULL,create,NULD;Iif(error!=0)施i(tZlprintf("th
21、readisnotcreated");Ireturn-1;卜)Hlprintf("mainpidis%d”,getpidO);Isleep(1);Ireturn0;9編譯方法:gcc-Wall-lpthreadthread_self>c執(zhí)行結(jié)果:Mainthreadisstartingmainpidis6860newthreadthread_tid=3084954544threadpidis68604、線程的處理程序函數(shù)原型:ttinclude<pthread.h>voidpthread_cleanup_push(void(*rtn)(void*),voi
22、d*arg);函數(shù)rtn是清理函數(shù),arg是調(diào)用參數(shù)voidpthread_cleanupjpop(intexecute);在前面講過線程的終止方式,是正常終止還是非正常終止,都會存在一個資源釋放的問題,在posix中提供了一組,就是我們上面看的函數(shù)進行線程退出的處理函數(shù),有些像在進程中的atexit 函數(shù)。M放的方式是指pthread_c1eanupjpush的調(diào)用點到pthread_c1eanup_pop之間程序段進行終止。pthread_cleanup_push()/pthread_cleanup_pop采用先入后出的方式的棧的管理方式,void*rtn(void*),在執(zhí)行pthrea
23、d_cleanup_push()壓入函數(shù)棧,多次執(zhí)行pthread_cleanup_push()形成一個函數(shù)鏈,在執(zhí)行這個函數(shù)鏈的時候會以反方向彈出,即先入后出。execute參數(shù)表識,是否執(zhí)行彈出清理函數(shù),當execute=0時不進行彈出清理函數(shù),非零的時候彈出處理函數(shù)。例題9程序目的:實現(xiàn)在正常結(jié)束線程的時候,進行函數(shù)處理程序名稱:thread_clean.c編譯方法:執(zhí)行結(jié)果:thread1starttMlthread1pushcompletethread1exitcode1thread2start回thread2pushcompletecleanup:thread2secondhand
24、lercleanup:thread2firsthandlerthread2exitcode2gcc-Wall-Ipthreadthread_clean.c#include<stdio.h>#include<pthread.h>#include<unistd.h>void*clean(void*arg)ESIprintf(Hcleanup:%sn9(char*)arg);Ireturn(void*)0;void*thr_fnl(void*arg)日圖Iprintf(nthread1startIpthread_cleanup_push(clean/*thread1firsthandlerv,);Ipthread_cleanup_push(clean,1*thread1secondhadlern);Iprintf(Hthread1pushcompleten);Iif(arg)施i(Ireturn(void*)1
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 健身俱樂部合同范本
- 分成合同范本上樣
- 蝦皮合作合同范本
- 代家出租民房合同范本
- 企業(yè)股票承銷合同范本
- 加盟福田汽車合同范本
- 全新拖拉機買賣合同范本
- 獸藥欠賬銷售合同范本
- 買賣生地合同范本
- 勞務(wù)合同范本在下
- 2025年湘教版二年級美術(shù)下冊計劃與教案
- GB/T 4706.30-2024家用和類似用途電器的安全第30部分:廚房機械的特殊要求
- 2024年岳陽職業(yè)技術(shù)學院單招職業(yè)技能測試題庫及答案解析
- 硬筆書法紙可打印
- 正丁烷的理化性質(zhì)及危險特性表
- 《朝天子詠喇叭》教學設(shè)計
- 《金融學基礎(chǔ)》實訓手冊
- 稅收基礎(chǔ)知識考試題庫
- 1t燃氣蒸汽鍋爐用戶需求(URS)(共13頁)
- 廣發(fā)證券分支機構(gòu)人員招聘登記表
- 機電一體化系統(tǒng)設(shè)計課件姜培剛[1]
評論
0/150
提交評論