title: pthread_create not working properly with pthread_attr_setschedparam
body: <p>I am a novice in thread programming. So my apologies for this seemingly stupid question.</p>
  
  <p>I am trying to create a POSIX thread using pthread_create() using pthread_attr_t. I am trying to set the sched_priority value and put it in the attribute.The code is pasted below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;pthread.h&gt;
  using namespace std;
  
  void* log(void* arg)
  {
  
    FILE *fp = fopen("/tmp/log.txt", "w+");
     if (fp != NULL)
     {
       fputs("Logging with Thread", fp);
      }
     else
     {
      cout &lt;&lt;"file pointer not created" &lt;&lt; endl;
     }
     return 0;
   }
  int main(int argc, char** argv )
  {
  
    pthread_t th;
    pthread_attr_t th_attr;
    int policy;
    struct sched_param thparam;
    memset(&amp;thparam, 0,sizeof(sched_param));
    int retval = pthread_attr_init(&amp;th_attr);
    if (0 != retval)
     {
       cout &lt;&lt;"Attribute initialization failed" &lt;&lt; endl;
     }
  
    thparam.sched_priority = 10;
  
    int ret1 = pthread_attr_setschedparam(&amp;th_attr, &amp;thparam);
    if (0 == ret1)
    {
     cout &lt;&lt;"pthread_attr_setschedparam PASSED" &lt;&lt; endl;
     } 
  
   int ret = pthread_create(&amp;th, &amp;th_attr, log, NULL);
   if (0 != ret)
    {
     cout &lt;&lt;"thread not created" &lt;&lt; endl;      
    }
   else
    {
     cout &lt;&lt;"Thread created" &lt;&lt; endl;
    }
  
    int retval1 = pthread_getschedparam(th, &amp;policy, &amp;thparam);
    if (0 != retval1)
    {
     cout &lt;&lt;"Inside Main::pthread_getschedparam FAILED at start" &lt;&lt; endl;
    }
    else
    {
     cout &lt;&lt;"Inside Main::priority: " &lt;&lt; thparam.sched_priority &lt;&lt; endl;
     cout &lt;&lt;"Inside Main::sched policy: " &lt;&lt; policy &lt;&lt; endl;
    }
    pthread_join(th, NULL);
  
    return (0);
   }
  </code></pre>
  
  <p>Every time I run this program, the thread gets created but the default priority (15 in my case).Since I have set the priority to 10 it should start with 10 priority. I can't understand why this is happening. All the log messages I am printing are as expected and there seem to be no error.
  Can anyone please point out what I am doing wrong in the code?</p>
  
  <p>Thanks!!!!</p>
  
  <p>EDIT:</p>
  
  <p>I seem to have found out a interesting thing.I was not able to set the thread priority during thread creation. But I can change the priority of the thread after it is created. I used the API <code>pthread_setschedparam</code> to set the priority. This time the priority of the thread changed properly. Still can't fathom why this is happening.</p>
  
  <p>I should also mention that I am using an embedded system with ARM arch. I am also setting the scheduler policy to SCHED_RR.</p>
  
  <p>Can some one please explain why this is happening?</p>