C++ 尝试显示我的线程优先级时出现分段错误

C++ 尝试显示我的线程优先级时出现分段错误,c++,pthreads,C++,Pthreads,我已经创建了两个线程,我想在创建它们之后查看它们的优先级:- 为此,我编写了以下代码:- #include <iostream> #include <unistd.h> #include <pthread.h> #include <sched.h> #include <string.h> #include <errno.h> int main() { //something

我已经创建了两个线程,我想在创建它们之后查看它们的优先级:- 为此,我编写了以下代码:-

   #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 
 } 
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{//
结构sched_param main_param、t1_param、t2_param;
pthread_t t1,t2;
int*sched1,*sched2;
if(pthread_create(&t1,NULL,fn1,NULL)!=0)//内部fn1线程在循环中休眠
{

std::cout您声明了类型为
int*
的指针
sched1
sched2
,但从未初始化这些指针。您需要分配
int
变量,然后将指针传递给这些变量

像这样:

int sched1, sched2;
.....
if (pthread_getschedparam(t1, &sched1, ....

您声明了
int*
类型的指针
sched1
sched2
,但从未初始化这些指针。您需要分配
int
变量,然后将指针传递给这些变量

像这样:

int sched1, sched2;
.....
if (pthread_getschedparam(t1, &sched1, ....
int*sched1; pthread_getschedparam(t1,sched1,&t1_param);//sched1是未初始化的指针! 考虑这一变化:

int sched1; pthread_getschedparam(t1, &sched1, &t1_param); int sched1; pthread_getschedparam(t1,&sched1,&t1_参数); int*sched1; pthread_getschedparam(t1,sched1,&t1_param);//sched1是未初始化的指针! 考虑这一变化:

int sched1; pthread_getschedparam(t1, &sched1, &t1_param); int sched1; pthread_getschedparam(t1,&sched1,&t1_参数);