linux下的執行緒屬性(5)

2021-10-10 14:16:22 字數 1093 閱讀 6907

pthread_attr_init,函式,作用是初始化乙個執行緒物件的屬性,需要用pthread_attr_destroy函式對其去除初始化。

#include

int pthread_attr_init(pthread_attr_t *attr);

返回0,表示函式初始化物件成功。失敗時返回乙個錯誤**。

指向乙個執行緒屬性結構的指標,結構中的元素分別對應著新執行緒的執行屬性。屬性物件主要包括是否繫結、是否分離、堆疊位址和大小、優先順序等。預設屬性為非繫結、非分離、預設1mb堆疊、與父程序有相同優先順序。

關於脫離執行緒的說明:

使用pthread_create()函式建立執行緒時,函式第二個引數為null,則使用執行緒屬性的預設引數,其中非分離屬性需要程式退出之前執行pthread_join把各個執行緒歸併到一起。如果想讓執行緒向建立它的執行緒返回資料,就必須這樣做。但是如果既不需要第二個執行緒向主線程返回資訊,也不需要主線程等待它,可以設定分離屬性,建立「脫離執行緒」。

#include#include#include#includeconst char message = "hello, linux.";

int thread_finished = 0;

void thread_function(void *arg)

int main(int argc, char *ar**)

ret = pthread_attr_setdetachstate(&attr, pthread_create_detached);

if(ret != 0)

//create a new thread

ret = pthread_create(&thid, &attr, (void*)&thread_function, (void*)message);

if(ret != 0)

//銷毀執行緒屬性結構,它在重新初始化之前不能重新使用

pthread_attr_destroy(&attr);

while(!thread_finished)

printf("other thread fiinshed. bye!\n");

return 0;

}

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執行緒 2 執行緒屬性

執行緒屬性由資料結構pthread attr t結構表示,其定義如下所示 typedef struct pthread attr t 這個結構體在使用過程中由pthread attr init和pthread attr destory負責資料的初始化和銷毀 schepolicy 表示執行緒被排程的策...