pthread_attr_init線程屬性



1.線程屬性

       線程具有屬性,用pthread_attr_t表示,在對該結構進行處理之前必須進行初始化,在使用後需要對其去除初始化。我們用pthread_attr_init函數對其初始化,用pthread_attr_destroy對其去除初始化。

 

1

名稱:

pthread_attr_init/pthread_attr_destroy

功能:

對線程屬性初始化/去除初始化

頭文件:

#include<pthread.h>

函數原形:

int pthread_attr_init(pthread_attr_t*attr);

int pthread_attr_destroy(pthread_attr_t*attr);

參數:

Attr   線程屬性變量

返回值:

若成功返回0,若失敗返回-1

      

 

 

 

 





調用pthread_attr_init之後,pthread_t結構所包含的內容就是操作系統實現支持的線程所有屬性的默認值。

       如果要去除對pthread_attr_t結構的初始化,可以調用pthread_attr_destroy函數。如果pthread_attr_init實現時爲屬性對象分配了動態內存空間,pthread_attr_destroy還會用無效的值初始化屬性對象,因此如果經pthread_attr_destroy去除初始化之後的pthread_attr_t結構被pthread_create函數調用,將會導致其返回錯誤。

 

線程屬性結構如下:

typedef struct

{

       int                               detachstate;   線程的分離狀態

       int                               schedpolicy;  線程調度策略

       structsched_param              schedparam;  線程的調度參數

       int                               inheritsched;  線程的繼承性

       int                                scope;       線程的作用域

       size_t                           guardsize;   線程棧末尾的警戒緩衝區大小

       int                                stackaddr_set;

       void*                          stackaddr;   線程棧的位置

       size_t                           stacksize;    線程棧的大小

}pthread_attr_t;

 

每個個屬性都對應一些函數對其查看或修改。下面我們分別介紹。

 

二、線程的分離狀態

       線程的分離狀態決定一個線程以什麼樣的方式來終止自己。在默認情況下線程是非分離狀態的,這種情況下,原有的線程等待創建的線程結束。只有當pthread_join()函數返回時,創建的線程纔算終止,才能釋放自己佔用的系統資源。

而分離線程不是這樣子的,它沒有被其他的線程所等待,自己運行結束了,線程也就終止了,馬上釋放系統資源。程序員應該根據自己的需要,選擇適當的分離狀態。所以如果我們在創建線程時就知道不需要了解線程的終止狀態,則可以pthread_attr_t結構中的detachstate線程屬性,讓線程以分離狀態啓動。

 

2

名稱:

pthread_attr_getdetachstate/pthread_attr_setdetachstate

功能:

獲取/修改線程的分離狀態屬性

頭文件:

#include<pthread.h>

函數原形:

int pthread_attr_getdetachstate(const pthread_attr_t *attr,int *detachstate);

int pthread_attr_setdetachstate(pthread_attr_t *attr,intdetachstate);

參數:

Attr   線程屬性變量

Detachstate  線程的分離狀態屬性

返回值:

若成功返回0,若失敗返回-1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

可以使用pthread_attr_setdetachstate函數把線程屬性detachstate設置爲下面的兩個合法值之一:設置爲PTHREAD_CREATE_DETACHED,以分離狀態啓動線程;或者設置爲PTHREAD_CREATE_JOINABLE,正常啓動線程。可以使用pthread_attr_getdetachstate函數獲取當前的datachstate線程屬性。

 

以分離狀態創建線程

#iinclude<pthread.h>

 

void *child_thread(void *arg)

{

printf(“child thread run!\n”);

}

 

int main(int argc,char *argv[ ])

{

      pthread_ttid;

      pthread_attr_tattr;

 

      pthread_attr_init(&attr);

      pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

      pthread_create(&tid,&attr,fn,arg);

      pthread_attr_destroy(&attr);

      sleep(1);

}

 

三、線程的繼承性

       函數pthread_attr_setinheritschedpthread_attr_getinheritsched分別用來設置和得到線程的繼承性,這兩個函數的定義如下:

 

3.

名稱:

pthread_attr_getinheritsched

pthread_attr_setinheritsched

功能:

獲得/設置線程的繼承性

頭文件:

#include<pthread.h>

函數原形:

int pthread_attr_getinheritsched(const pthread_attr_t*attr,int *inheritsched);

int pthread_attr_setinheritsched(pthread_attr_t *attr,intinheritsched);

參數:

attr            線程屬性變量

inheritsched     線程的繼承性

返回值:

若成功返回0,若失敗返回-1

      

 

 

 

 

 

 

 

 

 這兩個函數具有兩個參數,第1個是指向屬性對象的指針,第2個是繼承性或指向繼承性的指針。繼承性決定調度的參數是從創建的進程中繼承還是使用在schedpolicyschedparam屬性中顯式設置的調度信息。Pthreads不爲inheritsched指定默認值,因此如果你關心線程的調度策略和參數,必須先設置該屬性。

       繼承性的可能值是PTHREAD_INHERIT_SCHED(表示新現成將繼承創建線程的調度策略和參數)和PTHREAD_EXPLICIT_SCHED(表示使用在schedpolicyschedparam屬性中顯式設置的調度策略和參數)。

       如果你需要顯式的設置一個線程的調度策略或參數,那麼你必須在設置之前將inheritsched屬性設置爲PTHREAD_EXPLICIT_SCHED.

       下面我來講進程的調度策略和調度參數。我會結合下面的函數給出本函數的程序例子。


 

四、線程的調度策略

       函數pthread_attr_setschedpolicypthread_attr_getschedpolicy分別用來設置和得到線程的調度策略。

 

4.

名稱:

pthread_attr_getschedpolicy

pthread_attr_setschedpolicy

功能:

獲得/設置線程的調度策略

頭文件:

