線程的清理函數:pthread_cleanup_push / pthread_cleanup_pop

pthread_cleanup_push 爲塞進即註冊一個清理函數

pthread_cleanup_pop 爲彈出即取消一個清理函數

執行順序與註冊順序相反。

1.源代碼:

cat -n 11_5.c
     1  #include "apue.h"
     2  #include <pthread.h>
     3
     4  void cleanup(void *s)
     5  {
     6          printf("%s executed\n",(char *) s);
     7  }
     8
     9  void * td_func1(char * arg)
    10  {
    11          printf("execute thread1 function\n");
    12          pthread_cleanup_push(cleanup,"Thread 1 cleanup first");
    13          pthread_cleanup_push(cleanup,"Thread 1 cleanup second");
    14
    15          if (arg)
    16                  return ((void *)1);
    17
    18          pthread_cleanup_pop(0);
    19          pthread_cleanup_pop(0);
    20
    21          return ((void *)1);
    22  }
    23  void * td_func2(char * arg)
    24  {
    25          printf("execute thread2 function\n");
    26          pthread_cleanup_push(cleanup,"Thread 2 cleanup first");
    27          pthread_cleanup_push(cleanup,"Thread 2 cleanup second");
    28
    29          if (arg)
    30                  pthread_exit ((void *)2);
    31
    32          pthread_cleanup_pop(0);
    33          pthread_cleanup_pop(0);
    34
    35          pthread_exit ((void *)2);
    36  }
    37
    38
    39  int main()
    40  {
    41          int err;
    42          pthread_t tid1,tid2;
    43          void *tret;
    44
    45          err = pthread_create(&tid1,NULL,td_func1,(void *)0);
    46          if (err != 0)
    47                  err_exit(err,"Create thread 1 error");
    48
    49          err = pthread_create(&tid2,NULL,td_func2,(void *)2);
    50          if (err != 0)
    51                  err_exit(err,"Create thread 2 error");
    52
    53          err = pthread_join(tid1,&tret);
    54          if (err != 0)
    55                  err_exit(err,"Join thread 1 error");
    56          printf("Thread 1 exit successfully with code:%d\n",(int)tret);
    57
    58          err = pthread_join(tid2,&tret);
    59          if (err != 0)
    60                  err_exit(err,"Join thread 2 error");
    61          printf("Thread 2 exit successfully with code:%d\n",(int)tret);
    62
    63          return (0);
    64  }



2.編譯及運行結果:

gcc -Wall -ggdb3 11_5.c error.c -o cleanup -lpthread
11_5.c: In function `td_func2':
11_5.c:36: warning: control reaches end of non-void function
11_5.c: In function `main':
11_5.c:45: warning: passing arg 3 of `pthread_create' from incompatible pointer type
11_5.c:49: warning: passing arg 3 of `pthread_create' from incompatible pointer type
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'
<bldc:/home/tingbinz/apue.3e/SBSCODE/11>R*_*G:./cleanup
execute thread1 function
execute thread2 function
Thread 1 exit successfully with code:1
Thread 2 cleanup second executed
Thread 2 cleanup first executed
Thread 2 exit successfully with code:2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章