线程的属性
线程的创建借口pthread_create第二个入参attr指定新建线程的相关属性,一个线程的属性主要包括:线程栈地址和大小,线程的调度策略和优先级,线程是否属于分离detach状态等. 属性的初始化和销毁,初始化必须在线程创建接口前
int pthread_attr_init(pthread_attr_t *attr );
int pthread_attr_destroy(pthread_attr_t *attr );
#define _GNU_SOURCE
#include <pthread.h>
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr);
设置和获取线程的状态 detachstate参数的取值:
– PTHREAD_CREATE_JOINABLE,新建线程处于可连接状态,该值为不指定属性创建线程时的默认值。
– PTHREAD_CREATE_DETACHED,新建线程处于分离状态
int pthread_attr_setdetachstate(pthread_attr_t *attr , int detachstate);
int pthread_attr_getdetachstate(pthread_attr_t *attr , int *detachstate );
int pthread_attr_setstack(pthread_attr_t *attr ,void *stackaddr , size_t stacksize);
int pthread_attr_getstack(pthread_attr_t *attr , void ** stackaddr, size_t *stacksize );
线程attr设置的一个demo代码
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define handle_error_en(en,msg)\
errno = en;perror(msg);exit (EXIT_FAILURE);
#define DEFAULT_STACK_SIZE 16384
static void display_pthread_attr(pthread_attr_t *attr,const char * prefix)
{
int result,state;
size_t stacksize;
void * stackaddr;
struct sched_param sp;
result = pthread_attr_getdetachstate(attr,&state);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_getdetachstate" );
}
if (state == PTHREAD_CREATE_DETACHED)
{
printf ("%s Detach state = PTHREAD_CREATE_DETACHED\n" ,prefix);
}
else
{
printf ("%s Detach state = PTHREAD_CREATE_JOINABLE\n" ,prefix);
}
result = pthread_attr_getstack(attr,&stackaddr,&stacksize);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_getstack" );
}
printf ("%s stack addr = %p\n" ,prefix,stackaddr);
printf ("%s stack size = 0x%x\n" ,prefix,(unsigned )stacksize);
}
static void * thread_start(void *arg)
{
int result;
pthread_attr_t gattr;
result = pthread_getattr_np(pthread_self(),&gattr);
if (result != 0 )
{
handle_error_en(result,"pthread_getattr_np" );
}
printf ("thread attributes:\n" );
display_pthread_attr(&gattr,"\t" );
exit (EXIT_SUCCESS);
}
int main(int argc,char * argv[])
{
pthread_t thr;
pthread_attr_t attr;
pthread_attr_t *attrp;
int result;
attrp = NULL;
size_t stack_size = DEFAULT_STACK_SIZE;
void *sp;
attrp = &attr;
result = pthread_attr_init(&attr);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_init" );
}
result = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_setdetachstate" );
}
if (argc > 1 )
{
stack_size = strtoul(argv[1 ], NULL, 0 );
}
result = posix_memalign(&sp,sysconf(_SC_PAGESIZE),stack_size);
if (result != 0 )
{
handle_error_en(result,"posix_memalign" );
}
printf ("posix_memalign() allocated at %p,stacksize = %zu\n" ,sp,stack_size);
result = pthread_attr_setstack(&attr,sp,stack_size);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_setstack" );
}
result = pthread_create(&thr,attrp,&thread_start,NULL);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_setstack" );
}
printf ("pthread_create ok,posix_memalign() allocated at %p,stacksize = %zu\n" ,sp,stack_size);
if (attrp)
{
result = pthread_attr_destroy(attrp);
if (result != 0 )
{
handle_error_en(result,"pthread_attr_destroy" );
}
}
return 0 ;
}