linux程式設計——取消一個線程(第十二章)

來源:互聯網
上載者:User

標籤:oid   perror   請求   etc   ble   read   void   name   other   

12.7    取消一個線程有時,想讓一個線程能夠要求還有一個線程終止,就像給它發送一個訊號一樣。

線程有方法能夠做到這一點,與與訊號處理一樣。線程能夠被要求終止時改變其行為。
pthread_cancel是用於請求一個線程終止的函數:

#inlude <pthread.h>int pthread_cancel(pthread_t thread);
這個函數提供一個線程標識符就能夠發送請求來取消它。
線程能夠用pthread_setcancelstate設定線程的取消狀態
#include <pthread.h>int pthread_setcancelstate(int state, int *oldstate);
第一個參數的取值能夠是PTHREAD_CANCEL_ENABLE,這個值同意線程接收取消請求;或者是PTHREAD_CANCEL_DISABLE,它的作用是忽略取消請求。oldstate指標用於擷取先前的取消狀態。
假設取消請求被接受了,線程就能夠進入第二個控制層次,用pthread_setcanceltype設定取消類型
#include <pthread.h>int pthread_setcanceltype(int type, int *oldtype);
type參數能夠有兩種取值:一個是PTHREAD_CANCEL_ASYNCHRONOUR,它將使得在接收到取消請求後馬上採取行動;還有一個是PTHREAD_CANCEL_DEFERRED,它將使得在接受到取消請求後,一直等待直到線程運行下述函數之中的一個才採取行動。詳細是函數pthread_join,pthread_cond_wait,pthread_cond_timedwait,pthread_testcancel,sem_wait或sigwait.
編敲代碼thread7.c,主線程向它建立的線程發送一個取消請求。


/************************************************************************* > File Name:    thread7.c > Description:  thread7.c程式在主線程中向它建立的新線程發送一個取消請求 > Author:       Liubingbing > Created Time: 2015/7/7 9:58:40 > Other:        thread7.c程式中新線程的調用函數中分別須要設定新線程的取消狀態和取消類型 ************************************************************************/#include<stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>void *thread_function(void *arg);int main(){int res;pthread_t a_thread;void *thread_result;/* pthread_create函數建立新線程,新線程標識符儲存在a_thread。新線程調用的函數為thread_function,函數的參數為NULL */res = pthread_create(&a_thread, NULL, thread_function, NULL);if (res != 0) {perror("Thread creation failed");exit(EXIT_FAILURE);}sleep(3);printf("Canceling thread...\n");/* pthread_cancel函數請求線程a_thread終止 */res = pthread_cancel(a_thread);if (res != 0) {perror("Thread cancelation failed");exit(EXIT_FAILURE);}printf("Waiting for thread to finish...\n");/* pthread_join等待線程a_thread與主線程又一次合并 */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;/* pthread_setcancelstate函數設定線程的取消狀態,PTHREAD_CANCEL_ENABLE同意線程接收取消請求。PTHREAD_CANCEL_DISABLE忽略取消請求 */res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);if (res != 0) {perror("Thread pthread_setcancelstate failed");exit(EXIT_FAILURE);}/* pthread_setcanceltype函數設定線程的取消類型,PTHREAD_CANCEL_DEFERRED將使得在接收到取消請求後。一直等待直到線程運行某個函數(如pthread_join)之後才採取行動 */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);}
以通常的方法建立新線程後,主線程休眠一會兒(好讓新線程有時間開始運行),然後發送一個取消請求。例如以下所看到的:
sleep(3);printf("Canceling thread...\n");res = pthread_cancel(a_thread);
在新建立的線程中。首先將取消狀態設定為同意取消,例如以下所看到的:
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
然後將取消類型設定為延遲取消,例如以下所看到的:
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
最後。線程在迴圈中等待被取消,例如以下所看到的:
for (i = 0; i < 10; i++) {    printf("Thread is still running (%d)...\n", i);    sleep(1);}


linux程式設計——取消一個線程(第十二章)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.