您当前的位置: 首页 > 
  • 3浏览

    0关注

    880博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

pthread_attr_init () 函数详解

CodeAllen嵌入式编程 发布时间:2021-07-12 20:33:44 ,浏览量:3

1.【线程属性】 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之前必须进行初始化,在使用后需要对其去除初始化。 调用pthread_attr_init之后,pthread_t结构所包含的内容就是操作系统实现支持的线程所有属性的默认值。 如果要去除对pthread_attr_t结构的初始化,可以调用pthread_attr_destroy函数。如果pthread_attr_init实现时为属性对象分配了动态内存空间,pthread_attr_destroy还会用无效的值初始化属性对象,因此如果经pthread_attr_destroy去除初始化之后的pthread_attr_t结构被pthread_create函数调用,将会导致其返回错误。  

typedef struct
{
       int                       detachstate;   // 线程的分离状态
       int                       schedpolicy;   // 线程调度策略
       structsched_param         schedparam;    // 线程的调度参数
       int                       inheritsched;  // 线程的继承性
       int                       scope;         // 线程的作用域
       size_t                    guardsize;     // 线程栈末尾的警戒缓冲区大小
       int                       stackaddr_set; // 线程的栈设置
       void*                     stackaddr;     // 线程栈的位置
       size_t                    stacksize;     // 线程栈的大小
} pthread_attr_t;

pthread_attr_init #include int pthread_attr_init(pthread_attr_t *attr); Compile and link with -pthread. 功能:初始化一个线程属性对象 参数:@attr 线程属性结构体指针变量 返回值:0 - 成功,非0 - 失败

pthread_attr_destroy #include int pthread_attr_destroy(pthread_attr_t *attr); Compile and link with -pthread. 功能:销毁一个线程属性对象 参数:@attr 线程属性结构体指针变量 返回值:0 - 成功,非0 - 失败  

2.【线程的分离状态】 线程的分离状态决定一个线程以什么样的方式来终止自己。在默认情况下线程是非分离状态的,这种情况下,原有的线程等待创建的线程结束。只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源。 而分离线程不是这样子的,它没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。程序员应该根据自己的需要,选择适当的分离状态。所以如果我们在创建线程时就知道不需要了解线程的终止状态,则可以pthread_attr_t结构中的detachstate线程属性,让线程以分离状态启动。 可以使用pthread_attr_setdetachstate函数把线程属性detachstate设置为下面的两个合法值之一:设置为PTHREAD_CREATE_DETACHED,以分离状态启动线程;或者设置为PTHREAD_CREATE_JOINABLE,正常启动线程。可以使用pthread_attr_getdetachstate函数获取当前的datachstate线程属性。

pthread_attr_getdetachstate #includeint pthread_attr_getdetachstate(pthread_attr_t *attr,int detachstate); Compile and link with -pthread. 功能:获取线程的分离状态属性 参数:     @attr 线程属性变量     @detachstate 线程的分离状态属性 返回值:0 - 成功,非0 - 失败

pthread_attr_setdetachstate #includeint pthread_attr_setdetachstate(pthread_attr_t *attr,int *detachstate); Compile and link with -pthread. 功能:获取线程的分离状态属性 参数:     @attr 线程属性变量     @detachstate 线程的分离状态属性 返回值:0 - 成功,非0 - 失败  

#include 
#include 
#include 
#include 
void *child_thread(void *arg)
{
	int policy = 0;
	int max_priority = 0,min_priority = 0;
	struct sched_param param;
	pthread_attr_t attr;
 
	pthread_attr_init(&attr);
	pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_getinheritsched(&attr,&policy);
	if(policy == PTHREAD_EXPLICIT_SCHED){
		printf("Inheritsched:PTHREAD_EXPLICIT_SCHED\n");
	}
	if(policy == PTHREAD_INHERIT_SCHED){
		printf("Inheritsched:PTHREAD_INHERIT_SCHED\n");
	}
 
	pthread_attr_setschedpolicy(&attr,SCHED_RR);
	pthread_attr_getschedpolicy(&attr,&policy);
	if(policy == SCHED_FIFO){
		printf("Schedpolicy:SCHED_FIFO\n");
	}
	if(policy == SCHED_RR){
		printf("Schedpolicy:SCHED_RR\n");
	}
	if(policy == SCHED_OTHER){
		printf("Schedpolicy:SCHED_OTHER\n");
	}
	max_priority = sched_get_priority_max(policy);
	min_priority = sched_get_priority_min(policy);
	printf("Maxpriority:%u\n",max_priority);
	printf("Minpriority:%u\n",min_priority);
 
	param.sched_priority = max_priority;
	pthread_attr_setschedparam(&attr,¶m);
	printf("sched_priority:%u\n",param.sched_priority);
	pthread_attr_destroy(&attr);
}
 
int main(int argc,char *argv[ ])
{
	pthread_t child_thread_id;
	pthread_create(&child_thread_id,NULL,child_thread,NULL);
	pthread_join(child_thread_id,NULL);
 
	return 0;
}
 
编译:
gcc pthread.c -o pthread -pthread
 
运行结果:
Inheritsched:PTHREAD_EXPLICIT_SCHED
Schedpolicy:SCHED_RR
Maxpriority:99
Minpriority:1
sched_priority:99

关注
打赏
1665938897
查看更多评论
立即登录/注册

微信扫码登录

0.0367s