不要使用POSIX线程库获得线程的优先级和策略... [英] don't get priority and policy of thread using POSIX thread lib...
本文介绍了不要使用POSIX线程库获得线程的优先级和策略...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
hi guys, this is my code
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#define THREAD_ID pthread_self()
#define PRIORITY1 4
#define PRIORITY2 3
int count = 0;
int policy;
pthread_t thread1, thread2;
pthread_attr_t attr1, attr2, attr;
struct sched_param param1, param2;
struct sched_param param;
pthread_mutex_t mutex;
/*
* === FUNCTION ======================================================================
* Name: fnThread
* Description:
* =====================================================================================
*/
void*
fnThread()
{
while(count < 10) {
pthread_mutex_lock(&mutex);
pthread_getschedparam(THREAD_ID, &policy, ¶m);
printf("thread: %lu, priority: %d, policy: %d, count: %d\n",
(unsigned long)THREAD_ID, param.sched_priority, policy, count++);
pthread_mutex_unlock(&mutex);
sleep(1);
}
} /* ----- end of function fnThread ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int
main ( int argc, char *argv[] )
{
pthread_mutex_init(&mutex, NULL);
//init attributes
pthread_attr_init(&attr);
pthread_attr_init(&attr2);
//set policy
pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);
//set priority
param1.sched_priority = PRIORITY1;
pthread_attr_setschedparam(&attr1, ¶m1);
param2.sched_priority = PRIORITY2;
pthread_attr_setschedparam(&attr2, ¶m2);
//create thread
pthread_create(&thread1, &attr1, &fnThread, NULL);
pthread_create(&thread2, &attr2, &fnThread, NULL);
//join
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_attr_destroy(&attr1);
pthread_attr_destroy(&attr2);
pthread_mutex_destroy(&mutex);
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
main函数包含2个线程,所有线程仅执行函数 fnThread .功能任务是将线程的线程ID ,优先级和策略打印到屏幕上(10次).
结果:
main function contains 2 threads, all of threads execute only function fnThread. Task of function is printing thread id, priority and policy of thread onto screen (10 times).
and result:
thread: 3078544240, priority: 0, policy: 0, count: 0
thread: 3070151536, priority: 0, policy: 0, count: 1
thread: 3078544240, priority: 0, policy: 0, count: 2
thread: 3070151536, priority: 0, policy: 0, count: 3
thread: 3078544240, priority: 0, policy: 0, count: 4
thread: 3070151536, priority: 0, policy: 0, count: 5
thread: 3078544240, priority: 0, policy: 0, count: 6
thread: 3070151536, priority: 0, policy: 0, count: 7
thread: 3070151536, priority: 0, policy: 0, count: 8
thread: 3078544240, priority: 0, policy: 0, count: 9
这是意外的结果.而是在 thread1 中将 priority 显示为 3 (或在 thread2 中显示 4 ),并且将 policy 显示为 1 (SCHED_FIFO定义为1),但都等于0.为什么没有确切获得线程的优先级和策略?
this is unexpected result. Instead displays priority as 3 in thread1 (or 4 in thread2), and display policy as 1 (SCHED_FIFO is defined as 1),but all equal 0. Why don''t get exactly priority and policy of thread? Please help me, thanks so much!
推荐答案
可以检查pthread_attr_setschedparam的结果是否正确吗?
也许永远不会设置该值.
Can you check if the result of pthread_attr_setschedparam is ok or not?
Maybe it is never setting the value.
这篇关于不要使用POSIX线程库获得线程的优先级和策略...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文