1. 程式人生 > >C語言多執行緒pthread庫-互斥鎖

C語言多執行緒pthread庫-互斥鎖

執行緒相關操作說明

  一 pthread_t

  pthread_t在標頭檔案/usr/include/bits/pthreadtypes.h中定義:

  typedef unsigned long int pthread_t;

  它是一個執行緒的識別符號。

  二 pthread_create

  函式pthread_create用來建立一個執行緒,它的原型為:

  extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,

  void *(*__start_routine) (void *), void *__arg));

  第一個引數為指向執行緒識別符號的指標,第二個引數用來設定執行緒屬性,第三個引數是執行緒執行函式的起始地址,最後一個引數是執行函式的引數。這裡,我們的函式thread不需要引數,所以最後一個引數設為空指標。第二個引數我們也設為空指標,這樣將生成預設屬性的執行緒。對執行緒屬性的設定和修改我們將在下一節闡述。當建立執行緒成功時,函式返回0,若不為0則說明建立執行緒失敗,常見的錯誤返回程式碼為EAGAIN和EINVAL.前者表示系統限制建立新的執行緒,例如執行緒數目過多了;後者表示第二個引數代表的執行緒屬性值非法。建立執行緒成功後,新建立的執行緒則執行引數三和引數四確定的函式,原來的執行緒則繼續執行下一行程式碼。

  三 pthread_join pthread_exit

  函式pthread_join用來等待一個執行緒的結束。函式原型為:

  extern int pthread_join __P ((pthread_t __th, void **__thread_return));

  第一個引數為被等待的執行緒識別符號,第二個引數為一個使用者定義的指標,它可以用來儲存被等待執行緒的返回值。這個函式是一個執行緒阻塞的函式,呼叫它的函式將一直等待到被等待的執行緒結束為止,當函式返回時,被等待執行緒的資源被收回。一個執行緒的結束有兩種途徑,一種是象我們上面的例子一樣,函式結束了,呼叫它的執行緒也就結束了;另一種方式是通過函式pthread_exit來實現。它的函式原型為:

  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));

  唯一的引數是函式的返回程式碼,只要pthread_join中的第二個引數thread_return不是NULL,這個值將被傳遞給 thread_return.最後要說明的是,一個執行緒不能被多個執行緒等待,否則第一個接收到訊號的執行緒成功返回,其餘呼叫pthread_join的執行緒則返回錯誤程式碼ESRCH.

  在這一節裡,我們編寫了一個最簡單的執行緒,並掌握了最常用的三個函式pthread_create,pthread_join和pthread_exit.下面,我們來了解執行緒的一些常用屬性以及如何設定這些屬性。

  互斥鎖相關

  互斥鎖用來保證一段時間內只有一個執行緒在執行一段程式碼。

  一 pthread_mutex_init

  函式pthread_mutex_init用來生成一個互斥鎖。NULL引數表明使用預設屬性。如果需要宣告特定屬性的互斥鎖,須呼叫函式 pthread_mutexattr_init.函式pthread_mutexattr_setpshared和函式 pthread_mutexattr_settype用來設定互斥鎖屬性。前一個函式設定屬性pshared,它有兩個取值, PTHREAD_PROCESS_PRIVATE和PTHREAD_PROCESS_SHARED.前者用來不同程序中的執行緒同步,後者用於同步本程序的不同執行緒。在上面的例子中,我們使用的是預設屬性PTHREAD_PROCESS_ PRIVATE.後者用來設定互斥鎖型別,可選的型別有PTHREAD_MUTEX_NORMAL、PTHREAD_MUTEX_ERRORCHECK、 PTHREAD_MUTEX_RECURSIVE和PTHREAD _MUTEX_DEFAULT.它們分別定義了不同的上所、解鎖機制,一般情況下,選用最後一個預設屬性。

  二 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np

  pthread_mutex_lock宣告開始用互斥鎖上鎖,此後的程式碼直至呼叫pthread_mutex_unlock為止,均被上鎖,即同一時間只能被一個執行緒呼叫執行。當一個執行緒執行到pthread_mutex_lock處時,如果該鎖此時被另一個執行緒使用,那此執行緒被阻塞,即程式將等待到另一個執行緒釋放此互斥鎖。

  下面先來一個例項。我們通過建立兩個執行緒來實現對一個數的遞加。

  view source

  print?

  01 #include

  02 #include

  03 #include

  04 #include

  05 #define MAX 10

  06 pthread_t thread[2];

  07 pthread_mutex_t mut;

  08 int number=0, i;

  09 void *thread1()

  10 {

  11 printf ("thread1 : I'm thread 1\n");

  12 for (i = 0; i < MAX; i++)

  13 {

  14 printf("thread1 : number = %d\n",number);

  15 pthread_mutex_lock(&mut);

  16 number++;

  17 pthread_mutex_unlock(&mut);

  18 sleep(2);

  19 }

  20 printf("thread1 :主函式在等我完成任務嗎?\n");

  21 pthread_exit(NULL);

  22 }

  23 void *thread2()

  24 {

  25 printf("thread2 : I'm thread 2\n");

  26 for (i = 0; i < MAX; i++)

  27 {

  28 printf("thread2 : number = %d\n",number);[nextpage]

  29 pthread_mutex_lock(&mut);

  30 number++;

  31 pthread_mutex_unlock(&mut);

  32 sleep(3);

  33 }

  34 printf("thread2 :主函式在等我完成任務嗎?\n");

  35 pthread_exit(NULL);

  36 }

  37 void thread_create(void)

  38 {

  39 int temp;

  40 memset(&thread, 0, sizeof(thread)); //comment1

  41 /*建立執行緒*/

  42 if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2

  43 printf("執行緒1建立失敗!\n");

  44 else

  45 printf("執行緒1被建立\n");

  46 if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3

  47 printf("執行緒2建立失敗");

  48 else

  49 printf("執行緒2被建立\n");

  50 }

  51 void thread_wait(void)

  52 {

  53 /*等待執行緒結束*/

  54 if(thread[0] !=0) { //comment4

  55 pthread_join(thread[0],NULL);

  56 printf("執行緒1已經結束\n");

  57 }

  58 if(thread[1] !=0) { //comment5

  59 pthread_join(thread[1],NULL);

  60 printf("執行緒2已經結束\n");

  61 }

  62 }

  63 int main()

  64 {

  65 /*用預設屬性初始化互斥鎖*/

  66 pthread_mutex_init(&mut,NULL);

  67 printf("我是主函式哦,我正在建立執行緒,呵呵\n");

  68 thread_create();

  69 printf("我是主函式哦,我正在等待執行緒完成任務阿,呵呵\n");

  70 thread_wait();

  71 return 0;

  72 }

  下面我們先來編譯、執行一下

  引文:

  [email protected]:~/program/c/code/ftp$ gcc -lpthread -o thread_example thread_example.c

  [email protected]:~/program/c/code/ftp$ ./thread_example

  我是主函式哦,我正在建立執行緒,呵呵

  執行緒1被建立

  執行緒2被建立

  我是主函式哦,我正在等待執行緒完成任務阿,呵呵

  thread1 : I'm thread 1

  thread1 : number = 0

  thread2 : I'm thread 2

  thread2 : number = 1

  thread1 : number = 2

  thread2 : number = 3

  thread1 : number = 4

  thread2 : number = 5

  thread1 : number = 6

  thread1 : number = 7

  thread2 : number = 8

  thread1 : number = 9

  thread2 : number = 10

  thread1 :主函式在等我完成任務嗎?

  執行緒1已經結束

  thread2 :主函式在等我完成任務嗎?

  執行緒2已經結束