這篇文章主要介紹了linux下c語言的多線程編程,需要的朋友可以參考下
我們在寫linux的服務的時候,經常會用到linux的多線程技術以提高程序性能?
多線程的一些小知識:
一個應用程序可以啟動若干個線程。
線程(Lightweight Process,LWP),是程序執行的最小單元。
立即學習“C語言免費學習筆記(深入)”;
一般一個最簡單的程序最少會有一個線程,就是程序本身,也就是主函數(單線程的進程可以簡單的認為只有一個線程的進程)
?一個線程阻塞并不會影響到另外一個線程。
多線程的進程可以盡可能的利用系統CPU資源。
1創建線程
先上一段在一個進程中創建一個線程的簡單的代碼,然后慢慢深入。
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void?*?func(void?*?arg) { ?printf("func?run...n"); ?return?NULL; } int?main() { ?pthread_t?t1; ?int?err?=?pthread_create(&t1,NULL,func,NULL); ?if(err!=0) ?{ ??printf("thread_create?Failed:%sn",strerror(errno)); ?}else{ ??printf("thread_create?successn"); ?} ?sleep(1); ?return?EXIT_SUCCESS; } int?pthread_create(pthread_t?*thread,const?pthread_attr_t?*attr,?void?*(*start_routine)(void*),?void?*arg);</errno.h></string.h></stdlib.h></stdio.h></pthread.h>
在main函數里面我們調用上面的函數進行創建一個線程。
函數參數:
第一個參數:pthread_t代表創建線程的唯一標識,是一個結構體,需要我們創建好后,將這個結構體的指針傳遞過去。
第二個參數:pthread_attr_t,代表創建這個線程的一些配置,比如分配棧的大小等等。。一般我們可以填NULL,代表默認的創建線程的配置
第三個參數:代表一個函數的地址,創建線程時,會調用這個函數,函數的返回值是void*,函數的參數也是void*,一般格式就像void * func(void * arg){}
第四個參數:代表調用第三個函數傳遞的參數
函數返回值:
函數成功返回0,如果不等于0則代表函數調用失敗,此時通過strerror(errno)可以打印出具體的錯誤。
注意:每個線程都擁有一份errno副本,不同的線程擁有不同的errno
最后通過gcc編譯
gcc?1createthread.c?-c?-o?1createthread.o gcc?1createthread.o?-o?thr1?-lpthread
編譯的時候需要加上-lpthread 用來鏈接libpthread.so動態庫,不然會提示找不到function
函數調用返回結果
問題:為什么調用sleep函數
答:可能新創建的線程還沒運行到打印的方法主線程就結束了,而主線程結束,所有線程都會結束了。
2線程掛起
有時候我們在一個線程中創建了另外一個線程,主線程要等到創建的線程返回了,獲取該線程的返回值后主線程才退出。這個時候就需要用到線程掛起。
int?pthread_join(pthread_t?th,?void?**thr_return);。
pthread_join函數用于掛起當前線程,直至th指定的線程終止為止。
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void?*?func(void?*?arg) { ?int?i=0; ?for(;i<p>函數執行結果</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/761d48b2b485187975e893de94d24c61-1.jpg"></p> <p>我們主函數一直在等待創建的線程執行完,并且得到了線程執行結束的返回值</p> <p><span style="color: #ff0000"><strong>3線程終止</strong></span></p> <p>進程終止時exit()函數,那么線程終止是什么呢?</p> <p>線程終止的三種情況:</p> <p>線程只是從啟動函數中返回,返回值是線程的退出碼。</p> <p>線程可以被同一進程中的其他線程取消。</p> <p>線程調用pthread_exit。</p> <p class="jb51code"><br></p> <pre class="brush:cpp;">#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void?*?func(void?*?arg) { ?int?i=0; ?while(1) ?{ ??if(i==10) ??{ ???int?*?p?=?(int?*)malloc(sizeof(int)); ???*p=11; ???pthread_exit(p); ??} ??printf("fun?run?%dn",i++); ??sleep(1); ?} ?return?NULL; } int?main() { ?pthread_t?t1,t2; ?int?err?=?pthread_create(&t1,NULL,func,NULL); ?if(err!=0) ?{ ??printf("thread_create?Failed:%sn",strerror(errno)); ?}else{ ??printf("thread_create?successn"); ?} ?void?*p=NULL; ?pthread_join(t1,&p); ?printf("線程退出:code=%d",*(int*)p); ?return?EXIT_SUCCESS; } void?pthread_exit(void?*arg);</errno.h></string.h></stdlib.h></stdio.h></pthread.h>
pthread_exit函數的參數就跟正常線程結束return的使用時一樣的,都會被等待它結束的主線程獲取到。
函數運行結果:
4線程分離
int?pthread_detach(pthread_t?th);
pthread_detach函數使線程處于被分離狀態。
如果不等待一個線程,同時對線程的返回值不感興趣,可以設置這個線程為被分離狀態,讓系統在線程退出的時候自動回收它所占用的資源。
一個線程不能自己調用pthread_detach改變自己為被分離狀態,只能由其他線程調用pthread_detach。
5線程取消
int?pthread_cancel(pthread_t?th);
pthread_cancel函數允許一個線程取消th指定的另一個線程。
函數成功,返回0,否則返回非0。
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void?*?func1(void?*?arg) { ?while(1) ?{ ??printf("fun?run...n"); ??sleep(1); ?} ?return?NULL; } int?main() { ?pthread_t?t1; ?if(pthread_create(&t1,NULL,func1,NULL)!=0) ?{ ??printf("thread_create?Failed:%sn",strerror(errno)); ??return?-1; ?} ?sleep(5); ?pthread_cancel(t1); ?pthread_join(t1,NULL); ?return?EXIT_SUCCESS; }</errno.h></string.h></stdlib.h></stdio.h></pthread.h>
?函數執行結果:
上面我們說過創建一個線程函數pthread_create的第二個參數,用來決定創建線程的一些初始化狀態,這里我們 舉個例子,改線程一創建就是分離狀態的線程(
上面介紹了pthread_detach函數的概念,可以通過pthread_attr_t在創建線程的時候就指定線程屬性為detach,而不用創建以后再去修改線程屬性。
)
先上一段代碼:
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void?*?func(void?*?arg) { ?int?i=0; ?for(;i<p>pthread_attr_t就是我們要傳入的參數的結構體,一般申明的步驟有</p> <p>1,申明一個pthread_attr_t對象</p> <p>2,函數pthread_attr_init初始化attr結構。</p> <p>3,設置線程的一些屬性,比如pthread_attr_setdetachstate函數就是設置該線程創建的時候為正常狀態還是分離狀態。</p> <p>4,函數pthread_attr_destroy釋放attr內存空間</p> <p>pthread_attr_setdetachstate把線程屬性設置為下面兩個合法值之一:</p> <table border="0" style="font-size: 14px; max-width: 850px; border-top: silver 1px solid; font-family: Verdana, Arial, Helvetica, sans-serif; border-right: silver 1px solid; white-space: normal; word-spacing: 0px; border-collapse: collapse; border-bottom: silver 1px solid; text-transform: none; font-weight: normal; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; padding-top: 0px; padding-left: 0px; margin: 0px; border-spacing: 0px; border-left: silver 1px solid; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px"><tbody style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px"> <tr style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px" class="firstRow"> <td class="oa1" width="423" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">值</p></td> <td class="oa1" width="355" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">說明</p></td> </tr> <tr style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px"> <td class="oa2" width="423" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">PTHREAD_CREATE_DETACHED</p></td> <td class="oa2" width="355" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">設置線程為分離狀態</p></td> </tr> <tr style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px"> <td class="oa3" width="423" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">PTHREAD_CREATE_JOINABLE</p></td> <td class="oa3" width="355" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">設置線程為正常狀態</p></td> </tr> </tbody></table> <p>上面函數運行結果:</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/761d48b2b485187975e893de94d24c61-4.jpg"></p> <p>因為線程是個分離狀態的,所以pthread_join掛起會失效,主線程很快運行結束,程序也就結束了,創建的線程還沒來得及運行</p> <p><strong>線程同步</strong></p> <p>有時候我們多個線程處理訂單扣減庫存會遇到這樣的問題,兩個線程同時進入一段代碼先查詢庫存,兩個都查出來為還剩一件庫存,第一個線程用掉這個庫存后,將庫存變為0,但是第二個線程剛才也查出來為1了,所以他還認為有庫存,</p> <p>這個時候操作就會引發我們想不到的意外,庫存變為負數了!!所以這個時候就需要使用線程的同步!!</p> <p>先上一段代碼看看效果:</p> <p class="jb51code"><br></p> <pre class="brush:bash;">#include<pthread.h> #include<stdio.h> #include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void?*?func(void?*?arg) { ?int?threadno?=*(int*)arg; ?int?i=0; ?for(;i<p>函數運行結果:</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/c696db446e357ad9a31396f99e5f3206-5.jpg"></p> <p>可以看到兩個線程是沒有規律的爭相處理的,如果這段代碼是扣減庫存就完蛋啦!,所以我們要對這段代碼進行加鎖,同一時刻只能有一個線程進入操作!</p> <p>先上代碼:</p> <p class="jb51code"><br></p> <pre class="brush:cpp;">#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> pthread_mutex_t?mutex?=?PTHREAD_MUTEX_INITIALIZER; void?*?func(void?*?arg) { ?pthread_mutex_lock(&mutex);//對mutex加鎖,其他線程進入后將會掛起,知道這個鎖被解鎖 ?int?threadno?=*(int*)arg; ?int?i=0; ?for(;i<p>函數運行結果:</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/c696db446e357ad9a31396f99e5f3206-6.jpg"></p> <p>可以看到第二個線程先進入后一直運行結束,對mutex解鎖后,第一個線程才能進方法里面運行!否則會掛起,一直等到鎖被解鎖!</p> <p>PTHREAD_MUTEX_INITIALIZER是初始化一個快速鎖的宏定義。<br></p> <p class="jb51code"><br></p> <pre class="brush:cpp;">pthread_mutex_t?mutex?=?PTHREAD_MUTEX_INITIALIZER;
加鎖解鎖函數:
int?pthread_mutex_lock(pthread_mutex_t?*mutex); int?pthread_mutex_unlock(pthread_mutex_t?*mutex);
總結