線程解析(二)
作者:曹忠明,華清遠(yuǎn)見嵌入式學(xué)院講師。
上次說了如何去創(chuàng)建一個(gè)線程及如何向線程中傳遞一個(gè)參數(shù)那么這次我們說一下如何終止一個(gè)線程。
如進(jìn)程中調(diào)用exit,_exit使進(jìn)程結(jié)束一樣,線程也可以通過類似的方法結(jié)束。
一、線程的執(zhí)行體結(jié)束線程自然結(jié)束。
二、可以通過調(diào)用pthread_exit使程序結(jié)束。
pthread_exit 的原型為:
void pthread_exit(void *value_ptr);
value_ptr是一個(gè)無類型返回值,其他線程可以通過調(diào)用phread_join來獲得這個(gè)值。
phread_join 的原型為:
int pthread_join(pthread_t thread, void **value_ptr);
調(diào)用pthread_join可以等待線程thread的退出并獲得退出時(shí)的狀態(tài),如果不關(guān)心線程返回值的話,value_ptr可以置為NULL,下面我們用一個(gè)例程說明這兩個(gè)函數(shù)的使用方法。
#include <stdio.h>
#include <pthread.h>
void *thread_a(void *arg)
{
printf("thread 1 entern");
pthread_exit((void *)1);
}
void *thread_b(void *arg)
{
printf("thread 2 entern");
pthread_exit((void *)2);
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b;
int err;
void *value_ptr;
err = pthread_create(&tid_a,NULL,thread_a,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
err = pthread_create(&tid_b,NULL,thread_b,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
pthread_join(tid_b,&value_ptr);
printf("phtread %d exit!n",(int)value_ptr);
pthread_join(tid_a,&value_ptr);
printf("phtread %d exit!n",(int)value_ptr);
sleep(5);
printf("the main closen");
return 0;
}
三、被統(tǒng)一線程中的其他線程取消而結(jié)束。
線程可以通過調(diào)用pthread_cancel函數(shù)向同一進(jìn)程中的其他線程發(fā)送取消的信號(hào),但是這個(gè)先好的響應(yīng)可以設(shè)定,可以設(shè)置為立即終止或忽略。所以發(fā)送取消信號(hào)并不意味著線程就會(huì)終止。
與線程取消相關(guān)的函數(shù)有:
phtread_cancel原型為:
int pthread_cancel(pthread_t thread);
這個(gè)函數(shù)向線程thread發(fā)送終止信號(hào)。
pthread_setcancelstate原型為:
int pthread_setcancelstate(int state, int *oldstate);
這個(gè)函數(shù)設(shè)定線程接收到終止信號(hào)的反應(yīng),state有兩種值:PHTREAD_CANCEL_ENABLE(默認(rèn)為這個(gè)狀態(tài))和PHREAD_CANCEL_DISABLE,這兩個(gè)值分別代表接受掉終止信號(hào)終止線程和或略這個(gè)信號(hào)。oldstate用來存放線程原來的狀態(tài)用來以后恢復(fù)只用,如不用回復(fù)可以設(shè)置為NULL。
pthread_setcanceltype原型為:
int pthread_setcanceltype(int type, int *oldtype);
這個(gè)函數(shù)用來設(shè)定線程接收到終止信號(hào)的執(zhí)行時(shí)間,type也是有兩個(gè)值:PTHREAD_CANCEL_DEFFERED和PTHREAD_CANCEL_ASYCHRONOUS,這兩個(gè)值分別表示線程接收到終止信號(hào)是運(yùn)行到下一個(gè)取消點(diǎn)退出還是立即退出。同樣oldtype也是用來存放線程原來的狀態(tài)。
pthread_testcancel原型為:
void pthread_testcancel(void);
檢查線程是否處于canceld狀態(tài)如果是則執(zhí)行取消動(dòng)作否則立即返回。
下面用一個(gè)例程說明這幾個(gè)函數(shù)的使用。
#include <stdio.h>
#include <pthread.h>
void *thread_a(void *arg)
{
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL) != 0)
{
perror("setcancelstate");
}
if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL) != 0)
{
perror("setcancelsype");
}
while(1)
{
sleep(1);
printf("thread_an");
pthread_testcancel();
}
}
void *thread_b(void *arg)
{
if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL) != 0)
{
perror("setcancelstate");
}
while(1)
{
sleep(1);
printf("thread_bn");
pthread_testcancel();
}
}
void *thread_c(void *arg)
{
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL) != 0)
{
perror("setcancelstate");
}
if(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL) != 0)
{
perror("setcancelsype");
}
while(1)
{
sleep(1);
printf("thread_cn");
pthread_testcancel();
}
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b,tid_c;
int err;
err = pthread_create(&tid_a,NULL,thread_a,(void *)&tid_a);
if(err < 0)
{
perror("pthread_create thread_a");
}
printf("create tid_a = %un",tid_a);
err = pthread_create(&tid_b,NULL,thread_b,(void *)&tid_b);
if(err < 0)
{
perror("pthread_create thread_b");
}
printf("create tid_b = %un",tid_b);
err = pthread_create(&tid_c,NULL,thread_c,(void *)&tid_c);
if(err < 0)
{
perror("pthread_create thread_c");
}
printf("create tid_c = %un",tid_c);
sleep(5);
if(pthread_cancel(tid_a) != 0)
{
perror("pthread_cancel tid_a");
}
sleep(5);
if(pthread_cancel(tid_b) != 0)
{
perror("pthread_cancel tid_b");
}
sleep(5);
if(pthread_cancel(tid_c) != 0)
{
perror("pthread_cancel tid_c");
}
sleep(30);
printf("the main closen");
return 0;
}
“本文由華清遠(yuǎn)見http://www.embedu.org/index.htm提供”
來源:華清遠(yuǎn)見0次