文章目錄
- 多線程的一些小知識:
- 1建立線程 pthread_create
- 2線程挂起 pthread_join
- 3線程終止 pthread_exit
- 4線程分離 pthread_detach
- 5線程取消 pthread_cancel
- 線程同步 pthread_mutex_t互斥變量
我們在寫linux的服務的時候,經常會用到linux的多線程技術以提高程式性能
多線程的一些小知識:
- 一個應用程式可以啟動若幹個線程。
- 線程(Lightweight Process,LWP),是程式執行的最小單元。
- 一般一個最簡單的程式最少會有一個線程,就是程式本身,也就是主函數(單線程的程序可以簡單的認為隻有一個線程的程序)
- 一個線程阻塞并不會影響到另外一個線程。
- 多線程的程序可以盡可能的利用系統CPU資源。
1建立線程 pthread_create
先上一段在一個程序中建立一個線程的簡單的代碼,然後慢慢深入。
#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函數裡面我們調用上面的函數進行建立一個線程。
函數參數:
第一個參數: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線程挂起 pthread_join
有時候我們在一個線程中建立了另外一個線程,主線程要等到建立的線程傳回了,擷取該線程的傳回值後主線程才退出。這個時候就需要用到線程挂起。
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<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;
}
函數執行結果
我們主函數一直在等待建立的線程執行完,并且得到了線程執行結束的傳回值
3線程終止 pthread_exit
程序終止時exit()函數,那麼線程終止是什麼呢?
線程終止的三種情況:
- 線程隻是從啟動函數中傳回,傳回值是線程的退出碼。
- 線程可以被同一程序中的其他線程取消。
- 線程調用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函數的參數就跟正常線程結束return的使用時一樣的,都會被等待它結束的主線程擷取到。
函數運作結果:
4線程分離 pthread_detach
int pthread_detach(pthread_t th);
pthread_detach函數使線程處于被分離狀态。
如果不等待一個線程,同時對線程的傳回值不感興趣,可以設定這個線程為被分離狀态,讓系統線上程退出的時候自動回收它所占用的資源。
一個線程不能自己調用pthread_detach改變自己為被分離狀态,隻能由其他線程調用pthread_detach。
5線程取消 pthread_cancel
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:%s\n",strerror(errno));
return -1;
}
sleep(5);
pthread_cancel(t1);
pthread_join(t1,NULL);
return EXIT_SUCCESS;
}
函數執行結果:
上面我們說過建立一個線程函數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<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的結構體
pthread_attr_init(&attr);//初始化結構體
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//設定線程為分離線程
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就是我們要傳入的參數的結構體,一般申明的步驟有
- 申明一個pthread_attr_t對象
- 函數pthread_attr_init初始化attr結構。
- 設定線程的一些屬性,比如pthread_attr_setdetachstate函數就是設定該線程建立的時候為正常狀态還是分離狀态。
- 函數pthread_attr_destroy釋放attr記憶體空間
pthread_attr_setdetachstate把線程屬性設定為下面兩個合法值之一:
上面函數運作結果:
因為線程是個分離狀态的,是以pthread_join挂起會失效,主線程很快運作結束,程式也就結束了,建立的線程還沒來得及運作
線程同步 pthread_mutex_t互斥變量
有時候我們多個線程處理訂單扣減庫存會遇到這樣的問題,兩個線程同時進入一段代碼先查詢庫存,兩個都查出來為還剩一件庫存,第一個線程用掉這個庫存後,将庫存變為0,但是第二個線程剛才也查出來為1了,是以他還認為有庫存,
這個時候操作就會引發我們想不到的意外,庫存變為負數了!!是以這個時候就需要使用線程的同步!!
先上一段代碼看看效果:
#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;
}
函數運作結果:
可以看到兩個線程是沒有規律的争相處理的,如果這段代碼是扣減庫存就完蛋啦!,是以我們要對這段代碼進行加鎖,同一時刻隻能有一個線程進入操作!
#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;
}
函數運作結果:
可以看到第二個線程先進入後一直運作結束,對mutex解鎖後,第一個線程才能進方法裡面運作!否則會挂起,一直等到鎖被解鎖!
PTHREAD_MUTEX_INITIALIZER是初始化一個快速鎖的宏定義。
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
加鎖解鎖函數: