本節主要講 pthread_create 函式的第二個引數——執行緒的屬性。之前設為 null,也就是採用預設屬性。現在講一下修改執行緒屬性的方法。
這些屬性主要包括繫結屬性、分離屬性、堆疊位址、堆疊大小、優先順序。其中系統預設的屬性為非繫結、非分離、預設1m 的堆疊、與父程序同樣級別的優先順序。
具體屬性的含義請參考手冊,本文只講如何使用這些函式。其實挺簡單的,
跟普通的執行緒呼叫只需多加三步:
1、呼叫pthread_attr_init初始化執行緒屬性。
2、呼叫相應的設定屬性的函式,如下文用到的pthread_attr_setschedparam設定執行緒優先順序。
3、然後將pthread_create的第二個引數傳進去。
#include
int pthread_attr_init(pthread_attr_t *attr)
返回值:若成功,返回0;若出錯,返回-1
引數:
#include
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
返回值:若成功,返回0;若出錯,返回-1
引數:
#include
int pthread_attr_setscope(pthread_attr_t *attr, int detachstate)
返回值:若成功,返回0;若出錯,返回-1
引數:
#include
int pthread_attr_getschedparam (pthread_attr_t *attr, struct sched_param *param)
返回值:若成功,返回0;若出錯,返回-1
引數:
#include
int pthread_attr_setschedparam (pthread_attr_t *attr, struct sched_param *param)
返回值:若成功,返回0;若出錯,返回-1
引數:主要看看執行緒屬性是如何使用的。
/* pthread_attr_control.c*/
#include
#include
#include
#include
#include
#include
#include
#include
sem_t sem;
static
int count = 0 ;
#define filename "pthread_attr_control_file"
/*執行緒一*/
void thread1(void * arg)
count++;
}printf("this is a pthread1....and count = %d\n",count);
/*訊號量加一,v 操作*/
sem_post(&sem);
sleep(2);
}}/*執行緒二*/
void thread2(void * arg)
count++;
}printf("this is a pthread2.... count = %d\n",count);
/*訊號量加一,v 操作*/
sem_post(&sem);
sleep(2);
}}static
int api_get_thread_policy (pthread_attr_t *attr)
return policy;
}int main(void)
printf("open success!\n");
int i,ret;
/*初始化訊號量為 1*/
ret=sem_init(&sem,0,1);
if(ret!=0)
pthread_t id1,id2;
pthread_attr_t attr;
//獲取執行緒排程優先順序
struct sched_param param;
int rs = pthread_attr_getschedparam (&attr, ¶m);
assert (rs == 0);
printf ("current priority = %d\n", param.__sched_priority);
/* 獲得當前排程策略 */
int policy = api_get_thread_policy (&attr);
int priority_max = sched_get_priority_max (policy);
assert (priority_max != -1);
printf ("max_priority = %d\n", priority_max);
int priority_min = sched_get_priority_min (policy);
assert (priority_min != -1);
printf ("min_priority = %d\n", priority_min);
int priority = priority_min + 1;
printf("the priority of pthread1 is %d",priority);
/*初始化執行緒屬性*/
pthread_attr_init(&attr);
//設定執行緒一優先順序
rs = pthread_attr_setschedpolicy (&attr, priority);
assert (rs == 0);
// rs = pthread_attr_getschedparam (&attr, ¶m);
// assert (rs == 0);
// printf ("show show ------priority = %d\n", param.__sched_priority);
/*建立執行緒一*/
ret=pthread_create(&id1,&attr,(void *) thread1,&fd);
if(ret!=0)
//設定執行緒二優先順序
priority = priority_min;
printf("the priority of pthread1 is %d",priority);
rs = pthread_attr_setschedpolicy (&attr, priority);
assert (rs == 0);
/*建立執行緒二*/
ret=pthread_create(&id2,&attr,(void *) thread2,&fd);
if(ret!=0)
/*等待執行緒結束*/
pthread_join(id1,null);
pthread_join(id2,null);
return
0;}
實驗結果:
ubuntu:~/test/pthread_test$ ./pthread_attr_control
open success!
current priority = 0
policy = sched_other
max_priority = 0
min_priority = 0
the priority of pthread1 is
1the priority of pthread1 is
0this is pthread2 and fd = 3
this is pthread1,fd = 3
write: success
write: success
write: success
write: success
write: success
this is a pthread2.... count = 5
write: success
write: success
write: success
write: success
write: success
this is a pthread1....and
count = 10
write: success
write: success
write: success
write: success
write: success
this is a pthread2.... count = 15
write: success
write: success
write: success
write: success
write: success
this is a pthread1....and
count = 20
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 表示執行緒被排程的策...