Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Posix thread priority and scheduling..

I would like to create 4 threads, with the following attributes

SCHED_FIFO

thread 1 has priority value of 99

thread 2 of 99

thread 3 of 50

thread 4 of 20

(where the higher the number, the higher the priority. thus higher priority thread should preempt lower priority thread)

So I wrote this code:

    pthread_attr_t thread_attribute;
    pthread_t      thread_id;
    struct sched_param thread_param;
    
    thread_param.sched_priority = 40;
    
    printf("priority %d\n", thread_param.sched_priority);
    
    pthread_attr_init( &thread_attribute );
    pthread_attr_setdetachstate( &thread_attribute, PTHREAD_CREATE_DETACHED );
    pthread_attr_setschedpolicy( &thread_attribute, SCHED_FIFO );
    pthread_attr_setschedparam(  &thread_attribute, &thread_param );
    printf("current prio: %d\n", pthread_attr_getschedparam(  &thread_attribute, &thread_param ));
    sched_setscheduler( 0, SCHED_FIFO, &thread_param );
    printf("current prio: %d\n", pthread_attr_getschedparam(  &thread_attribute, &thread_param ));

What I see on the screen is the prority value is 0 and 0. Am I setting the thread priority value the right way? Has anyone been able to change the scheduling policy and thread's priority?

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi ShaChris23,

    > printf("current prio: %d\n", pthread_attr_getschedparam( &thread_attribute, &thread_param ));

    pthread_attr_getschedparam doesn't return the _priority_, it returns zero on success, non-zero

    otherwise. You need to look at the returned thread_param.

    Regards,

    --Scott