thread and PTHREAD_EXPLICIT_SCHED

Hi,

Is it possible to set a thread attr to PTHREAD_EXPLICIT_SCHED after the
thread creation (with pthread_create)? (I can’t find the correct function
in doc)
I use the Qt3 QThread class wich hard set this attribute to
PTHREAD_INHERIT_SCHED so I can’t change this thread priority.

(another solution is to recompile Qt libs but I have some prob. with that)

thanks

Johan CARDON
supprimer .SPAMOUT pour répondre par mail

Johan CARDON <johan.cardon@imag.fr.spamout> wrote:

Hi,

Is it possible to set a thread attr to PTHREAD_EXPLICIT_SCHED after the
thread creation (with pthread_create)? (I can’t find the correct function
in doc)
I use the Qt3 QThread class wich hard set this attribute to
PTHREAD_INHERIT_SCHED so I can’t change this thread priority.

The thread attribute structure is only meaningful for thread creation,
and it specifies how to create the thread. Once the thread is created
you can explicitly change its priority and scheduling algorithm to anything
you want by calling pthread_setschedparam().

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs wrote:

Johan CARDON <> johan.cardon@imag.fr.spamout> > wrote:
Hi,

Is it possible to set a thread attr to PTHREAD_EXPLICIT_SCHED after the
thread creation (with pthread_create)? (I can’t find the correct function
in doc)
I use the Qt3 QThread class wich hard set this attribute to
PTHREAD_INHERIT_SCHED so I can’t change this thread priority.

The thread attribute structure is only meaningful for thread creation,
and it specifies how to create the thread. Once the thread is created
you can explicitly change its priority and scheduling algorithm to
anything you want by calling pthread_setschedparam().

-David

Thank you for your response David,

But I’ve previously read this news : http://groups.google.fr/
groups?hl=fr&lr=&ie=UTF-8&selm=tg34g5fn0ugh62%40corp.supernews.com
In wich Mario CHAREST say that if the attr is not at PTHREAD_EXPLICIT_SCHED
the priority set with pthread_setschedparam() won’t take. And this is the
case in my App. pthread_setschedparam() return without error but the new
priority isn’t set.

I think I’ve two solutions :
First, recompile Qt3 libs with the good attr in the source of Qthread class.

Or, a more simple solution given to me by David BUTENHOF on
comp.programming.threads :
Modify my own priority before create the thread and restore them after
creation. I will test it today.

thank again

Johan CARDON
supprimer .SPAMOUT pour répondre par mail

Johan CARDON <johan.cardon@imag.fr.spamout> wrote:

David Gibbs wrote:

Johan CARDON <> johan.cardon@imag.fr.spamout> > wrote:
Hi,

Is it possible to set a thread attr to PTHREAD_EXPLICIT_SCHED after the
thread creation (with pthread_create)? (I can’t find the correct function
in doc)
I use the Qt3 QThread class wich hard set this attribute to
PTHREAD_INHERIT_SCHED so I can’t change this thread priority.

The thread attribute structure is only meaningful for thread creation,
and it specifies how to create the thread. Once the thread is created
you can explicitly change its priority and scheduling algorithm to
anything you want by calling pthread_setschedparam().

-David



Thank you for your response David,

But I’ve previously read this news : > http://groups.google.fr/
groups?hl=fr&lr=&ie=UTF-8&selm=tg34g5fn0ugh62%40corp.supernews.com
In wich Mario CHAREST say that if the attr is not at PTHREAD_EXPLICIT_SCHED
the priority set with pthread_setschedparam() won’t take. And this is the
case in my App. pthread_setschedparam() return without error but the new
priority isn’t set.

Are you sure he didn’t say that pthread_attr_setschedparam won’t take unless
you set PTHREAD_EXPLICIT_SCHED?

My demonstration:


/*

  • thread1.c

*/

#include <stdio.h>
#include <sys/neutrino.h>
#include <pthread.h>
#include <sched.h>


void *update_thread (void *);

char *progname = “thread1”;

