Thanks MathGuy and Louie! I think the best solution would be to use by default THREAD_PRIORITY_LOWEST and let the user to choose THREAD_PRIORITY_IDLE.
Here is the code other clients use for setting priority:
a) Sum95 (Euler2000)
Code:
void SetPriority(HANDLE thread)
{
SetPriorityClass (GetCurrentProcess (),
(PRIORITY < 2 || PRIORITY > 6) ?
NORMAL_PRIORITY_CLASS :
IDLE_PRIORITY_CLASS);
SetThreadPriority (thread,
(PRIORITY == 1) ? THREAD_PRIORITY_IDLE :
(PRIORITY == 2 || PRIORITY == 7) ? THREAD_PRIORITY_LOWEST :
(PRIORITY == 3 || PRIORITY == 8) ? THREAD_PRIORITY_BELOW_NORMAL :
(PRIORITY == 4 || PRIORITY == 9) ? THREAD_PRIORITY_NORMAL :
(PRIORITY == 5 || PRIORITY == 10) ? THREAD_PRIORITY_ABOVE_NORMAL :
THREAD_PRIORITY_HIGHEST);
if (CPU_AFFINITY != 99 && !isWindows95 ())
SetThreadAffinityMask (thread, 1 << CPU_AFFINITY);
}
b) Distributed.net 20010416
Code:
#elif (CLIENT_OS == OS_WIN32)
{
static int useidleclass = -1; // track detection state.
int threadprio = 0, classprio = 0;
HANDLE our_thrid = GetCurrentThread(); // Win32 pseudo-handle constant.
if (set_for_thread && (w32ConGetType() & 0xff)=='G')
{ // crunchers in a 'fat-'GUI client always run at idle prio
prio = 0;
}
/*************************** Article ID: Q106253 *******************
process priority class
THREAD_PRIORITY Normal, in Normal, in
Idle Background Foreground High Realtime
_TIME_CRITICAL 15 15 15 15 31
_HIGHEST 6 9 11 15 26
_ABOVE_NORMAL 5 8 10 14 25
_NORMAL 4 7 9 13 24
_BELOW_NORMAL 3 6 8 12 23
_LOWEST 2 5 7 11 22
_IDLE 1 1 1 1 16
********************************************************************/
/* If we want to run with an idle priority *class*, we need to be able
to set a *thread* priority of _TIME_CRITICAL for the main and
window-handler threads, otherwise I/O will be laggy beyond belief.
If we cannot set _TIME_CRITICAL, then we have no choice but to use a
NORMAL_PRIO_CLASS. Such is win32 scheduling; stupid, stupid, stupid.
*/
if (useidleclass == -1) /* haven't selected yet */
{
SetPriorityClass( GetCurrentProcess(), IDLE_PRIORITY_CLASS );
Sleep(1);
SetThreadPriority( our_thrid, THREAD_PRIORITY_TIME_CRITICAL);
if (GetThreadPriority( our_thrid ) == THREAD_PRIORITY_TIME_CRITICAL)
useidleclass = 1;
else
{
useidleclass = 0;
SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );
Sleep(1);
}
SetThreadPriority( our_thrid, THREAD_PRIORITY_NORMAL );
}
if (useidleclass == 1)
{
classprio = IDLE_PRIORITY_CLASS;
if (!set_for_thread) threadprio = THREAD_PRIORITY_TIME_CRITICAL;/* 15 */
else if (prio >= 7) threadprio = THREAD_PRIORITY_HIGHEST; /* 6 */
else if (prio >= 5) threadprio = THREAD_PRIORITY_ABOVE_NORMAL; /* 5 */
else if (prio >= 4) threadprio = THREAD_PRIORITY_NORMAL; /* 4 */
else if (prio >= 3) threadprio = THREAD_PRIORITY_BELOW_NORMAL; /* 3 */
else if (prio >= 2) threadprio = THREAD_PRIORITY_LOWEST; /* 2 */
else /* prio < 2 */ threadprio = THREAD_PRIORITY_IDLE; /* 1 */
}
else /* if (useidleclass == 0) */
{
classprio = NORMAL_PRIORITY_CLASS;
if (!set_for_thread) threadprio = THREAD_PRIORITY_NORMAL; /* 8 */
else if (prio >= 7) threadprio = THREAD_PRIORITY_BELOW_NORMAL; /* 6 */
else if (prio >= 5) threadprio = THREAD_PRIORITY_LOWEST; /* 5 */
else threadprio = THREAD_PRIORITY_IDLE; /* 1 */
}
//SetPriorityClass( GetCurrentProcess(), classprio );
//Sleep(1);
SetThreadPriority( our_thrid, threadprio );
}