正文
我們在寫linux的服務(wù)的時候,經(jīng)常會用到linux的多線程技術(shù)以提高程序性能
多線程的一些小知識:
一個應(yīng)用程序可以啟動若干個線程。
線程(Lightweight Process,LWP),是程序執(zhí)行的最小單元。
一般一個最簡單的程序最少會有一個線程,就是程序本身,也就是主函數(shù)(單線程的進程可以簡單的認為只有一個線程的進程)
一個線程阻塞并不會影響到另外一個線程。
多線程的進程可以盡可能的利用系統(tǒng)CPU資源。
1創(chuàng)建線程
先上一段在一個進程中創(chuàng)建一個線程的簡單的代碼,然后慢慢深入。
#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:%s\n",strerror(errno));
}else{
printf("thread_create success\n");
}
sleep(1);
return EXIT_SUCCESS;
}
int pthread_create(pthread_t *thread,const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
在main函數(shù)里面我們調(diào)用上面的函數(shù)進行創(chuàng)建一個線程。
函數(shù)參數(shù):
第一個參數(shù):pthread_t代表創(chuàng)建線程的唯一標(biāo)識,是一個結(jié)構(gòu)體,需要我們創(chuàng)建好后,將這個結(jié)構(gòu)體的指針傳遞過去。
第二個參數(shù):pthread_attr_t,代表創(chuàng)建這個線程的一些配置,比如分配棧的大小等等。。一般我們可以填NULL,代表默認的創(chuàng)建線程的配置
第三個參數(shù):代表一個函數(shù)的地址,創(chuàng)建線程時,會調(diào)用這個函數(shù),函數(shù)的返回值是void*,函數(shù)的參數(shù)也是void*,一般格式就像void * func(void * arg){}
第四個參數(shù):代表調(diào)用第三個函數(shù)傳遞的參數(shù)
函數(shù)返回值:
函數(shù)成功返回0,如果不等于0則代表函數(shù)調(diào)用失敗,此時通過strerror(errno)可以打印出具體的錯誤。
注意:每個線程都擁有一份errno副本,不同的線程擁有不同的errno
最后通過gcc編譯
gcc 1createthread.c -c -o 1createthread.o
gcc 1createthread.o -o thr1 -lpthread
編譯的時候需要加上-lpthread 用來鏈接libpthread.so動態(tài)庫,不然會提示找不到function
函數(shù)調(diào)用返回結(jié)果
![](http://image109.360doc.com/DownloadImg/2021/12/0516/235349645_2_202112050424335.jpg)
問題:為什么調(diào)用sleep函數(shù)
答:可能新創(chuàng)建的線程還沒運行到打印的方法主線程就結(jié)束了,而主線程結(jié)束,所有線程都會結(jié)束了。
2線程掛起
有時候我們在一個線程中創(chuàng)建了另外一個線程,主線程要等到創(chuàng)建的線程返回了,獲取該線程的返回值后主線程才退出。這個時候就需要用到線程掛起。
int pthread_join(pthread_t th, void **thr_return);。 pthread_join函數(shù)用于掛起當(dāng)前線程,直至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<5;i++)
{
printf("func run%d\n",i);
sleep(1);
}
int * p = (int *)malloc(sizeof(int));
*p=11;
return p;
}
int main()
{
pthread_t t1,t2;
int err = pthread_create(&t1,NULL,func,NULL);
if(err!=0)
{
printf("thread_create Failed:%s\n",strerror(errno));
}else{
printf("thread_create success\n");
}
void *p=NULL;
pthread_join(t1,&p);
printf("線程退出:code=%d\n",*(int*)p);
return EXIT_SUCCESS;
}
函數(shù)執(zhí)行結(jié)果
![](http://image109.360doc.com/DownloadImg/2021/12/0516/235349645_3_20211205042433614.jpg)
我們主函數(shù)一直在等待創(chuàng)建的線程執(zhí)行完,并且得到了線程執(zhí)行結(jié)束的返回值
3線程終止
進程終止時exit()函數(shù),那么線程終止是什么呢?
線程終止的三種情況:
- 線程只是從啟動函數(shù)中返回,返回值是線程的退出碼。
- 線程可以被同一進程中的其他線程取消。
- 線程調(diào)用pthread_exit。
#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 %d\n",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:%s\n",strerror(errno));
}else{
printf("thread_create success\n");
}
void *p=NULL;
pthread_join(t1,&p);
printf("線程退出:code=%d",*(int*)p);
return EXIT_SUCCESS;
}
void pthread_exit(void *arg);
pthread_exit函數(shù)的參數(shù)就跟正常線程結(jié)束return的使用時一樣的,都會被等待它結(jié)束的主線程獲取到。
函數(shù)運行結(jié)果:
![](http://image109.360doc.com/DownloadImg/2021/12/0516/235349645_4_20211205042434177.jpg)
4線程分離
int pthread_detach(pthread_t th);
pthread_detach函數(shù)使線程處于被分離狀態(tài)。
如果不等待一個線程,同時對線程的返回值不感興趣,可以設(shè)置這個線程為被分離狀態(tài),讓系統(tǒng)在線程退出的時候自動回收它所占用的資源。
一個線程不能自己調(diào)用pthread_detach改變自己為被分離狀態(tài),只能由其他線程調(diào)用pthread_detach。
5線程取消
int pthread_cancel(pthread_t th); pthread_cancel函數(shù)允許一個線程取消th指定的另一個線程。
函數(shù)成功,返回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:%s\n",strerror(errno));
return -1;
}
sleep(5);
pthread_cancel(t1);
pthread_join(t1,NULL);
return EXIT_SUCCESS;
}
函數(shù)執(zhí)行結(jié)果:
![](http://pubimage.360doc.com/wz/default.gif)
上面我們說過創(chuàng)建一個線程函數(shù)pthread_create的第二個參數(shù),用來決定創(chuàng)建線程的一些初始化狀態(tài),這里我們 舉個例子,改線程一創(chuàng)建就是分離狀態(tài)的線程(
上面介紹了pthread_detach函數(shù)的概念,可以通過pthread_attr_t在創(chuàng)建線程的時候就指定線程屬性為detach,而不用創(chuàng)建以后再去修改線程屬性。
)
先上一段代碼:
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
void * func(void * arg)
{
int i=0;
for(;i<5;i++)
{
printf("func run%d\n",i);
sleep(1);
}
int * p = (int *)malloc(sizeof(int));
*p=11;
return p;
}
int main()
{
pthread_t t1;
pthread_attr_t attr;//申明一個attr的結(jié)構(gòu)體
pthread_attr_init(&attr);//初始化結(jié)構(gòu)體
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//設(shè)置線程為分離線程
int err = pthread_create(&t1,&attr,func,NULL);
if(err!=0)
{
printf("thread_create Failed:%s\n",strerror(errno));
}else{
printf("thread_create success\n");
}
pthread_attr_destroy(&attr);
pthread_join(t1,NULL);
printf("主線程退出\n");
return EXIT_SUCCESS;
}
pthread_attr_t就是我們要傳入的參數(shù)的結(jié)構(gòu)體,一般申明的步驟有
1,申明一個pthread_attr_t對象
2,函數(shù)pthread_attr_init初始化attr結(jié)構(gòu)。
3,設(shè)置線程的一些屬性,比如pthread_attr_setdetachstate函數(shù)就是設(shè)置該線程創(chuàng)建的時候為正常狀態(tài)還是分離狀態(tài)。
4,函數(shù)pthread_attr_destroy釋放attr內(nèi)存空間
pthread_attr_setdetachstate把線程屬性設(shè)置為下面兩個合法值之一:
值
|
說明
|
PTHREAD_CREATE_DETACHED
|
設(shè)置線程為分離狀態(tài)
|
PTHREAD_CREATE_JOINABLE
|
設(shè)置線程為正常狀態(tài)
|
上面函數(shù)運行結(jié)果:
![](http://pubimage.360doc.com/wz/default.gif)
因為線程是個分離狀態(tài)的,所以pthread_join掛起會失效,主線程很快運行結(jié)束,程序也就結(jié)束了,創(chuàng)建的線程還沒來得及運行
線程同步
有時候我們多個線程處理訂單扣減庫存會遇到這樣的問題,兩個線程同時進入一段代碼先查詢庫存,兩個都查出來為還剩一件庫存,第一個線程用掉這個庫存后,將庫存變?yōu)?,但是第二個線程剛才也查出來為1了,所以他還認為有庫存,
這個時候操作就會引發(fā)我們想不到的意外,庫存變?yōu)樨摂?shù)了??!所以這個時候就需要使用線程的同步?。?/p>
先上一段代碼看看效果:
#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<10;i++)
{
printf("%d thread%d \n",threadno,i);
sleep(1);
}
return NULL;
}
int main()
{
pthread_t t1,t2;
int i1=1,i2=2;
pthread_create(&t1,NULL,func,&i1);
pthread_create(&t2,NULL,func,&i2);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
printf("主線程退出\n");
return EXIT_SUCCESS;
}
函數(shù)運行結(jié)果:
![](http://pubimage.360doc.com/wz/default.gif)
可以看到兩個線程是沒有規(guī)律的爭相處理的,如果這段代碼是扣減庫存就完蛋啦!,所以我們要對這段代碼進行加鎖,同一時刻只能有一個線程進入操作!
先上代碼:
#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<10;i++)
{
printf("%d thread%d \n",threadno,i);
sleep(1);
}
pthread_mutex_unlock(&mutex);
return NULL;
}
int main()
{
pthread_t t1,t2;
int i1=1,i2=2;
pthread_create(&t1,NULL,func,&i1);
pthread_create(&t2,NULL,func,&i2);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
printf("主線程退出\n");
return EXIT_SUCCESS;
}
函數(shù)運行結(jié)果:
![](http://pubimage.360doc.com/wz/default.gif)
可以看到第二個線程先進入后一直運行結(jié)束,對mutex解鎖后,第一個線程才能進方法里面運行!否則會掛起,一直等到鎖被解鎖!
PTHREAD_MUTEX_INITIALIZER是初始化一個快速鎖的宏定義。
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
加鎖解鎖函數(shù):
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
|