I have created two threads and i want to see their priority after they are created :- for this I have written the following code :-
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <errno.h>
int main()
{ //something
struct sched_param main_param, t1_param, t2_param;
pthread_t t1, t2;
int *sched1, *sched2;
if (pthread_create(&t1, NULL, fn1, NULL) != 0) // inside fn1 thread sleeps in loop
{
std::cout << "couldn t create t1" << std::endl;
return -1;
}
if (pthread_create(&t2, NULL, fn2, NULL) != 0) //inside fn2 thread sleeps in loop
{
std::cout << "couldn t create t2" << std::endl;
return -1;
}
if (pthread_getschedparam(t1, sched1, &t1_param) != 0)
{
std::cout << "error setting priority for T1: (" << errno << "), " <<
strerror(errno) << std::endl;
}
std::cout << "t1 thread will have a prio of " << t1_param.sched_priority << std::endl;
if (pthread_getschedparam(t1, sched2, &t1_param) != 0)
{
std::cout << "error setting priority for T1: (" << errno << "), " <<
strerror(errno) << std::endl;
}
std::cout << "t2 thread will have a prio of " << t2_param.sched_priority <<std::endl;
// something
}
只有当我添加与 pthread_gettshedparam
有关的代码时, 我才会得到此代码的分割断断, 否则代码还能工作 。 我在这里做错了什么吗? 我甚至尝试用 NULL
替换错误 ched1
和 sched2
。