日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux多线程基本编程

發布時間:2024/9/30 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux多线程基本编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要參考:

作者:吳秦
出處:http://www.cnblogs.com/skynet/

前言

線程?為什么有了進程還需要線程呢,他們有什么區別?使用線程有什么優勢呢?還有多線程編程的一些細節問題,如線程之間怎樣同步、互斥,這些東西將在本文中介紹。我在某QQ群里見到這樣一道面試題:

是否熟悉POSIX多線程編程技術?如熟悉,編寫程序完成如下功能:

1)有一int型全局變量g_Flag初始值為0;

2) 在主線稱中起動線程1,打印“this is thread1”,并將g_Flag設置為1

3) 在主線稱中啟動線程2,打印“this is thread2”,并將g_Flag設置為2

4) 線程序1需要在線程2退出后才能退出

5) 主線程在檢測到g_Flag從1變為2,或者從2變為1的時候退出

我們帶著這題開始這篇文章,結束之后,大家就都會做了。本文的框架如下:

  • 1、進程與線程
  • 2、使用線程的理由
  • 3、有關線程操作的函數
  • 4、線程之間的互斥
  • 5、線程之間的同步
  • 6、試題最終代碼

1、進程與線程

進程是程序執行時的一個實例,即它是程序已經執行到何種程度的數據結構的匯集。從內核的觀點看,進程的目的就是擔當分配系統資源(CPU時間、內存等)的基本單位

線程是進程的一個執行流,是CPU調度和分派的基本單位,它是比進程更小的能獨立運行的基本單位。一個進程由幾個線程組成(擁有很多相對獨立的執行流的用戶程序共享應用程序的大部分數據結構),線程與同屬一個進程的其他的線程共享進程所擁有的全部資源。

"進程——資源分配的最小單位,線程——程序執行的最小單位"

進程有獨立的地址空間,一個進程崩潰后,在保護模式下不會對其它進程產生影響,而線程只是一個進程中的不同執行路徑。線程有自己的堆棧和局部變量,但線程沒有單獨的地址空間,一個線程死掉就等于整個進程死掉,所以多進程的程序要比多線程的程序健壯,但在進程切換時,耗費資源較大,效率要差一些。但對于一些要求同時進行并且又要共享某些變量的并發操作,只能用線程,不能用進程。

2、使用線程的理由

從上面我們知道了進程與線程的區別,其實這些區別也就是我們使用線程的理由。總的來說就是:進程有獨立的地址空間,線程沒有單獨的地址空間(同一進程內的線程共享進程的地址空間)。(下面的內容摘自Linux下的多線程編程)

使用多線程的理由之一是和進程相比,它是一種非常"節儉"的多任務操作方式。我們知道,在Linux系統下,啟動一個新的進程必須分配給它獨立的地址空間,建立眾多的數據表來維護它的代碼段、堆棧段和數據段,這是一種"昂貴"的多任務工作方式。而運行于一個進程中的多個線程,它們彼此之間使用相同的地址空間,共享大部分數據,啟動一個線程所花費的空間遠遠小于啟動一個進程所花費的空間,而且,線程間彼此切換所需的時間也遠遠小于進程間切換所需要的時間。據統計,總的說來,一個進程的開銷大約是一個線程開銷的30倍左右,當然,在具體的系統上,這個數據可能會有較大的區別。

使用多線程的理由之二是線程間方便的通信機制。對不同進程來說,它們具有獨立的數據空間,要進行數據的傳遞只能通過通信的方式進行,這種方式不僅費時,而且很不方便。線程則不然,由于同一進程下的線程之間共享數據空間,所以一個線程的數據可以直接為其它線程所用,這不僅快捷,而且方便。當然,數據的共享也帶來其他一些問題,有的變量不能同時被兩個線程所修改,有的子程序中聲明為static的數據更有可能給多線程程序帶來災難性的打擊,這些正是編寫多線程程序時最需要注意的地方。

除了以上所說的優點外,不和進程比較,多線程程序作為一種多任務、并發的工作方式,當然有以下的優點:

  • 提高應用程序響應。這對圖形界面的程序尤其有意義,當一個操作耗時很長時,整個系統都會等待這個操作,此時程序不會響應鍵盤、鼠標、菜單的操作,而使用多線程技術,將耗時長的操作(time consuming)置于一個新的線程,可以避免這種尷尬的情況。
  • 使多CPU系統更加有效。操作系統會保證當線程數不大于CPU數目時,不同的線程運行于不同的CPU上。
  • 改善程序結構。一個既長又復雜的進程可以考慮分為多個線程,成為幾個獨立或半獨立的運行部分,這樣的程序會利于理解和修改。

