博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程属性相关函数与操作
阅读量:5216 次
发布时间:2019-06-14

本文共 6490 字,大约阅读时间需要 21 分钟。

(1)线程属性相关函数

int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);
 
/* 栈空间大小 */
PTHREAD_STACK_MIN // 0x4000
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);
 
/* 分离状态 */
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);
 
/* 作用域 */
PTHREAD_SCOPE_SYSTEM
PTHREAD_SCOPE_PROCESS
int pthread_attr_setscope(pthread_attr_t *attr, int scope);
int pthread_attr_getscope(pthread_attr_t *attr, int *scope);
 
/* 调度策略 */
SCHED_FIFO
SCHED_RR
SCHED_OTHER
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
/* 调度优先级 */
#include <sched.h>
MAX:int sched_get_priority_max(int policy);  // 99
MIN:int sched_get_priority_min(int policy); // 1
int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);
int pthread_attr_getschedparam(pthread_attr_t *attr,struct sched_param *param);
 
/* 继承属性 */
PTHREAD_INHERIT_SCHED
PTHREAD_EXPLICIT_SCHED
int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched);
int pthread_attr_getinheritsched(pthread_attr_t *attr,int *inheritsched);
 

/* 栈末尾的警戒缓冲区大小 */

int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);
 
/* 栈空间地址 */
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
int pthread_attr_getstackaddr(pthread_attr_t *attr, void **stackaddr);

(2)线程操作相关函数

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
void pthread_exit(void *retval);
pthread_t pthread_self(void);
int pthread_equal(pthread_t t1, pthread_t t2);
int pthread_join(pthread_t thread, void **retval);
int pthread_cancel(pthread_t thread);
int pthread_kill(pthread_t thread, int sig);
int pthread_detach(pthread_t thread);