#include<pthread.h>

函數原形:

int pthread_attr_getschedpolicy(const pthread_attr_t*attr,int *policy);

int pthread_attr_setschedpolicy(pthread_attr_t *attr,intpolicy);

參數:

attr           線程屬性變量

policy         調度策略

返回值:

若成功返回0,若失敗返回-1

      

 

 

 

 

 

 

 

 這兩個函數具有兩個參數,第1個參數是指向屬性對象的指針,第2個參數是調度策略或指向調度策略的指針。調度策略可能的值是先進先出(SCHED_FIFO)、輪轉法(SCHED_RR,或其它(SCHED_OTHER)。

       SCHED_FIFO策略允許一個線程運行直到有更高優先級的線程準備好,或者直到它自願阻塞自己。在SCHED_FIFO調度策略下,當有一個線程準備好時,除非有平等或更高優先級的線程已經在運行,否則它會很快開始執行。

    SCHED_RR(輪循)策略是基本相同的,不同之處在於:如果有一個SCHED_RR

策略的線程執行了超過一個固定的時期(時間片間隔)沒有阻塞,而另外的SCHED_RRSCHBD_FIPO策略的相同優先級的線程準備好時,運行的線程將被搶佔以便準備好的線程可以執行。

    當有SCHED_FIFOSCHED_RR策賂的線程在一個條件變量上等持或等持加鎖同一個互斥量時,它們將以優先級順序被喚醒。即,如果一個低優先級的SCHED_FIFO線程和一個高優先織的SCHED_FIFO線程都在等待鎖相同的互斥且,則當互斥量被解鎖時,高優先級線程將總是被首先解除阻塞。

 

五、線程的調度參數

       函數pthread_attr_getschedparam pthread_attr_setschedparam分別用來設置和得到線程的調度參數。

 

5.

名稱:

pthread_attr_getschedparam

pthread_attr_setschedparam

功能:

獲得/設置線程的調度參數

頭文件:

#include<pthread.h>

函數原形:

int pthread_attr_getschedparam(const pthread_attr_t*attr,struct sched_param *param);

int pthread_attr_setschedparam(pthread_attr_t *attr,conststruct sched_param *param);

參數:

attr           線程屬性變量

param          sched_param結構

返回值:

若成功返回0,若失敗返回-1

      

 

 

 

 

 

 

 

這兩個函數具有兩個參數,第1個參數是指向屬性對象的指針,第2個參數是sched_param結構或指向該結構的指針。結構sched_param在文件/usr/include/bits/sched.h中定義如下:

      

struct sched_param

{

       intsched_priority;

};

 

結構sched_param的子成員sched_priority控制一個優先權值,大的優先權值對應高的優先權。系統支持的最大和最小優先權值可以用sched_get_priority_max函數和sched_get_priority_min函數分別得到。

 

注意:如果不是編寫實時程序,不建議修改線程的優先級。因爲,調度策略是一件非常複雜的事情,如果不正確使用會導致程序錯誤,從而導致死鎖等問題。如:在多線程應用程序中爲線程設置不同的優先級別,有可能因爲共享資源而導致優先級倒置。

 

6.

http://linux.die.net/man/3/sched_get_priority_min 

Synopsis

#include <sched.h>

int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy); 

Description

The sched_get_priority_max() and sched_get_priority_min() functions shall return the appropriate maximum or minimum, respectively, for the scheduling policy specified bypolicy.

The value of policy shall be one of the scheduling policy values defined in <sched.h>.

Return Value

If successful, the sched_get_priority_max() and sched_get_priority_min() functions shall return the appropriate maximum or minimum values, respectively. If unsuccessful, they shall return a value of -1 and set errno to indicate the error.

Errors

The sched_get_priority_max() and sched_get_priority_min() functions shall fail if:

EINVAL
The value of the policy parameter does not represent a defined scheduling policy.
 

   下面是上面幾個函數的程序例子:


#include <string.h>
#include<pthread.h>
#include<sched.h>
void *child_thread(void *arg)
{
int policy = 0;
int max_priority = 0,min_priority = 0;
struct sched_param param;
pthread_attr_t attr;
 
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_getinheritsched(&attr,&policy);
if(policy == PTHREAD_EXPLICIT_SCHED){
printf(“Inheritsched:PTHREAD_EXPLICIT_SCHED\n”);
}
if(policy == PTHREAD_INHERIT_SCHED){
printf(“Inheritsched:PTHREAD_INHERIT_SCHED\n”);
}
 
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_getschedpolicy(&attr,&policy);
if(policy == SCHED_FIFO){
printf(“Schedpolicy:SCHED_FIFO\n”);
}
if(policy == SCHED_RR){
printf(“Schedpolicy:SCHED_RR\n”);
}
if(policy == SCHED_OTHER){
printf(“Schedpolicy:SCHED_OTHER\n”);
}
max_priority = sched_get_priority_max(policy);
min_priority = sched_get_priority_min(policy);
printf(“Maxpriority:%u\n”,max_priority);
printf(“Minpriority:%u\n”,min_priority);
 
param.sched_priority = max_priority;
pthread_attr_setschedparam(&attr,&param);
printf(“sched_priority:%u\n”,param.sched_priority);
pthread_attr_destroy(&attr);
}
 
int main(int argc,char *argv[ ])
{
pthread_t child_thread_id; 
pthread_create(&child_thread_id,NULL,child_thread,NULL);
pthread_join(child_thread_id,NULL);
}
==[23]==gaoke@dev64_23:~/codeg++otestgaoke.cpplpthread==[23]==gaoke@dev6423: /code ./test
Inheritsched:PTHREAD_EXPLICIT_SCHED
Schedpolicy:SCHED_RR
Maxpriority:99
Minpriority:1
sched_priority:99
 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章