[LUAU] pthreads priority question

Jim Thompson jim at netgate.com
Tue Jan 25 23:14:59 PST 2005


Lou Rickard wrote:
> I was writing a multi-threaded program, and was using
> pthread_setschedparam() to boost the priority to
> something pseudo-realtime, when I got stumped on a
> question:
> 
> I want my program to run at a higher level priority
> than standard, in fact I want it to run at as high a
> priority level as possible.  I had thought that using
> pthread_setschedparam() would do this.  But then it
> occurred to me that the function may just be running
> my thread at a higher priority level compared to the
> other threads in the parent process, to no real
> benefit of the program overall.  Is that true?

Depends on which threading library you're using.  Hopefully you've got 
something modern, and it implements native (kernel) posix threads.

Note that you're going to have to be (E)UID == 0 in order to increase 
your priority, and/or set the scheduler policy to something other than 
"SCHED_OTHER".

> When I run ps -l, the priority listed for the program
> isn't anything different from everything else.  

If I run the below as non-root, then the scheduler class isn't (can't 
be) set, and you'll see the two threads round robin in the scheduler.

Run it as root, and you'll find that your entire machine seems to be 
"locked up" until thread0 exits, then thread1 gets to run (and your 
system is nice and responsive).

I'm runing:
jim/src> cat /proc/version
Linux version 2.6.10-gentoo-r6 (root at zaphod) (gcc version 3.3.5 (Gentoo 
Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)) #6 SMP Mon Jan 24 21:45:37 
HST 2005

On a "3.2GHz P4 with HT".

Jim

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Thread start routines                                                */
void *user_thread0 (void *);
void *user_thread1 (void *);

int
main (int argc, char *argv[])
{
     int rc;
     pthread_t threads[2];
     pthread_attr_t attr;

     pthread_attr_init(&attr);
     pthread_attr_setdetachstate(&attr,
				PTHREAD_CREATE_DETACHED);

     rc = pthread_create (&threads[1],
			 &attr, user_thread1, (void *) NULL);
     if (rc != 0) {
	perror ("pthread_create first thread");
	exit (1);
     }

     rc = pthread_create (&threads[0], &attr, user_thread0, NULL);
     if (rc != 0) {
	perror ("pthread_create second thread");
	exit (1);
     }

     /* joins the threads */
     pthread_join(threads[0], NULL);
     pthread_join(threads[1], NULL);

     pthread_attr_destroy(&attr);
     pthread_exit(NULL);
}

int
set_my_thread_priority(int priority) {
     struct sched_param sp;
     int policy;

     (void)memset(&sp, 0, sizeof(struct sched_param));
     if (pthread_getschedparam(pthread_self(), &policy, &sp) == -1) {
         fprintf(stderr, "Failed to obtain sched params.\n");
         return -1;
     }
     sp.sched_priority=priority;
     if (pthread_setschedparam(pthread_self(), SCHED_RR, &sp) == -1) {
         fprintf(stderr, "Failed to change priority.\n");
         return -1;
     }
     return 0;
}

void *
user_thread0 (void *ptr)
{
     unsigned int i, j;
     if (set_my_thread_priority(85) < 0) {
	fprintf (stderr, "user_thread0: unable to set priority\n");
	return (NULL);
     }
     for (j = 0; j < 2; j++) {
	for (i = 0;i <= 1000000000; i++) {
	    if ((i % 100000000) == 0)
		fprintf(stderr, "thread0: %d\n", i);
	}
     }
     fprintf (stderr, "user_thread0: exiting\n");
     return (NULL);
}

void *
user_thread1 (void *ptr)
{
     unsigned int i, j;

     if ((set_my_thread_priority(127)) < 0) {
	fprintf (stderr, "user_thread1: unable to set priority\n");
	return (NULL);
     }
     for (j = 0; j < 2; j++) {
	for (i = 0;i <= 1000000000; i++) {
	    if ((i % 100000000) == 0)
		fprintf(stderr, "thread1: %d\n", i);
	}
     }
     fprintf (stderr, "user_thread1: exiting\n");
     return (NULL);
}



More information about the LUAU mailing list