1.線程屬性
?
?
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。 |
?
?
?
?
?
?
?
線程屬性結構如下:
typedef struct
{
?
?
?
?
?
?
?
?
?
}pthread_attr_t;
?
每個個屬性都對應一些函數對其查看或修改。下面我們分別介紹。
?
二、線程的分離狀態
?
而分離線程不是這樣子的,它沒有被其他的線程所等待,自己運行結束了,線程也就終止了,馬上釋放系統資源。程序員應該根據自己的需要,選擇適當的分離狀態。所以如果我們在創建線程時就知道不需要了解線程的終止狀態,則可以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[ ]) { ? ? ? ? ? ? ? ? } |
?
三、線程的繼承性
?
?
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。 |
?
?
?
?
?
?
?
?
?
?
?
?
?
?
四、線程的調度策略
?
?
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。 |
?
?
?
?
?
?
?
?
?
?
?
策略的線程執行了超過一個固定的時期(時間片間隔)沒有阻塞,而另外的SCHED_RR或SCHBD_FIPO策略的相同優先級的線程準備好時,運行的線程將被搶占以便準備好的線程可以執行。
?
?
五、線程的調度參數
?
?
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? |
返回值: | 若成功返回0,若失敗返回-1。 |
?
?
?
?
?
?
?
?
這兩個函數具有兩個參數,第1個參數是指向屬性對象的指針,第2個參數是sched_param結構或指向該結構的指針。結構sched_param在文件/usr/include/bits/sched.h中定義如下:
?
struct sched_param
{
?
};
?
結構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<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,¶m);
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:~/code$./test
Inheritsched:PTHREAD_EXPLICIT_SCHED
Schedpolicy:SCHED_RR
Maxpriority:99
Minpriority:1
sched_priority:99