pthread_create and priorities pthread_attr_setschedparam

来源:互联网 发布:海岛奇兵极品神像数据 编辑:程序博客网 时间:2024/05/17 18:23

pthread_create and priorities

I want to start up a pthread with a given priority. The documentation seems
clear enough, however, the code I wrote doesn't work. The thread always
starts with a default priority. I can use a similar procedure within the
thread to adjust priority and that works. What have I overlooked? I am
running DLL 1.3.2 on Win NT (Patch 5.0, I believe).

THE CODE:
******** FILE: schedTest.c ******************

#include <pthread.h>

char *sched_type[] = {
"SCHED_OTHER",
"SCHED_FIFO",
"SCHED_RR"
};

void pthread_info(void){
pthread_t thread;
struct sched_param sp;
int type;
int rc;
thread = pthread_self();
rc=pthread_getschedparam(thread, &type, &sp);
printf("Thread id:%x %s Priority Max:%d, Current:%d, Min:%d/n",
thread,
sched_type[type],
sched_get_priority_max(type),
sp.sched_priority,
sched_get_priority_min(type));
}

pthread_t t1;
void mytest(void) {
puts("Mytest1");
/*print thread info*/
pthread_info();
}


int main(void) {
pthread_attr_t attr;
struct sched_param parm;
puts("Hello world!");
pthread_attr_init(&attr);
parm.sched_priority=-3;
pthread_attr_setschedpolicy(&attr,SCHED_FIFO);
pthread_attr_setschedparam(&attr,&parm);
pthread_create(&t1, &attr, (void *) mytest, NULL);
puts("That's all folks");
pthread_join(t1,NULL);
return 0;
}

THE OUTPUT:
**************************
Administrator@OLE300GL /home/testy/FW_test
$ gcc schedTest.c

Administrator@OLE300GL /home/testy/FW_test
$ a
Hello world!
That's all folks
Mytest1
Thread id:a010500 SCHED_FIFO Priority Max:-14, Current:0, Min:15
原创粉丝点击