<UNIX環境高級編程(第二版)> 線程學習P287-P297
#include <pthread.h> //新建線程 int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg); //線程終止 void pthread_exit(void *rval_ptr);//線程自身主動退出 int pthread_join(pthread_t tid, void **rval_ptr);//其他線程阻塞自身,等待tid退出 //線程清理 void pthread_cleanup_push(void (*rtn)(void *), void *arg); void pthread_cleanup_pop(int execute);
補充說明:
1. 線程創建
pthread_create()函數返回值0,表示創建成功,線程id保存載tidp中;失敗則返回非零,需自行處理,不會修改errno值
2. 線程終止
a. 任一線程調用exit, _Exit, _exit都將導致整個進程終止;
b. 單個線程退出方式有三種:
1> 線程執行函數start_rtn()中使用return返回,返回值為線程退出碼;
2> 被同一個進程的其他線程使用pthread_cancel()取消;
3> 線程自身調用了pthread_exit();
說明:pthread_join(pthread_t tid, void **rval_ptr)函數會阻塞調用線程,直到tid線程通過上述三種方式終止退出,且return/pthread_exit()方式會設置相應線程退出碼rval_ptr,而pthread_cancel()取消的線程,將退出碼設置為PTHREAD_CANCELED.
3. 線程清理處理程序(thread cleanup handler)
3.a> pthread_cleanup_push()與pthread_cleanup_pop()均為<pthread.h>中實現的宏定義,具體實現如下:
pthread_cleanup_push and pthread_cleanup_pop are macros and must always
be used in matching pairs at the same nesting level of braces. */
# define pthread_cleanup_push(routine, arg) \
do { \
__pthread_cleanup_class __clframe (routine, arg)
/* Remove a cleanup handler installed by the matching pthread_cleanup_push.
If EXECUTE is non-zero, the handler function is called. */
# define pthread_cleanup_pop(execute) \
__clframe.__setdoit (execute); \
} while (0)
可見push/pop中的{/}是一一對應的,因此pthread_cleanup_push/pop()也應一一對應出現,否則編譯出錯。
3.b> 當線程執行下列之一操作時調用清理函數,thread_cleanup_push由棧結構實現,注意清理程序調用的順序,先入后出。
1: 調用pthread_exit()時,而直接return不會出發清理函數;
2: 相應取消請求pthread_cancel()時;
3: 使用非零execute參數調用pthread_cleanup_pop()時;
尤其需注意pthread_cleanup_pop()參數不同及此語句所處位置不同而有不同效果。
看此代碼實例,注意return或pthread_exit()位置不同導致pthread_cleanup_pop()不同參數的效果變化。
#include <pthread.h>
void testPointerSize()
{
void *tret;
printf("size of pointer in x86-64:%d\n",sizeof(tret));
//result is 8 in x86-64.
//which is 4 in x86-32.
printf("size of int in x86-64:%d\n",sizeof(int));
//result is 4 in x86-64.
//which is also 4 in x86-32.
}
void cleanup(void *arg)
{
printf("cleanup:%s\n",(char *)arg);
}
void * thr_fn1(void *arg)
{
printf("thread 1 start\n");
pthread_cleanup_push(cleanup, "thread 1 first handler");
pthread_cleanup_push(cleanup, "thread 1 second handler");
if(arg)
return ((void *)1);//arg !=0 ,return here.
// return here will not triger any cleanup.
pthread_cleanup_pop(0);
pthread_cleanup_pop(1);
return ((void *)2);//will not run this
}
void * thr_fn2(void *arg)
{
printf("thread 2 start\n");
pthread_cleanup_push(cleanup, "thread 2 first handler");
pthread_cleanup_push(cleanup, "thread 2 second handler");
pthread_cleanup_pop(0);
pthread_cleanup_pop(1);
return ((void *)2);
// return here can triger cleanup second handler;
}
void * thr_fn3(void *arg)
{
printf("thread 3 start\n");
pthread_cleanup_push(cleanup, "thread 3 first handler");
pthread_cleanup_push(cleanup, "thread 3 second handler");
if(arg)
pthread_exit((void *)3);
//pthread_exit() here will triger both cleanup first&second handler.
pthread_cleanup_pop(1);
pthread_cleanup_pop(0);
pthread_exit((void *)3);//wont run this
}
void * thr_fn4(void *arg)
{
printf("thread 4 start\n");
pthread_cleanup_push(cleanup, "thread 4 first handler");
pthread_cleanup_push(cleanup, "thread 4 second handler");
pthread_cleanup_pop(1);
pthread_cleanup_pop(0);
pthread_exit((void *)4);
//pthread_exit() here will triger cleanup second handler.
}
int main(void)
{
testPointerSize();
int err;
pthread_t tid1, tid2, tid3, tid4;
void *tret;
err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
err = pthread_join(tid1,&tret);
printf("thread 1 exit code %d\n",(int)tret);
err = pthread_create(&tid2, NULL, thr_fn2, (void *)2);
err = pthread_join(tid2, &tret);
printf("thread 2 exit code %d\n",(int)tret);
err = pthread_create(&tid3, NULL, thr_fn3, (void *)3);
err = pthread_join(tid3,&tret);
printf("thread 3 exit code %d\n",(int)tret);
err = pthread_create(&tid4, NULL, thr_fn4, (void *)4);
err = pthread_join(tid4, &tret);
printf("thread 4 exit code %d\n",(int)tret);
}
運行結果:
[root@hello testData]# ./test size of pointer in x86-64:8 size of int in x86-64:4 thread 1 start thread 1 exit code 1 thread 2 start cleanup:thread 2 first handler thread 2 exit code 2 thread 3 start cleanup:thread 3 second handler cleanup:thread 3 first handler thread 3 exit code 3 thread 4 start cleanup:thread 4 second handler thread 4 exit code 4
由上述測試程序總結如下:
1> push與pop間的return,將導致清理程序不被觸發;
2> 位於pop之后return,由pop的參數確定是否觸發清理程序,非零參數觸發,零參數不觸發;
3> push/pop間的pthread_exit(),將觸發所有清理函數;
4>位於pop之后的pthread_exit()時,pop參數決定是否觸發清理程序;
其實,上述四種情況只是測試驗證了前文3.b所說三個條件,加深理解。
參考文獻:
2. <UNIX環境高級編程(第2版)> P295-296程序
3. pthread_cleanup_push()/pthread_cleanup_pop()的詳解
4. Linux中vim的列編輯實例 (Mark記錄)