=============================

從函數調用上來說,進程創建使用fork()操作;線程創建使用clone()操作。Richard Stevens大師這樣說過:

  • fork?is expensive. Memory is copied from the parent to the child, all descriptors are duplicated in the child, and so on. Current implementations use a technique called copy-on-write, which avoids a copy of the parent's data space to the child until the child needs its own copy. But, regardless of this optimization,?fork?is expensive.

  • IPC is required to pass information between the parent and child after the?fork. Passing information from the parent to the child before the?fork?is easy, since the child starts with a copy of the parent's data space and with a copy of all the parent's descriptors. But, returning information from the child to the parent takes more work.

Threads help with both problems. Threads are sometimes called lightweight processes since a thread is "lighter weight" than a process. That is, thread creation can be 10–100 times faster than process creation.

All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem of synchronization.

=============================

3、有關線程操作的函數

#include <pthread.h>int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg); int pthread_join (pthread_t tid, void ** status); pthread_t pthread_self (void); int pthread_detach (pthread_t tid); void pthread_exit (void *status);

pthread_create用于創建一個線程,成功返回0,否則返回Exxx(為正數)。

  • pthread_t *tid:線程id的類型為pthread_t,通常為無符號整型,當調用pthread_create成功時,通過*tid指針返回。
  • const?pthread_attr_t *attr:指定創建線程的屬性,如線程優先級、初始棧大小、是否為守護進程等。可以使用NULL來使用默認值,通常情況下我們都是使用默認值。
  • void?*(*func) (void?*):函數指針func,指定當新的線程創建之后,將執行的函數。
  • void?*arg:線程將執行的函數的參數。如果想傳遞多個參數,請將它們封裝在一個結構體中。

pthread_join用于等待某個線程退出,成功返回0,否則返回Exxx(為正數)。

  • pthread_t tid:指定要等待的線程ID
  • void?** status:如果不為NULL,那么線程的返回值存儲在status指向的空間中(這就是為什么status是二級指針的原因!這種才參數也稱為“值-結果”參數)。

pthread_self用于返回當前線程的ID。

pthread_detach用于是指定線程變為分離狀態,就像進程脫離終端而變為后臺進程類似。成功返回0,否則返回Exxx(為正數)。變為分離狀態的線程,如果線程退出,它的所有資源將全部釋放。而如果不是分離狀態,線程必須保留它的線程ID,退出狀態直到其它線程對它調用了pthread_join

進程也是類似,這也是當我們打開進程管理器的時候,發現有很多僵死進程的原因!也是為什么一定要有僵死這個進程狀態。

pthread_exit用于終止線程,可以指定返回值,以便其他線程通過pthread_join函數獲取該線程的返回值。

  • void?*status:指針線程終止的返回值。

知道了這些函數之后,我們試圖來完成本文一開始的問題:

1)有一int型全局變量g_Flag初始值為0;

2)在主線稱中起動線程1,打印“this is thread1”,并將g_Flag設置為1

3)在主線稱中啟動線程2,打印“this is thread2”,并將g_Flag設置為2

這3點很簡單嘛!!!不就是調用pthread_create創建線程。代碼如下:

/** 1)有一int型全局變量g_Flag初始值為0;** 2)在主線稱中起動線程1,打印“this is thread1”,并將g_Flag設置為1** 3)在主線稱中啟動線程2,打印“this is thread2”,并將g_Flag設置為2**/ #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<errno.h> #include<unistd.h>int g_Flag=0;void* thread1(void*); void* thread2(void*);/** when program is started, a single thread is created, called the initial thread or main thread.* Additional threads are created by pthread_create.* So we just need to create two thread in main().*/ int main(int argc, char** argv) {printf("enter main\n");pthread_t tid1, tid2;int rc1=0, rc2=0;rc2 = pthread_create(&tid2, NULL, thread2, NULL);if(rc2 != 0)printf("%s: %d\n",__func__, strerror(rc2));rc1 = pthread_create(&tid1, NULL, thread1, &tid2);if(rc1 != 0)printf("%s: %d\n",__func__, strerror(rc1));printf("leave main\n");exit(0); } /** thread1() will be execute by thread1, after pthread_create()* it will set g_Flag = 1;*/ void* thread1(void* arg) {printf("enter thread1\n");printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());g_Flag = 1;printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());printf("leave thread1\n");pthread_exit(0); }/** thread2() will be execute by thread2, after pthread_create()* it will set g_Flag = 2;*/ void* thread2(void* arg) {printf("enter thread2\n");printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());g_Flag = 2;printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());printf("leave thread2\n");pthread_exit(0); }

