linux程式設計——線程的屬性(第十二章)

來源:互聯網
上載者:User

標籤:

12.6    線程的屬性在前面的所有程式樣本中,都在程式退出之前用pthread_join對線程再次進行同步,如果想讓線程向建立它的線程返回資料就需要這樣做.但有時,也有這種情況,既不需要第二個線程向主線程返回資訊,也不想讓主線程等待它的結束.
假設在主線程繼續為使用者提供服務的同時建立了第二個線程,新線程的作用是將使用者正在編輯的資料檔案進行備份儲存.備份工作結束後,第二個線程就可以直接終止了,它沒有必要再回到主線程中.
可以建立這一類型的線程,它們被稱為脫離線程(detached thread).可以通過修改線程屬性或調用pthread_detach的方法來建立它們
.這節是介紹線程的屬性,因此在這裡就使用修改線程屬性的方法.
需要用到的最重要的函數是pthread_attr_init,它作用是初始化一個線程屬性對象.
#include <pthread.h>int pthread_attr_init(pthread_attr_t *attr);
還有一個回收函數pthread_attr_destroy,它的目的是對屬性對象進行清理和回收.一旦對象被回收了,除非它被重新初始化,否則就不能被再次利用.
初始化一個線程屬性對象後,可以調用許多其他的函數來設定不同的屬性行為.
常見的屬性如下:
detachedstate:這個屬性允許對線程進行重新合并.它以一個屬性指標和一個標誌為參數來確定需要的狀態,預設標誌值為PTHREAD_CREATE_JOINABLE允許兩個線程重新合并,另一個標誌PTHREAD_CREATE_DETACHED不允許調用pthread_join來獲得另一個線程的退出狀態.
schedpolicy:這個屬性控制線程的調度方式.它的取值可以是SHED_OTHER,SCHED_RP和SCHED_FIFO
schedparam:這個屬性是和schedpolicy屬性結合使用的,它可以對SCHED_OTHER策略啟動並執行線程的調度進行控制
inheritsched:這個屬性可以取PTHREAD_EXPLICIT_SCHED和PTHREAD_INHERIT_SCHED.前者表示調度由屬性明確地設定,後者表示新線程將沿用其建立者所使用的參數.
scope:這個屬性控制一個線程調度的計算方式.
stacksize:這個屬性控制線程建立的棧大小,單位為位元組.
1.設定脫離狀態屬性編寫脫離線程樣本程式thread5.c.建立一個線程屬性,將其設定為脫離狀態,然後用這個屬性建立一個線程.子線程結束時,它照常調用pthread_exit,但是這次,原來的線程不再等待它建立的子線程重新合并.主線程通過一個簡單的thread_finished標誌來檢測子線程是否已經結束,併線程線程之間仍然共用著變數.
/************************************************************************* > File Name:    thread5.c > Description:  thread5.c程式建立一個線程屬性thread_attr,並將其設定為脫離狀態,然後建立一個新線程,設定屬性為thread_attr. > Author:       Liubingbing > Created Time: 2015年07月06日 星期一 20時36分36秒 > Other:        thread5.c程式與之前程式的不同在於,原來的線程不再等待與它建立的新線程合并.主線程通過thread_finish標誌來檢測新線程是否結束. ************************************************************************/#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>/* 新線程調用的函數 */void *thread_function(void *arg);char message[] = "hello world";int thread_finished = 0;int main(){int res;pthread_t a_thread;/* 線程屬性對象thread_attr */pthread_attr_t thread_attr;/* pthread_attr_init初始化一個線程屬性對象 */res = pthread_attr_init(&thread_attr);if (res != 0) {perror("Attribute creation failed");exit(EXIT_FAILURE);}/* pthread_attr_setdetachstate設定屬性,PTHREAD_CREATE_DETACHED不允許調用pthread_join獲得另一個線程的退出狀態 */res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);if (res != 0) {perror("Setting detached attribute failed");exit(EXIT_FAILURE);}/* pthread_create建立一個新線程 * 第一個參數athread中存放新線程的標識符 * 第二個參數thread_attr為線程屬性,上面的代碼就是設定這個對象 * 第三個參數thread_function為新線程將要調用的函數  * 第四個參數(void *)message為傳遞給將要調用函數的參數 */res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);if (res != 0) {perror("Thread creation failed");exit(EXIT_FAILURE);}/* pthread_attr_destroy函數對線程屬性對象thread_attr進行清理和回收 */(void)pthread_attr_destroy(&thread_attr);/* 等待新線程函數設定thread_finished=1 */while (!thread_finished) {printf("Waiting for thread to say it's finished...\n");sleep(1);}printf("Other thread finished, bye!\n");exit(EXIT_SUCCESS);}void *thread_function(void *arg) {printf("thread_function is running. Argument was %s\n", (char *)arg);sleep(4);printf("Second thread setting finished flag, and exiting now\n");thread_finished = 1;pthread_exit(NULL);}
這個程式有兩段比較重要的代碼,第一段代碼是:
pthread_attr_t thread_attr;res = pthread_attr_init(&thread_attr);
它表明了一個線程屬性並對其進行初始化,第二段代碼是:
res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
它把屬性的值設定為脫離狀態.
2.線程屬性--調度該變調度屬性和設定脫離狀態非常相似,可以用sched_get_priority_max和sched_get_priority_min這兩個函數來尋找可用的優先順序層級.
編寫程式thread6.c
/************************************************************************* > File Name:    thread6.c > Description:  thread6.c程式設定線程的調度屬性 > Author:       Liubingbing > Created Time: 2015年07月06日 星期一 21時15分10秒 > Other:        thread6.c程式先通過sched_get_priority擷取允許的優先順序範圍,然後設定優先權為min_priority ************************************************************************/#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>/* 新線程調用的函數 */void *thread_function(void *arg);char message[] = "hello world";int thread_finished = 0;int main(){int res;pthread_t a_thread;int max_priority;int min_priority;struct sched_param scheduling_value;/* 線程屬性對象thread_attr */pthread_attr_t thread_attr;/* pthread_attr_init初始化一個線程屬性對象 */res = pthread_attr_init(&thread_attr);if (res != 0) {perror("Attribute creation failed");exit(EXIT_FAILURE);}/* pthread_attr_setdetachstate設定屬性,PTHREAD_CREATE_DETACHED不允許調用pthread_join獲得另一個線程的退出狀態 */res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);if (res != 0) {perror("Setting detached attribute failed");exit(EXIT_FAILURE);}/* pthread_attr_setschedpolicy設定調度策略*/res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER);if (res != 0) {perror("Setting scheduling policy failed");exit(EXIT_FAILURE);}/* 尋找可用的優先順序的範圍 */max_priority = sched_get_priority_max(SCHED_OTHER);min_priority = sched_get_priority_min(SCHED_OTHER);/* 設定優先權 */scheduling_value.sched_priority = min_priority;res = pthread_attr_setschedparam(&thread_attr, &scheduling_value);if (res != 0) {perror("Seting scheduling priority failed");exit(EXIT_FAILURE);}/* pthread_create建立一個新線程 * 第一個參數athread中存放新線程的標識符 * 第二個參數thread_attr為線程屬性,上面的代碼就是設定這個對象 * 第三個參數thread_function為新線程將要調用的函數  * 第四個參數(void *)message為傳遞給將要調用函數的參數 */res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);if (res != 0) {perror("Thread creation failed");exit(EXIT_FAILURE);}/* pthread_attr_destroy函數對線程屬性對象thread_attr進行清理和回收 */(void)pthread_attr_destroy(&thread_attr);/* 等待新線程函數設定thread_finished=1 */while (!thread_finished) {printf("Waiting for thread to say it's finished...\n");sleep(1);}printf("Other thread finished, bye!\n");exit(EXIT_SUCCESS);}void *thread_function(void *arg) {printf("thread_function is running. Argument was %s\n", (char *)arg);sleep(4);printf("Second thread setting finished flag, and exiting now\n");thread_finished = 1;pthread_exit(NULL);}
這個程式與設定脫離狀態屬性很相似,區別只是設定的是調度策略.


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

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.