main ()
{
pthread_attr_t attrib; // scheduling attributes
struct sched_param param; // for setting priority
int i;
pthread_t threadID;

var1 = var2 = 0; /* initialize to known values */

printf ("%s: starting; creating threads\n", progname);

/*

  • we want to create the new threads using Round Robin
  • scheduling, so set up a thread attributes structure
    */

pthread_attr_init (&attrib);
pthread_attr_setinheritsched (&attrib, PTHREAD_INHERIT_SCHED);
attrib.flags |= PTHREAD_CANCEL_ASYNCHRONOUS;

/*

  • create the threads. As soon as each pthread_create
  • call is done, the thread has been started.
    */

pthread_create (&threadID, &attrib, &update_thread, (void *) i);

param.sched_priority = 8;
pthread_setschedparam( threadID, SCHED_RR, &param );

sleep (40);
exit (0);
}

void *
update_thread (void *i)
{
while (1) {
sched_yield();
}
return (NULL);
}

After this has started, pidin gives:


pid tid name prio STATE Blocked
5652500 1 thread1 10o NANOSLEEP
5652500 2 thread1 8r READY

As expected.

Remember, though, that threads can change priority due to other things,
in particular if they are receiving messages or pulses on a channel, they
will run at the priority of the sender or of the pulse.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs wrote:

Johan CARDON <> johan.cardon@imag.fr.spamout> > wrote:
David Gibbs wrote:

Johan CARDON <> johan.cardon@imag.fr.spamout> > wrote:
Hi,

Is it possible to set a thread attr to PTHREAD_EXPLICIT_SCHED after the
thread creation (with pthread_create)? (I can’t find the correct
function in doc)
I use the Qt3 QThread class wich hard set this attribute to
PTHREAD_INHERIT_SCHED so I can’t change this thread priority.

The thread attribute structure is only meaningful for thread creation,
and it specifies how to create the thread. Once the thread is created
you can explicitly change its priority and scheduling algorithm to
anything you want by calling pthread_setschedparam().

-David


Thank you for your response David,

But I’ve previously read this news : > http://groups.google.fr/
groups?hl=fr&lr=&ie=UTF-8&selm=tg34g5fn0ugh62%40corp.supernews.com
In wich Mario CHAREST say that if the attr is not at
PTHREAD_EXPLICIT_SCHED the priority set with pthread_setschedparam()
won’t take. And this is the case in my App. pthread_setschedparam()
return without error but the new priority isn’t set.

Are you sure he didn’t say that pthread_attr_setschedparam won’t take
unless you set PTHREAD_EXPLICIT_SCHED?

My demonstration:


/*

  • thread1.c

*/

#include <stdio.h
#include <sys/neutrino.h
#include <pthread.h
#include <sched.h


void *update_thread (void *);

char *progname = “thread1”;

main ()
{
pthread_attr_t attrib; // scheduling attributes
struct sched_param param; // for setting priority
int i;
pthread_t threadID;

var1 = var2 = 0; /* initialize to known values */

printf ("%s: starting; creating threads\n", progname);

/*

  • we want to create the new threads using Round Robin
  • scheduling, so set up a thread attributes structure
    */

pthread_attr_init (&attrib);
pthread_attr_setinheritsched (&attrib, PTHREAD_INHERIT_SCHED);
attrib.flags |= PTHREAD_CANCEL_ASYNCHRONOUS;

/*

  • create the threads. As soon as each pthread_create
  • call is done, the thread has been started.
    */

pthread_create (&threadID, &attrib, &update_thread, (void *) i);

param.sched_priority = 8;
pthread_setschedparam( threadID, SCHED_RR, &param );

sleep (40);
exit (0);
}

void *
update_thread (void *i)
{
while (1) {
sched_yield();
}
return (NULL);
}

After this has started, pidin gives:


pid tid name prio STATE Blocked
5652500 1 thread1 10o NANOSLEEP
5652500 2 thread1 8r READY

As expected.

Remember, though, that threads can change priority due to other things,
in particular if they are receiving messages or pulses on a channel, they
will run at the priority of the sender or of the pulse.

-David

Effectively,

My thread start a timer wich send a pulse with SIGEV_PULSE_PRIO_INHERIT
priority. If I set this struct. attribute to the priority I want, the
thread get it.

Thank you for demonstration.


Johan CARDON
supprimer .SPAMOUT pour répondre par mail