1、每个POSIX线程有一个相连的属性对象来表示属性。线程属性对象的类型是pthread_attr_t,pthread_attr_t 在文件/usr/include/bits/pthreadtypes.h中定义。
2、代码及运行结果:
/*
* pthreadAttr.c
*
* Created on: Aug 17, 2013
* Author: root
*/
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h> void * my_thread(void * arg){
int retval = ;
pthread_attr_t attr;
struct sched_param param;
size_t stacksize;
int detachstate;
int scope;
int inherit;
int policy; if(pthread_attr_init(&attr) == ){
if(pthread_attr_getstacksize(&attr, &stacksize) == ){
printf("StackSize: %d\n", stacksize);
}
if(pthread_attr_getdetachstate(&attr, &detachstate) == ){
if(detachstate == PTHREAD_CREATE_JOINABLE){
printf("DetachState:PTHREAD_CREATE_JOINABLE.\n");
}
if(detachstate == PTHREAD_CREATE_DETACHED){
printf("DetachState:PTHREAD_CREATE_DETACHED.\n");
}
}
if(pthread_attr_getscope(&attr, &scope) == ){
if(scope == PTHREAD_SCOPE_PROCESS){
printf("Scope:PTHREAD_SCOPE_PROCESS\n");
}
if(scope == PTHREAD_SCOPE_SYSTEM){
printf("Scope:PTHREAD_SCOPE_SYSTEM\n");
}
}
if(pthread_attr_getinheritsched(&attr, &inherit) == ){
if(inherit == PTHREAD_INHERIT_SCHED){
printf("InheritSched:PHREAD_INHERIT_SCHED\n");
}
if(inherit == PTHREAD_EXPLICIT_SCHED){
printf("InheritSched:PHTREAD_EXPLICIT_SCHED\n");
}
}
if(pthread_attr_getschedpolicy(&attr, &policy) == ){
if(policy == SCHED_FIFO){
printf("schedPolicy:SCHED_FIFO\n");
}
if(policy == SCHED_RR){
printf("SchedPolicy:SCHED_RR\n");
}
else{
printf("SchedPolicy:SCHED_OTHER\n");
}
}
if(pthread_attr_getschedparam(&attr, ¶m) == ){
printf("SchedPriority:%d\n", param.__sched_priority);
}
pthread_attr_destroy(&attr);
} pthread_exit(&retval);
} int main(){
int count;
pthread_t thread;
int *retval;
if(pthread_create(&thread, NULL, my_thread, (void*)NULL) != ){
printf("Count not create thread!\n");
return -;
}
if(pthread_join(thread, (void **)(&retval)) != ){
printf("No thread to join!\n");
return -;
}
return ;
}
运行结果: