title: Why setting of the scheduled policy and parameter is failing in my program - pthread Linux?
body: <p>My PC has the following characteristics - </p>
  
  <pre><code># chrt -m
  SCHED_OTHER min/max priority    : 0/0
  SCHED_FIFO min/max priority : 1/99
  SCHED_RR min/max priority   : 1/99
  SCHED_BATCH min/max priority    : 0/0
  SCHED_IDLE min/max priority : 0/0
  </code></pre>
  
  <p>Here is the program -</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;iostream&gt;
  /** get pid **/
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  /** kill signal **/
  #include &lt;signal.h&gt;
  
  using namespace std;
  
  int shared_variable = 0;
  
  
  class helium_thread
  {
    private:
    pthread_t *thread_id;
    pid_t process_pid;
  
    public:
    static pthread_mutex_t mutex_thread;
    static pthread_cond_t cond_var;
    void set_thread_id(pthread_t tid);
    pthread_t *get_thread_id();
    int create_thread(pthread_t *thread_ptr, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg );
    helium_thread();  
    ~helium_thread();
  
  };
  
  helium_thread thread_1, thread_2;
  /** The definition of the static member can't be inside a function, You need to put it outside **/
  /** When I tried using inside a function, I got the error - error: invalid use of qualified-name ‘helium_thread::mutex_thread **/
  
  pthread_mutex_t helium_thread::mutex_thread = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t  helium_thread::cond_var = PTHREAD_COND_INITIALIZER;
  
  
  void helium_thread::set_thread_id( pthread_t tid)
  {
     *(this-&gt;thread_id) = tid;    
  }
  
  pthread_t * helium_thread::get_thread_id( )
  {
     return (this-&gt;thread_id);
  }
  
  int helium_thread::create_thread(pthread_t *thread_ptr, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg )
  {
     int ret;
     ret = pthread_create(thread_ptr,attr,start_routine,(void *)arg)  ;
     return ret;
  
  }
  
  helium_thread::helium_thread()
  {
  
      thread_id = new pthread_t;
  }
  
  helium_thread::~helium_thread()
  {
      delete thread_id;
  }
  
  
  /** While defining the methods of the class, Keywords static and virtual should not be repeated in the definition. **/
  /** They should only be used in the class declaration. **/
  
  /** remember this **/
  
  #if 0
  enum schedparam_policy {
      SCHED_RR,
      SCHED_IO,
      SCHED_FIFO,
      SCHED_OTHER
  };
  
  struct sched_param {
  int sched_priority;     
  /* Scheduling priority */
  };
  
  #endif
  
  /*** End here ***/
  
  void print_sched_priority()
  {
  
      int rc;
     struct sched_param sched_param;
     int  policy;
  
     rc = pthread_getschedparam(pthread_self(), &amp;policy, &amp;sched_param);
  
     if( rc != 0)
       {
        cout&lt;&lt;"Getting Scheduling paramter failed"&lt;&lt;endl;   
       }
     else
     {
       cout&lt;&lt;"The priority of Thread_Func_A is "&lt;&lt; sched_param.sched_priority&lt;&lt;endl;  
       const char *policy_name = (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER":"??";
       cout&lt;&lt;"The policy of Thread_Func_A is "&lt;&lt; policy_name&lt;&lt;endl;
       cout&lt;&lt;"The max priority is "&lt;&lt;sched_get_priority_max(policy)&lt;&lt;endl;
       cout&lt;&lt;"The min priority is "&lt;&lt;sched_get_priority_min(policy)&lt;&lt;endl;
  
     }
  }
  
  
  void set_sched_priority()
  {
  
     int rc;
     struct sched_param sched_param;
     int  policy;
  
     policy = SCHED_RR;
  
     /** highest priority **/
     sched_param.sched_priority = 0; 
  
     /** with chrt -m or sched_get_priority_max, I get to know the priority range **/
  
     rc = pthread_setschedparam(pthread_self(), policy, &amp;sched_param);
  
     if( rc != 0)
       {
        cout&lt;&lt;"Setting Scheduling paramter failed"&lt;&lt;endl;   
       }
     else
     {
       cout&lt;&lt;"The priority of Thread_Func_A is "&lt;&lt; sched_param.sched_priority&lt;&lt;endl;  
       const char *policy_name = (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER":"??";
       cout&lt;&lt;"The policy of Thread_Func_A is "&lt;&lt; policy_name&lt;&lt;endl;
  
     }
  }
  
  void *Thread_Function_A(void *thread_arg)
  {
    print_sched_priority();
    set_sched_priority();
    print_sched_priority();
  
    #if 0
  
    while(shared_variable &lt; 10)
    {
       /** print only odd numbers **/
      pthread_mutex_lock(&amp;(helium_thread::mutex_thread));
  
  
      if ( (shared_variable % 2 ) == 0 )
      {
       /** It means the even number; this thread should not print even numbers **/    
       pthread_cond_wait(&amp;helium_thread::cond_var, &amp;helium_thread::mutex_thread);
  
      }
  
     cout&lt;&lt;"Thread A shared_variable ..odd = "&lt;&lt; shared_variable&lt;&lt;endl;  
     shared_variable++;
     pthread_mutex_unlock(&amp;(helium_thread::mutex_thread));
  
   }
   #endif
      pthread_exit(NULL);
  }
  
  void thread_b_cleanup (void* buffer)
  {
    cout&lt;&lt;"Clean up for thread_b"&lt;&lt;endl;
    delete[] (int *)buffer;
  }
  
  void *Thread_Function_B(void *thread_arg)
  {
  
     void *thread_b_resource = new int[5];    
     pthread_cleanup_push (thread_b_cleanup, thread_b_resource);
  
    while(shared_variable &lt; 10)
    { 
       pthread_mutex_lock(&amp;(helium_thread::mutex_thread));
  
       if ( (shared_variable % 2 ) != 0 )
       {
          /** This means odd **/ 
          pthread_cond_signal (&amp;helium_thread::cond_var);
       }
  
       else
       {
  
                cout&lt;&lt;"Thread B prints shared_variable--even = "&lt;&lt; shared_variable&lt;&lt;endl; 
                shared_variable++;
  
       }
  
  
    pthread_mutex_unlock(&amp;(helium_thread::mutex_thread));
  
  
   }
    pthread_cleanup_pop (1);
    pthread_exit(NULL);
  
  }
  
  
  int main(int argc, char *argv[])
  {
     pid_t thread_pid_val = getpid();
     thread_1.create_thread((thread_1.get_thread_id()), NULL,Thread_Function_A,&amp;thread_pid_val);
     //thread_2.create_thread((thread_2.get_thread_id()), NULL,Thread_Function_B,&amp;thread_pid_val);
     pthread_join( *(thread_1.get_thread_id()), NULL);
     //pthread_join( *(thread_2.get_thread_id()), NULL);
  
     return  0;   
  }
  </code></pre>
  
  <p>The output is the following -</p>
  
  <pre><code># ./thread_basic.out 
  The priority of Thread_Func_A is 0
  The policy of Thread_Func_A is SCHED_OTHER
  The max priority is 0
  The min priority is 0
  Setting Scheduling paramter failed
  The priority of Thread_Func_A is 0
  The policy of Thread_Func_A is SCHED_OTHER
  The max priority is 0
  The min priority is 0
  </code></pre>
  
  <p>The setting of the scheduling fails. What can I do to fix it?</p>