pthread_attr_init () 函数详解

举报
CodeAllen 发表于 2021/10/30 00:41:04 2021/10/30
【摘要】 1.【线程属性】 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之前必须进行初始化,在使用后需要对其去除初始化。 调用pthread_attr_init之后,pthread_t结构所包含的内容就是操作系统实现支持的线程所有属性的默认值。 如果要去除对pthread_attr_t结构的初始化,可以调用pthread_...

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函数调用,将会导致其返回错误。
 


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

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

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

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


  
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <pthread.h>
  4. #include <sched.h>
  5. void *child_thread(void *arg)
  6. {
  7. int policy = 0;
  8. int max_priority = 0,min_priority = 0;
  9. struct sched_param param;
  10. pthread_attr_t attr;
  11. pthread_attr_init(&attr);
  12. pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
  13. pthread_attr_getinheritsched(&attr,&policy);
  14. if(policy == PTHREAD_EXPLICIT_SCHED){
  15. printf("Inheritsched:PTHREAD_EXPLICIT_SCHED\n");
  16. }
  17. if(policy == PTHREAD_INHERIT_SCHED){
  18. printf("Inheritsched:PTHREAD_INHERIT_SCHED\n");
  19. }
  20. pthread_attr_setschedpolicy(&attr,SCHED_RR);
  21. pthread_attr_getschedpolicy(&attr,&policy);
  22. if(policy == SCHED_FIFO){
  23. printf("Schedpolicy:SCHED_FIFO\n");
  24. }
  25. if(policy == SCHED_RR){
  26. printf("Schedpolicy:SCHED_RR\n");
  27. }
  28. if(policy == SCHED_OTHER){
  29. printf("Schedpolicy:SCHED_OTHER\n");
  30. }
  31. max_priority = sched_get_priority_max(policy);
  32. min_priority = sched_get_priority_min(policy);
  33. printf("Maxpriority:%u\n",max_priority);
  34. printf("Minpriority:%u\n",min_priority);
  35. param.sched_priority = max_priority;
  36. pthread_attr_setschedparam(&attr,¶m);
  37. printf("sched_priority:%u\n",param.sched_priority);
  38. pthread_attr_destroy(&attr);
  39. }
  40. int main(int argc,char *argv[ ])
  41. {
  42. pthread_t child_thread_id;
  43. pthread_create(&child_thread_id,NULL,child_thread,NULL);
  44. pthread_join(child_thread_id,NULL);
  45. return 0;
  46. }
  47. 编译:
  48. gcc pthread.c -o pthread -pthread
  49. 运行结果:
  50. Inheritsched:PTHREAD_EXPLICIT_SCHED
  51. Schedpolicy:SCHED_RR
  52. Maxpriority:99
  53. Minpriority:1
  54. sched_priority:99

文章来源: allen5g.blog.csdn.net,作者:CodeAllen的博客,版权归原作者所有,如需转载,请联系作者。

原文链接:allen5g.blog.csdn.net/article/details/118683360

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。