這樣就完成了1)、2)、3)這三點要求。編譯執行得如下結果:

netsky@ubuntu:~/workspace/pthead_test$ gcc?-lpthread?test.c

如果程序中使用到了pthread庫中的函數,除了要#include<pthread.h>,在編譯的時候還有加上-lpthread?選項。?
netsky@ubuntu:~/workspace/pthead_test$ ./a.out?
enter main?
enter thread2?
this is thread2, g_Flag: 0, thread id is 3079588720?
this is thread1, g_Flag: 2, thread id is 3079588720?
leave thread2?
leave main?
enter thread1?
this is thread1, g_Flag: 2, thread id is 3071196016?
this is thread1, g_Flag: 1, thread id is 3071196016?
leave thread1?
但是運行結果不一定是上面的,還有可能是:

netsky@ubuntu:~/workspace/pthead_test$ ./a.out?
enter main?
leave main?
enter thread1?
this is thread1, g_Flag: 0, thread id is 3069176688?
this is thread1, g_Flag: 1, thread id is 3069176688?
leave thread1

或者是:

netsky@ubuntu:~/workspace/pthead_test$ ./a.out?
enter main?
leave main?
等等。這也很好理解因為,這取決于主線程main函數何時終止,線程thread1、thread2是否能夠來得急執行它們的函數。這也是多線程編程時要注意的問題,因為有可能一個線程會影響到整個進程中的所有其它線程!如果我們在main函數退出前,sleep()一段時間,就可以保證thread1、thread2來得及執行。

Attention:大家肯定已經注意到了,我們在線程函數thread1()、thread2()執行完之前都調用了pthread_exit。如果我是調用exit()又或者是return會怎樣呢?自己動手試試吧!

pthread_exit()用于線程退出,可以指定返回值,以便其他線程通過pthread_join()函數獲取該線程的返回值。?
return是函數返回,只有線程函數return,線程才會退出。?
exit是進程退出,如果在線程函數中調用exit,進程中的所有函數都會退出!

“4) 線程序1需要在線程2退出后才能退出”第4點也很容易解決,直接在thread1的函數退出之前調用pthread_join就OK了。

4、線程之間的互斥

上面的代碼似乎很好的解決了問題的前面4點要求,其實不然!!!因為g_Flag是一個全局變量,線程thread1和thread2可以同時對它進行操作,需要對它進行加鎖保護,thread1和thread2要互斥訪問才行。下面我們就介紹如何加鎖保護——互斥鎖。

互斥鎖:

使用互斥鎖(互斥)可以使線程按順序執行。通常,互斥鎖通過確保一次只有一個線程執行代碼的臨界段來同步多個線程。互斥鎖還可以保護單線程代碼。

互斥鎖的相關操作函數如下:

#include <pthread.h> int pthread_mutex_lock(pthread_mutex_t * mptr); int pthread_mutex_unlock(pthread_mutex_t * mptr); //Both return: 0 if OK, positive Exxx value on error

在對臨界資源進行操作之前需要pthread_mutex_lock先加鎖,操作完之后pthread_mutex_unlock再解鎖。而且在這之前需要聲明一個pthread_mutex_t類型的變量,用作前面兩個函數的參數。具體代碼見第5節。

5、線程之間的同步

第5點——主線程在檢測到g_Flag從1變為2,或者從2變為1的時候退出。就需要用到線程同步技術!線程同步需要條件變量。

條件變量:

使用條件變量可以以原子方式阻塞線程,直到某個特定條件為真為止。條件變量始終與互斥鎖一起使用。對條件的測試是在互斥鎖(互斥)的保護下進行的。

如果條件為假,線程通常會基于條件變量阻塞,并以原子方式釋放等待條件變化的互斥鎖。如果另一個線程更改了條件,該線程可能會向相關的條件變量發出信號,從而使一個或多個等待的線程執行以下操作:

  • 喚醒
  • 再次獲取互斥鎖
  • 重新評估條件

在以下情況下,條件變量可用于在進程之間同步線程:

  • 線程是在可以寫入的內存中分配的
  • 內存由協作進程共享

使用條件變量可以以原子方式阻塞線程,直到某個特定條件為真為止。”即可用到第5點,主線程main函數阻塞于等待g_Flag從1變為2,或者從2變為1。條件變量的相關函數如下:

