Hi all,

My program have two threads (parent thread & child thread).
I want to set a high priority to my child thread when it is created. So I just follow the example given in the follow (http://cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-12.htm)


**************************************************

#define _MULTI_THREADED
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include "check.h"

#define BUMP_PRIO 1
static int thePriority = 0;

void showSchedParam(pthread_attr_t *a)
{
  int                 rc=0;
  struct sched_param  param;

  printf("Get scheduling parameters\n");
  rc = pthread_attr_getschedparam(a, &param);
  checkResults("pthread_attr_getschedparam()\n", rc);

  printf("The thread attributes object indicates priority: %d\n",
         param.sched_priority);
  thePriority = param.sched_priority;
  return;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  pthread_attr_t        pta;
  struct sched_param    param;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create a thread attributes object\n");
  rc = pthread_attr_init(&pta);
  checkResults("pthread_attr_init()\n", rc);

  showSchedParam(&pta);

  memset(&param, 0, sizeof(param));
  if (thePriority + BUMP_PRIO <= PRIORITY_MAX_NP) {
    param.sched_priority = thePriority + BUMP_PRIO;
  }

  printf("Setting scheduling parameters\n");
  rc = pthread_attr_setschedparam(&pta, &param);
  checkResults("pthread_attr_setschedparam()\n", rc);

  showSchedParam(&pta);

  printf("Destroy thread attributes object\n");
  rc = pthread_attr_destroy(&pta);
  checkResults("pthread_attr_destroy()\n", rc);

  printf("Main completed\n");
  return 0;
}

**************************************************

but in the above example, there is no any new thread created. So I just change this as follow



**************************************************

pthread_t client_thread;
int client_rc;
int (*client_fp)(void *);	

struct sched_param param;
int rc;

pthread_attr_t attr;
pthread_mutex_init(&mutex, NULL);  
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

showSchedParam(&attr);
memset(&param, 0, sizeof(param));
param.sched_priority = 10;

rc = pthread_attr_setschedparam(&attr, &param);
showSchedParam(&attr);

    client_fp = calc_available_bandwidth;	
    client_rc = pthread_create(&client_thread, &attr, client_fp, (void *)dst_addres);
    if (client_rc)
    {
      fprintf(stderr, "ERROR; return code from pthread_create()[CLIENT] is %d\n", client_rc);
      exit(-1);
    }

showSchedParam(&attr);
pthread_attr_destroy(&attr);

**************************************************

But still I could not get the result that I am expected to have (High priority for child thread)

please can anyone tell me whether this is correct or not (setting a priority when create a new thread)

Also I would like to know the value should I set for "param.sched_priority " to have high priority....? (Positive values or negative values)

Increasing priority for a thread is not a good idea because it may halt all other threads, including the operating system itself.

So in which type of situations do we use "pthread_attr_setschedparam()" ......?

Please tell me what should I do to solve my problem....?

Try setting them with the same priority. Does this work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.