pthread cancel 使用方法1

2021-05-24 22:19:44 字數 4471 閱讀 3914

#include

#include

#define  failure  0

#define  success  1

#define  uns32  unsigned int

#define m_error(format, args...)   printf(format, ## args);//fflush(stdout);

static pthread_t   g_pthread_wait;

static pthread_t   g_pthread_signal;

static pthread_t   g_pthread_mt;

pthread_mutex_t         g_pthread_mutex;

pthread_cond_t          g_pthread_cond;

uns32 pthread_initialize_cond(void) 

return success;

}uns32 pthread_initialize_mutex(void) 

return success;

}void pthread_wait(void *args)

pthread_exit(null); 

}void pthread_signal(void *args)

void pthread_mt(void *args)

int main()

sleep(1);

if (pthread_create(&g_pthread_signal , null, (void * (*)(void *)) pthread_signal, null) !=0)

sleep(5);

if (pthread_create(&g_pthread_mt , null, (void * (*)(void *)) pthread_mt, null) !=0)

printf("while!/n");

while(1);

}pthread_cancel(g_pthread_wait);的作用是將pthread_wait執行緒殺掉。

列印結果為:

pthread_cond_wait_in

pthread_cond_signal_out

pthread_cond_wait_out

wait_while

wait_while

wait_while

wait_while

wait_while

while!

pthread_cancel_in

pthread_cancel_out

pthread_mt_lock

如果將pthread_mutex_unlock(&g_pthread_mutex);注釋掉的話,那麼就不會列印出pthread_mt_lock這條語句。

分析:

1.pthread_wait先執行,列印pthread_cond_wait_in後,pthread_cond_wait函式被呼叫,等待g_pthread_cond條件,並解

鎖;2.sleep 1秒後,pthread_signal執行,pthread_cond_signal函式被呼叫後,列印pthread_cond_signal_out,執行緒退出,則

pthread_cond_wait被喚醒,加鎖之後進入while迴圈;

3.sleep 5秒後,pthread_mt執行,列印pthread_cancel_in 後,呼叫pthread_cancel將pthread_wait執行緒殺掉,因為

pthread_cond_wait返回時,加鎖了,所以pthread_wait被殺死之後,鎖沒有被解開,如果不呼叫

pthread_mutex_unlock解鎖,下面的加鎖函式就阻塞了,因此就列印不出pthread_mt_lock了。

補充:

被殺執行緒可以使用如下**,控制本執行緒被殺死時的動作

int state = 0;      

int type = 0;

pthread_setcancelstate(pthread_cancel_enable, &state);

pthread_setcanceltype(pthread_cancel_asynchronous, &type);

pthread_setcancelstate函式

pthread_setcancelstate函式的作用是設定本執行緒是否允許被其他執行緒關閉,原型如下:

int pthread_setcancelstate(int state, int *oldstate);

引數state:可以取值pthread_cancel_enable或pthread_cancel_disable,前者表示允許被其他執行緒關閉,後者表示不允許,預設為前者;

引數oldstate:返回設定之前的本屬性值;

返回值:0成功,非0失敗;

pthread_setcanceltype函式

pthread_setcanceltype函式的作用是設定本執行緒被其他執行緒關閉時,以什麼方式關閉,原型如下:

int pthread_setcanceltype(int type, int *oldtype);

引數type:pthread_cancel_asynchronous或pthread_cancel_deferred,前者表示被立刻關閉,後者表示等執行緒被阻塞時再關閉,預設為後者;

引數oldtype:返回設定之前的本屬性值;

返回值:0成功,非0失敗;

例子:#include

#include

#include

#include

void *thread_function(void *arg);

int main() {

int res;

pthread_t a_thread;

void *thread_result;

res = pthread_create(&a_thread, null, thread_function, null);

if (res != 0) {

perror("thread creation failed");

exit(exit_failure);

sleep(3);

printf("cancelling thread.../n");

res = pthread_cancel(a_thread);

if (res != 0) {

perror("thread cancelation failed");

exit(exit_failure);

printf("waiting for thread to finish.../n");

res = pthread_join(a_thread, &thread_result);

if (res != 0) {

perror("thread join failed");

exit(exit_failure);

exit(exit_success);

void *thread_function(void *arg) {

int i, res;

res = pthread_setcancelstate(pthread_cancel_enable, null);

if (res != 0) {

perror("thread pthread_setcancelstate failed");

exit(exit_failure);

res = pthread_setcanceltype(pthread_cancel_deferred, null);

if (res != 0) {

perror("thread pthread_setcanceltype failed");

exit(exit_failure);

printf("thread_function is running/n");

for(i = 0; i < 10; i++) {

printf("thread is still running (%d).../n", i);

sleep(1);

pthread_exit(0);

執行結果:

thread_function is running

thread is still running (0)...

thread is still running (1)...

thread is still running (2)...

cancelling thread...

waiting for thread to finish...

thread is still running (3)...

執行緒取消 pthread cancel

基本概念 pthread cancel呼叫並不等待執行緒終止,它只提出請求。執行緒在取消請求 pthread cancel 發出後會繼續執行,直到到達某個取消點 cancellationpoint 取消點是執行緒檢查是否被取消並按照請求進行動作的乙個位置.與執行緒取消相關的pthread函式 int...

執行緒pthread cancel 函式

功能 呼叫執行緒終止同程序中,其他的執行緒,呼叫該方法後,被終止的執行緒並不一定立馬被終止,只有在下次系統呼叫或呼叫了pthread testcancel 方法後,才真正終止執行緒 原型 int pthread cancel pthread t pid include includevoid chi...

pthread cancel 完美退出執行緒

pthread cancel 完美退出執行緒 程式 include include include include void cleanup void void test pthread void pthread cleanup pop 0 return null int main sleep 1 ...