English 中文(简体)
试图显示线索主线的偏优先度时断分
原标题:segmentation fault while trying to display priority of my thread
  • 时间:2012-05-22 13:33:06
  •  标签:
  • c++
  • pthreads

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

最佳回答

您所申报的指针 ched1 > 和 ched2 类型 _int_%/code> 的 code2 , 但却从未确认这些指针。 您需要分配 int 变量, 然后将指针传递给这些变量 。

像这样:

int sched1, sched2;
.....
if (pthread_getschedparam(t1, &sched1, ....
问题回答
int *sched1;
pthread_getschedparam(t1, sched1, &t1_param); // sched1 is uninitialized pointer!

考虑此更改 :

int sched1;
pthread_getschedparam(t1, &sched1, &t1_param);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签