linux執行緒 2 執行緒屬性

2021-06-19 00:07:58 字數 1363 閱讀 5674

執行緒屬性由資料結構pthread_attr_t結構表示,其定義如下所示:

typedef struct

pthread_attr_t;

這個結構體在使用過程中由pthread_attr_init和pthread_attr_destory負責資料的初始化和銷毀;

schepolicy : 表示執行緒被排程的策略。主要包括sched_other(正常、非實時)、sched_rr(實時、輪轉法)和 sched_fifo(實時、先入先出)三種,預設為sched_other,後兩種排程策略僅對超級使用者有效。執行時可以用過 pthread_setschedparam()來改變,該函式原型是這樣的:int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param *param)。這個呼叫可以動態的改變排程策略和執行緒的優先順序。

scheparam: 乙個struct sched_param結構,結構體包含乙個sched_priority整型變數表示執行緒的執行優先順序。這個引數僅當排程策略為實時(即sched_rr 或sched_fifo)時才有效,並可以在執行時通過pthread_setschedparam()函式來改變,預設為0。

inheritshed : 表示執行緒建立的繼承;呼叫函式pthread_attr_setinheritsched和pthread_attr_getinheritsched用來設定和得到執行緒的繼承屬性。有兩種值可供選擇:pthread_explicit_sched和pthread_inherit_sched,前者表示新執行緒使用顯式指定排程策略和排程引數(即attr中的值),而後者表示繼承呼叫者執行緒的值。預設為pthread_explicit_sched。

scope : 表示執行緒間競爭cpu的範圍,也就是說執行緒優先順序的有效範圍。posix的標準中定義了兩個值: pthread_scope_system和pthread_scope_process,前者表示與系統中所有執行緒一起競爭cpu時間,後者表示僅與同程序中的執行緒競爭cpu。可以通過呼叫pthread_attr_getscope(), pthread_attr_setscope(),用於獲取屬性中的執行緒的作用域。

stacksize,stackaddr : 表示執行緒堆疊的大小和位址。也有四個函式用於設定和獲取相應的值。pthread_attr_getstatcksize(),pthread_attr_setstatcksize(),pthread_attr_setstackaddr(),pthread_attr_getstatckaddr()。

guardsize : 控制著執行緒棧末尾之後以避免棧溢位的擴充套件記憶體大小。同樣可以呼叫pthread_attr_setguardsize()和pthread_attr_getguardsize()。

linux 執行緒 執行緒屬性

typedef struct pthread attr t 這個結構只是為了說明 實際結構具體系統而定 雖然如此我們並不用擔心因為 屬性值不能直接設定,須使用相關函式進行操作 int pthread attr init pthread attr t attr 初始化執行緒屬性 int pthread...

Linux執行緒屬性

執行緒屬性識別符號 pthread attr t 包含在 pthread.h 標頭檔案中。typedef struct pthread attr t 屬性值不能直接設定,須使用相關函式進行操作,初始化的函式為pthread attr init,這個函式必須在pthread create函式之前呼叫。...

Linux執行緒屬性總結

執行緒屬性識別符號 pthread attr t 包含在 pthread.h 標頭檔案中。c view plain copy 執行緒屬性結構如下 typedef struct pthread attr t 屬性值不能直接設定,須使用相關函式進行操作,初始化的函式為pthread attr init,...