#include <pthread.h>int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr); int pthread_cond_signal(pthread_cond_t *cptr); //Both return: 0 if OK, positive Exxx value on error

pthread_cond_wait用于等待某個特定的條件為真,pthread_cond_signal用于通知阻塞的線程某個特定的條件為真了。在調用者兩個函數之前需要聲明一個pthread_cond_t類型的變量,用于這兩個函數的參數。

為什么條件變量始終與互斥鎖一起使用,對條件的測試是在互斥鎖(互斥)的保護下進行的呢?因為“某個特性條件”通常是在多個線程之間共享的某個變量。互斥鎖允許這個變量可以在不同的線程中設置和檢測。

通常,pthread_cond_wait只是喚醒等待某個條件變量的一個線程。如果需要喚醒所有等待某個條件變量的線程,需要調用:

int pthread_cond_broadcast (pthread_cond_t * cptr);

默認情況下面,阻塞的線程會一直等待,知道某個條件變量為真。如果想設置最大的阻塞時間可以調用:

int pthread_cond_timedwait (pthread_cond_t * cptr, pthread_mutex_t *mptr, const struct timespec *abstime);

如果時間到了,條件變量還沒有為真,仍然返回,返回值為ETIME。

pthread_cond_wait原理及調用方法:

pthread_cond_wait函數內部實現是:

先解鎖,然后再wait,等到觸發pthread_cond_t時,函數在返回前重新加鎖。

調用方法:

pthread_mutex_lock(&mutex); pthread_cond_wait(&cond,&mutex); 現在又加鎖了 do some thing pthread_mutex_unlock(&mutex);


6、試題最終代碼

通過前面的介紹,我們可以輕松的寫出代碼了,如下所示:

/*是否熟悉POSIX多線程編程技術?如熟悉,編寫程序完成如下功能:1)有一int型全局變量g_Flag初始值為0;2)在主線稱中起動線程1,打印“this is thread1”,并將g_Flag設置為13)在主線稱中啟動線程2,打印“this is thread2”,并將g_Flag設置為24)線程序1需要在線程2退出后才能退出5)主線程在檢測到g_Flag從1變為2,或者從2變為1的時候退出*/ #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<errno.h> #include<unistd.h>typedef void* (*fun)(void*);int g_Flag=0; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;void* thread1(void*); void* thread2(void*);/** when program is started, a single thread is created, called the initial thread or main thread.* Additional threads are created by pthread_create.* So we just need to create two thread in main().*/int main(int argc, char** argv) {printf("enter main\n");pthread_t tid1, tid2;int rc1=0, rc2=0;rc2 = pthread_create(&tid2, NULL, thread2, NULL);if(rc2 != 0)printf("%s: %d\n",__func__, strerror(rc2));rc1 = pthread_create(&tid1, NULL, thread1, &tid2);if(rc1 != 0)printf("%s: %d\n",__func__, strerror(rc1));pthread_cond_wait(&cond, &mutex);printf("leave main\n");exit(0); }/** thread1() will be execute by thread1, after pthread_create()* it will set g_Flag = 1;*/ void* thread1(void* arg) {printf("enter thread1\n");printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());pthread_mutex_lock(&mutex);if(g_Flag == 2)pthread_cond_signal(&cond);g_Flag = 1;printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());pthread_mutex_unlock(&mutex);pthread_join(*(pthread_t*)arg, NULL);printf("leave thread1\n");pthread_exit(0); }/** thread2() will be execute by thread2, after pthread_create()* it will set g_Flag = 2;*/ void* thread2(void* arg) {printf("enter thread2\n");printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());pthread_mutex_lock(&mutex);if(g_Flag == 1)pthread_cond_signal(&cond);g_Flag = 2;printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());pthread_mutex_unlock(&mutex);printf("leave thread2\n");pthread_exit(0); }

這篇博客也很好:http://blog.chinaunix.net/space.php?uid=312504&do=blog&id=2133185

