打印默认线程的优先级 [英] Printing the default thread's priority
本文介绍了打印默认线程的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写一个代码来打印默认线程的优先级,但我不知道这是否可行.到目前为止,我创建了一个具有默认属性的线程,但我没有找到任何允许我存储和打印其默认优先级的语句.
I'd like to write a code which prints the default thread's priority, but I don't know if this is possible. So far I created a thread with default attributes, but I didn't find any statement which allows me to store and print its default priority.
// main.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include "task.h"
int main()
{
pthread_attr_t attr;
struct sched_param prio;
pthread_t tid;
int create = 1;
// default attributes
pthread_attr_init(&attr);
create = pthread_create(&tid, &attr, task, NULL);
if (create != 0) exit(EXIT_FAILURE);
pthread_join(tid, NULL);
return(0);
}
// task.h
#ifndef TASK_H
#define TASK_H
void *task();
#endif
// task.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "task.h"
void *task()
{
printf("I am a simple thread.\n");
pthread_exit(NULL);
}
推荐答案
我没有找到任何允许我存储和打印其默认优先级的语句.
I didn't find any statement which allows me to store and print its default priority.
它是 pthread_attr_getschedparam
并且 sched_param
具有调度优先级(至少).
It's pthread_attr_getschedparam
and sched_param
has scheduling priority (at least).
struct sched_param prio;
pthread_attr_getschedparam(&attr, &prio);
printf("sched_priority = %d\n", prio.sched_priority);
这篇关于打印默认线程的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文