top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How can you change the priority of threads?

+4 votes
145 views

Assume we have three threads and in this case how can we change the priority of threads. If it is possible then please explain with example.

posted Sep 21, 2013 by Giri Prasad

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

+2 votes

By using setPriority() method of Thread class.

answer Oct 3, 2013 by Dheerendra Dwivedi
+1 vote

Dont know what is your real problem, having said that let me try to say something.

Linux supports several scheduling policies: SCHED_OTHER, the default scheduling policy, with round-robin sharing; SCHED_BATCH for batch processing; SCHED_IDLE for extremely low priority (background) jobs; and the two real-time priority policies SCHED_FIFO and SCHED_RR for FIFO and round-robin, respectively. In real-time scheduling policies, threads have a priority from 1 to 99, and higher priority threads always preempt lower priority threads. The order among waiting threads with the same priority is determined by the policy (FIFO or round-robin).

Two alternatives exist to set thread scheduling policy and priority: using chrt from the command line, and POSIX threads (also known as pthreads). For chrt, the chrt man page is a useful reference. Below are details and references for the use of pthreads.

The following code sets a thread's real-time policy to SCHED_FIFO, with the maximum allowable priority. It uses the headers pthread.h and sched.h

#include <pthread.h>
#include <sched.h>

We'll set the priority of the current thread (via pthread_self), although it's possible to set other threads' priorities.

void set_realtime_priority() {
    int ret;

    // We'll operate on the currently running thread.
    pthread_t this_thread = pthread_self();

Setting thread priority is done through struct sched_param, which contains a sched_priority member. It's possible to query the maximum and minimum priorities for a policy.

// struct sched_param is used to store the scheduling priority
struct sched_param params;
// We'll set the priority to the maximum.
params.sched_priority = sched_get_priority_max(SCHED_FIFO);

Setting thread priority is one call to pthread_setschedparam.

std::cout << "Trying to set thread realtime prio = " << params.sched_priority << std::endl;

// Attempt to set thread real-time priority to the SCHED_FIFO policy
ret = pthread_setschedparam(this_thread, SCHED_FIFO, &params);
if (ret != 0) {
    // Print the error
    std::cout << "Unsuccessful in setting thread realtime prio" << std::endl;
    return;
}

In some cases it makes sense to verify the thread priority and policy

    // Now verify the change in thread priority
    int policy = 0;
    ret = pthread_getschedparam(this_thread, &policy, &params);
    if (ret != 0) {
        std::cout << "Couldn't retrieve real-time scheduling paramers" << std::endl;
        return;
    }

    // Check the correct policy was applied
    if(policy != SCHED_FIFO) {
        std::cout << "Scheduling is NOT SCHED_FIFO!" << std::endl;
    } else {
        std::cout << "SCHED_FIFO OK" << std::endl;
    }

    // Print thread scheduling priority
    std::cout << "Thread priority is " << params.sched_priority << std::endl;
}

Hope it helps

answer Sep 21, 2013 by anonymous
Hi my doubt is 3 threads are thr in tht how you can make Priority of those threads?
Similar Questions
+4 votes

in case if is it possible, how you can change and pls explain with some example.

+1 vote

I eager to know how watch point works in detail ?

+3 votes

How priority is initialized in RTOS? who will initiate priority for scheduling?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...