由于工作站軟件的移植牽涉到這方面的內容, 搜一最近一直在學習Linux線程方面的知識!由于<Beginning? the Linux programming>這本書上沒有將條件變量的問題,所以不能理解的一個函數就是pthread_cond_wait().
??? 今天終于有點明白了,趕快記下心得!
??? 條件變量的結構為pthread_cond_t,函數pthread_cond_init()被用來初始化一個條件變量。它的原型為:

  extern int pthread_cond_init __P ((pthread_cond_t *__cond,__const pthread_condattr_t *__cond_attr));

  其中cond是一個指向結構pthread_cond_t的指針,cond_attr是一個指向結構pthread_condattr_t的指 針。結構 pthread_condattr_t是條件變量的屬性結構,和互斥鎖一樣我們可以用它來設置條件變量是進程內可用還是進程間可用,默認值是 PTHREAD_ PROCESS_PRIVATE,即此條件變量被同一進程內的各個線程使用。注意初始化條件變量只有未被使用時才能重新初始化或被釋放。釋放一個條件變量 的函數為pthread_cond_destroy(pthread_cond_t cond)。 

??? 也可以靜態的初始化條件變量

???? pthread_cond_t my_condition = PTHREAD_COND_INITIALIZER;

  函數pthread_cond_wait()使線程阻塞在一個條件變量上。它的函數原型為:

  extern int pthread_cond_wait __P ((pthread_cond_t *__cond,pthread_mutex_t *__mutex));

   調用這個函數時,線程解開mutex指向的鎖并被條件變量cond阻塞。線程可以被函數pthread_cond_signal和函數 pthread_cond_broadcast喚醒線程被喚醒后,它將重新檢查判斷條件是否滿足,如果還不滿足,一般說來線程應該仍阻塞在這里,被等待被下 一次喚醒。這個過程一般用while語句實現。

??? 通過下面的程序來理解:

__________________華麗的CODE分割線_________________________

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*初始化互斥鎖*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;? //初始化條件變量?

void *thread1(void *);
void *thread2(void *);

int i=1;
int main(void)
{
??? ??? pthread_t t_a;
??? ??? pthread_t t_b;
??? ??? pthread_create(&t_a,NULL,thread1,(void *)NULL);/*創建進程t_a*/
??? ??? pthread_create(&t_b,NULL,thread2,(void *)NULL); /*創建進程t_b*/
??? ??? pthread_join(t_b, NULL);/*等待進程t_b結束*/
??? ??? pthread_mutex_destroy(&mutex);
??? ??? pthread_cond_destroy(&cond);
??? ??? ?exit(0);
}

void *thread1(void *junk)
{
??? ??? for(i=1;i<=9;i++)
??? ??? {
??? ??? ???? pthread_mutex_lock(&mutex);//
??? ??? ??? ?if(i%3==0)
??? ??? ??? ???? pthread_cond_signal(&cond);/*條件改變,發送信號,通知t_b進程*/
??? ??? ????? else???????
??? ??? ????????? printf("thead1:%d\n",i);
??? ??? ????? pthread_mutex_unlock(&mutex);//*解鎖互斥量*/
??? ??? ??? ? printf("Up Unlock Mutex\n");??? ??
??? ??? ??? ? sleep(1);
??? ??? }

}

void *thread2(void *junk)
{
??? ??? while(i<9)
??? ??? {
??? ??? ??? ? pthread_mutex_lock(&mutex);

??? ??? ??? if(i%3!=0)
????????? ??? ??? ?pthread_cond_wait(&cond,&mutex);/*等待*/
??? ??????? printf("thread2:%d\n",i);
??? ??? ??? pthread_mutex_unlock(&mutex);
??? ??? ??? printf("Down Ulock Mutex\n");

??? ??? ??? sleep(1);
??? ??? ??? ??? }

}

___________________至關重要的絢爛的結果分割線_________________

thead1:1
Up Unlock Mutex
thead1:2
Up Unlock Mutex
Up Unlock Mutex
thread2:3
Down Ulock Mutex
thead1:4
Up Unlock Mutex
thead1:5
Up Unlock Mutex
Up Unlock Mutex
thread2:6
Down Ulock Mutex
thead1:7
Up Unlock Mutex
thead1:8
Up Unlock Mutex
Up Unlock Mutex
thread2:9
Down Ulock Mutex

_________________HOW IT WORKS________

i不是三的倍數的時候.

thread2條件變量阻塞,釋放Mutex

thread1加鎖,打印thread1:i,釋放鎖,打印"Up Unlock Mutex"

i為3的倍數的時候,

thread1,加鎖,條件變量通知,喚醒條件變量阻塞線程,打印"Up Unlock Mutex"

thread2,被喚醒,加鎖,打印"thread2:i",釋放鎖,打印"Down Ulock Mutex"


??? 所以說函數pthread_cond_wait()調用時,不僅對條件變量起作用,還對互斥鎖有作用!


與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的linux多线程基本编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。