(3)代码举例

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 10 /* usr date */ 11 static int usr_date = 0; 12 13 static void test_write(void *arg) 14 { 15 /* set pthread name */ 16 prctl(PR_SET_NAME, "test_write"); 17 printf("write pthread id:%lu\n", pthread_self()); 18 19 while(1) { 20 printf("w:usr_date:%d\n", usr_date++); 21 if (usr_date >= 5) { 22 pthread_exit(0); 23 } 24 25 sleep(2); 26 } 27 28 return ; 29 } 30 31 static void test_read(void *arg) 32 { 33 /* set pthread name */ 34 prctl(PR_SET_NAME, "test_read"); 35 printf("read pthread id:%lu\n", pthread_self()); 36 37 while(1) { 38 printf("r:usr_date:%d\n", usr_date); 39 sleep(1); 40 } 41 42 return ; 43 } 44 45 int pthread_set_attr(pthread_attr_t *attr, int priority, size_t stacksize) 46 { 47 int rval; 48 struct sched_param params; 49 int maxPriority, minPriority; 50 51 rval = pthread_attr_init(attr); 52 if (rval != 0) 53 { 54 return rval; 55 } 56 57 /* normally, need not to set */ 58 rval = pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); 59 if (rval != 0) 60 { 61 printf("pthread_attr_setinheritsched failed\n"); 62 return rval; 63 } 64 65 rval = pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM); 66 if (rval != 0) 67 { 68 if (rval == ENOTSUP) 69 { 70 printf("The system does not support the %s scope, using %s\n", 71 "PTHREAD_SCOPE_SYSTEM", "PTHREAD_SCOPE_PROCESS"); 72 73 rval = pthread_attr_setscope(attr, PTHREAD_SCOPE_PROCESS); 74 } 75 76 if (rval) 77 { 78 printf("pthread_attr_setscope failed\n"); 79 return rval; 80 } 81 } 82 83 /* use the round robin scheduling algorithm */ 84 rval = pthread_attr_setschedpolicy(attr, SCHED_RR); 85 if (rval != 0) 86 { 87 printf("pthread_attr_setschedpolicy failed\n"); 88 return rval; 89 } 90 91 /* set the thread to be detached */ 92 rval = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_JOINABLE); 93 if (rval != 0) 94 { 95 printf("pthread_attr_setdetachstate failed\n"); 96 return rval; 97 } 98 99 /* first get the scheduling parameter, then set the new priority */100 rval = pthread_attr_getschedparam(attr, &params);101 if (rval != 0)102 {103 printf("pthread_attr_getschedparam failed\n");104 return rval;105 }106 107 minPriority = sched_get_priority_min(SCHED_RR);108 maxPriority = sched_get_priority_max(SCHED_RR);109 printf("maxPriority:%d, minPriority:%d\n", maxPriority, minPriority);110 111 if (priority < minPriority)112 {113 priority = minPriority;114 }115 else if (priority > maxPriority)116 {117 priority = maxPriority;118 }119 params.sched_priority = priority;120 rval = pthread_attr_setschedparam(attr, &params);121 if (rval != 0)122 {123 printf("pthread_attr_setschedparam failed\n");124 return rval;125 }126 127 /* when set stack size, we define a minmum value to avoid fail */128 printf("PTHREAD_STACK_MIN:%x\n", PTHREAD_STACK_MIN);129 if (stacksize < PTHREAD_STACK_MIN)130 {131 stacksize = PTHREAD_STACK_MIN;132 }133 rval = pthread_attr_setstacksize(attr, stacksize);134 if (rval != 0)135 {136 printf("pthread_attr_setstacksize failed\n");137 return rval;138 }139 140 return 0;141 }142 143 void test(void)144 {145 pthread_t wtest_taskid;146 pthread_t rtest_taskid;147 pthread_attr_t attr;148 int *retval = NULL;149 size_t stacksize;150 int priority;151 int ret;152 153 pthread_attr_init(&attr);154 155 stacksize = 4096;156 priority = 50;157 ret = pthread_set_attr(&attr, priority, stacksize);158 if (ret) {159 pthread_attr_destroy(&attr);160 return ;161 }162 163 /* create write pthread */164 ret = pthread_create(&wtest_taskid, NULL, (void *)test_write, NULL);165 if (ret) {166 printf("%s failed, errno:%d\n", __FUNCTION__, errno);167 }168 169 /* create read pthreadi */170 ret = pthread_create(&rtest_taskid, NULL, (void *)test_read, NULL);171 if (ret) {172 printf("%s failed, errno:%d\n", __FUNCTION__, errno);173 }174 175 pthread_attr_destroy(&attr);176 177 ret = pthread_join(wtest_taskid, (void **)&retval);178 printf("%d %d\n", ret, (int *)retval);179 if (ret == 0 && (int *)retval == 0) {180 if (pthread_kill(rtest_taskid, 0) == 0)181 pthread_cancel(rtest_taskid);182 }183 184 printf("pthread exit success!\n");185 }186 187 int main(int argc, char **argv)188 {189 test();190 191 pause();192 return 0;193 }

 

转载于:https://www.cnblogs.com/hancq/p/5360587.html

你可能感兴趣的文章
如何更改Android的默认虚拟机地址(Android virtual driver路径设置)
查看>>
Python内置函数(36)——iter
查看>>
HTML标签_1
查看>>
jsp组成元素
查看>>
排序算法(转)
查看>>
windows自带的可生成各种数据库连接字符串工具打开方法
查看>>
Python命名规范
查看>>
滚动条
查看>>
程序员的自我修养九Windows下的动态链接
查看>>
Codeforces Round #361 (Div. 2)
查看>>
细说WebSocket - Node篇
查看>>
jenkins+testNG
查看>>
[洛谷1485] 火枪打怪
查看>>
PAT B1018.锤子剪刀布(20)
查看>>
Extjs控件之 grid打印功能
查看>>
枚举类型(不常用)递归
查看>>
ETL
查看>>
Tomcat源码分析(六)--日志记录器和国际化
查看>>
YII缓存依赖的应用
查看>>
决策树在机器学习的理论学习与实践
查看>>