Thread-priodities

Regan Heath regan at netmail.co.nz
Wed Aug 22 02:10:24 PDT 2007


Charma wrote:
> Hello,
> i am currently working on a little programm which will use threads... I 
> want to start two threads, one with the same priority as the programm 
> itself and one with very low priority.
> As i found in phobos, there is this setPriority() function but i can't 
> input a number or somthing in there only INCREASE and DECREASE and so on...
> Does anyone know how many steps there are from lowest to highest 
> priority and how to use this function? if i want to lower the priority 
> of one thread by two, do i need to run the function twice with decrease 
> or will it end up the same?!
> THanks for any help, Sorry for my engrish :)

If you look in dmd\src\phobos\std\thread.d you will find:

     void setPriority(PRIORITY p)
     {
	int nPriority;

	switch (p)
	{
	    case PRIORITY.INCREASE:
		nPriority = THREAD_PRIORITY_ABOVE_NORMAL;
		break;
	    case PRIORITY.DECREASE:
		nPriority = THREAD_PRIORITY_BELOW_NORMAL;
		break;
	    case PRIORITY.IDLE:
		nPriority = THREAD_PRIORITY_IDLE;
		break;
	    case PRIORITY.CRITICAL:
		nPriority = THREAD_PRIORITY_TIME_CRITICAL;
		break;
	    default:
		assert(0);
	}

	if (SetThreadPriority(hdl, nPriority) == THREAD_PRIORITY_ERROR_RETURN)
	    error("set priority");
     }

It seems the std.thread.setPriority function only supports 4 priority 
modes.  SetThreadPriority, the windows api function supports:

[THREAD_PRIORITY_ABOVE_NORMAL]
Indicates 1 point above normal priority for the priority class.

[THREAD_PRIORITY_BELOW_NORMAL]
Indicates 1 point below normal priority for the priority class.

[THREAD_PRIORITY_HIGHEST]
Indicates 2 points above normal priority for the priority class.

[THREAD_PRIORITY_IDLE]
Indicates a base priority level of 1 for
IDLE_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, 
ABOVE_NORMAL_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS processes, and a 
base priority level of 16 for REALTIME_PRIORITY_CLASS processes.

[THREAD_PRIORITY_LOWEST]
Indicates 2 points below normal priority for the priority class.

[THREAD_PRIORITY_NORMAL]
Indicates normal priority for the priority class.

[THREAD_PRIORITY_TIME_CRITICAL]
Indicates a base priority level of 15 for IDLE_PRIORITY_CLASS, 
BELOW_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, 
ABOVE_NORMAL_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS processes, and a 
base priority level of 31 for REALTIME_PRIORITY_CLASS processes.


So, the THREAD_PRIORITY_HIGHEST and THREAD_PRIORITY_LOWEST options are 
not available with std.thread.setPriority.

You can call SetThreadPriority yourself if you want:

import std.c.windows.windows;
import std.thread;

void main()
{
	auto t = new Thread();
	...
	SetThreadPriority(t.hdl, THREAD_PRIORITY_LOWEST);
}

Regan


More information about the Digitalmars-d-learn mailing list