(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 <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <pthread.h> 5 #include <sched.h> 6 #include <limits.h> 7 #include <sys/prctl.h> 8 #include <errno.h> 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 ", pthread_self()); 18 19 while(1) { 20 printf("w:usr_date:%d ", 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 ", pthread_self()); 36 37 while(1) { 38 printf("r:usr_date:%d ", 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 "); 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 ", 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 "); 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 "); 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 "); 96 return rval; 97 } 98 99 /* first get the scheduling parameter, then set the new priority */ 100 rval = pthread_attr_getschedparam(attr, ¶ms); 101 if (rval != 0) 102 { 103 printf("pthread_attr_getschedparam failed "); 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 ", 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, ¶ms); 121 if (rval != 0) 122 { 123 printf("pthread_attr_setschedparam failed "); 124 return rval; 125 } 126 127 /* when set stack size, we define a minmum value to avoid fail */ 128 printf("PTHREAD_STACK_MIN:%x ", 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 "); 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 ", __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 ", __FUNCTION__, errno); 173 } 174 175 pthread_attr_destroy(&attr); 176 177 ret = pthread_join(wtest_taskid, (void **)&retval); 178 printf("%d %d ", 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! "); 185 } 186 187 int main(int argc, char **argv) 188 { 189 test(); 190 191 pause(); 192 return 0; 193 }