0

In Java I know we can set priority's of threads, example:

  Thread t1 = new Thread();  
  t1.setPriority(Thread.MIN_PRIORITY);

I'm looking at the various ways of handling this in C++ and the main differences between Thread priority in Java and C++, does thread priority work in a similar way giving a thread more access to resources over a lower thread? Or in C++ does it just refer to the amount of CPU that thread has access to?

Would this Java implementation be viable in C++?

class thrun implements Runnable
{
      Thread t;
      boolean runn = true;

      thrun(String st,int p) 
      {
    t = new Thread(this,st);
    t.setPriority(p);
    t.start();
      }

      publicvoid run()
      {
    System.out.println("Thread name : " + t.getName());
    System.out.println("Thread Priority : " + t.getPriority());
      }     

}

class priority
{
      publicstaticvoid main(String args[])
      {
     Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
     thrun t1 = new thrun("Thread1",Thread.NORM_PRIORITY + 2);
     thrun t2 = new thrun("Thread2",Thread.NORM_PRIORITY - 2);
     System.out.println("Main Thread : " + Thread.currentThread());
     System.out.println("Main Thread Priority : " + Thread.currentThread().getPriority());
      }
}
  • 4
    [Related](http://stackoverflow.com/questions/18884510/portable-way-of-setting-stdthread-priority-in-c11) – nrussell Mar 17 '16 at 13:24
  • 2
    @nrussell, it's a pure duplicated of this. – SergeyA Mar 17 '16 at 13:26
  • Refer to your OS - that is where the priorities are cosidered by the scheduler/dispatcher. In general, thread priority is one value considered by the scheduling algorithm when deciding with ready threads to run in the case of the box having more ready threads than cores available to run them. – Martin James Mar 17 '16 at 13:31

0 Answers0