linux下c語言的多線程編程


 

我們在寫linux的服務的時候,經常會用到linux的多線程技術以提高程序性能

 

多線程的一些小知識:

一個應用程序可以啟動若干個線程。

線程(Lightweight Process,LWP),是程序執行的最小單元。

一般一個最簡單的程序最少會有一個線程,就是程序本身,也就是主函數(單線程的進程可以簡單的認為只有一個線程的進程)

 一個線程阻塞並不會影響到另外一個線程。

多線程的進程可以盡可能的利用系統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:%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線程掛起

有時候我們在一個線程中創建了另外一個線程,主線程要等到創建的線程返回了,獲取該線程的返回值后主線程才退出。這個時候就需要用到線程掛起。

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線程終止

進程終止時exit()函數,那么線程終止是什么呢?

線程終止的三種情況:

  1. 線程只是從啟動函數中返回,返回值是線程的退出碼。
  2. 線程可以被同一進程中的其他線程取消。
  3. 線程調用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線程分離

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:%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就是我們要傳入的參數的結構體,一般申明的步驟有

1,申明一個pthread_attr_t對象

2,函數pthread_attr_init初始化attr結構。


3,設置線程的一些屬性,比如pthread_attr_setdetachstate函數就是設置該線程創建的時候為正常狀態還是分離狀態。

4,函數pthread_attr_destroy釋放attr內存空間

 

pthread_attr_setdetachstate把線程屬性設置為下面兩個合法值之一:

 

說明

PTHREAD_CREATE_DETACHED

設置線程為分離狀態

PTHREAD_CREATE_JOINABLE

設置線程為正常狀態

 

上面函數運行結果:

因為線程是個分離狀態的,所以pthread_join掛起會失效,主線程很快運行結束,程序也就結束了,創建的線程還沒來得及運行

 


線程同步

有時候我們多個線程處理訂單扣減庫存會遇到這樣的問題,兩個線程同時進入一段代碼先查詢庫存,兩個都查出來為還剩一件庫存,第一個線程用掉這個庫存后,將庫存變為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;

 

加鎖解鎖函數:

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM