有時(shí)候我們希望線程退出時(shí)能夠自動(dòng)的執(zhí)行某些函數(shù),為了能達(dá)到此目的,OS 提供了兩個(gè)函數(shù)幫我們完成這個(gè)功能:
void pthread_cleanup_push(void (*rtn)(void*), void *arg);void pthread_cleanup_pop(int execute);如果想要你的線程在退出時(shí)能夠執(zhí)行清理函數(shù),你需要使用 pthread_cleanup_push 對(duì)你的清理函數(shù)進(jìn)行注冊(cè),如下:
在 linux 中,pthread_cleanup_push 和 pthread_cleanup_pop 這兩個(gè)函數(shù)是通過宏來(lái)做的,pthread_cleanup_push 被替換成以左花括號(hào) { 為開頭的一段代碼,而 pthread_cleanup_pop 被替換成以右花括號(hào) } 結(jié)尾的一段代碼,這就意味著這兩個(gè)函數(shù)必須要成對(duì)出現(xiàn)才能將左右花括號(hào)匹配上,否則就出現(xiàn)編譯錯(cuò)誤。
有些平臺(tái)可能不是使用宏來(lái)實(shí)現(xiàn),就算不成對(duì)也沒什么關(guān)系。
有三種情況線程清理函數(shù)會(huì)被調(diào)用:
線程還未執(zhí)行 pthread_cleanup_pop 前,被 pthread_cancel 取消線程還未執(zhí)行 pthread_cleanup_pop 前,主動(dòng)執(zhí)行 pthread_exit 終止線程執(zhí)行 pthread_cleanup_pop,且 pthread_cleanup_pop 的參數(shù)不為 0.注意:如果線程還未執(zhí)行 pthread_cleanup_pop 前通過 return 返回,是不會(huì)執(zhí)行清理函數(shù)的。
程序 clean 需要傳入兩個(gè)參數(shù),第 1 個(gè)參數(shù)表示是否提前返回(在執(zhí)行 pthread_cleanup_pop 前返回),第 2 個(gè)參數(shù)表示 pthread_cleanup_pop 的參數(shù)。所以有 4 種組合情況。
代碼// clean.c#include <unistd.h>#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define ERR(name,err) do{
圖1 運(yùn)行結(jié)果 從圖 1 中的結(jié)果可以看到:
當(dāng) clean 程序中的線程正常返回時(shí),只有 pthread_cleanup_pop 的參數(shù)非 0 時(shí),才會(huì)正常執(zhí)行清理函數(shù)。當(dāng) clean 程序中的線程在執(zhí)行 pthread_cleanup_pop 前時(shí),使用 pthread_exit 退出時(shí),清理函數(shù)才會(huì)被執(zhí)行,和 pthread_cleanup_pop 的參數(shù)沒有關(guān)系。而使用 return 返回的線程 1 并不會(huì)執(zhí)行清理函數(shù)。清理函數(shù)的執(zhí)行順序,是按照注冊(cè)時(shí)候相反的順序執(zhí)行的。注意,在有些系統(tǒng)中(FreeBSD/Mac OS X),提前終止可能會(huì)出現(xiàn)段錯(cuò)誤。
練習(xí):完成本文中的實(shí)驗(yàn),編譯運(yùn)行并查看結(jié)果。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注