Printing the default thread's priority

北战南征 提交于 2021-01-29 07:10:08

问题


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);
}

回答1:


I didn't find any statement which allows me to store and print its default priority.

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);


来源:https://stackoverflow.com/questions/62898977/printing-the-default-threads-priority

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!