• 线程优先级


    通过SetThreadPriority可以设置线程优先级别:

    WINBASEAPI
    BOOL
    WINAPI
    SetThreadPriority(
      _In_ HANDLE hThread,     //线程句柄
      _In_ int nPriority                   //设置的权限级别
    ); 

    MSDN:

    PriorityMeaning
    THREAD_MODE_BACKGROUND_BEGIN
    0x00010000

    Begin background processing mode. The system lowers the resource scheduling priorities of the thread so that it can perform background work without significantly affecting activity in the foreground.

    This value can be specified only if hThread is a handle to the current thread. The function fails if the thread is already in background processing mode.

    Windows Server 2003:This value is not supported.

    THREAD_MODE_BACKGROUND_END
    0x00020000

    End background processing mode. The system restores the resource scheduling priorities of the thread as they were before the thread entered background processing mode.

    This value can be specified only if hThread is a handle to the current thread. The function fails if the thread is not in background processing mode.

    Windows Server 2003:This value is not supported.

    THREAD_PRIORITY_ABOVE_NORMAL
    1

    Priority 1 point above the priority class.

    THREAD_PRIORITY_BELOW_NORMAL
    -1

    Priority 1 point below the priority class.

    THREAD_PRIORITY_HIGHEST
    2

    Priority 2 points above the priority class.

    THREAD_PRIORITY_IDLE
    -15

    Base priority 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 of 16 for REALTIME_PRIORITY_CLASS processes.

    THREAD_PRIORITY_LOWEST
    -2

    Priority 2 points below the priority class.

    THREAD_PRIORITY_NORMAL
    0

    Normal priority for the priority class.

    THREAD_PRIORITY_TIME_CRITICAL
    15

    Base priority 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 of 31 for REALTIME_PRIORITY_CLASS processes.

    // 线程优先级.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <Windows.h>  
    DWORD WINAPI ThreadProcIdle(LPVOID lpParameter);
    DWORD WINAPI ThreadProcNormal(LPVOID lpParameter);
    
    
    int main()
    {
    	DWORD ThreadPriority_Idle;
    	DWORD ThreadIdPriority_CriticalTime;
    	HANDLE ThreadHandle[2];
    
    	ThreadHandle[0] = CreateThread(NULL, 0, ThreadProcIdle, NULL, CREATE_SUSPENDED, &ThreadPriority_Idle);
    	SetThreadPriority(ThreadHandle[0], THREAD_PRIORITY_IDLE);
    	ResumeThread(ThreadHandle[0]);
    	
    	ThreadHandle[1] = CreateThread(NULL, 0, ThreadProcNormal, NULL, CREATE_SUSPENDED, &ThreadIdPriority_CriticalTime);
    	//更多的CPU占有时间
    	SetThreadPriority(ThreadHandle[1], THREAD_PRIORITY_TIME_CRITICAL);
    	ResumeThread(ThreadHandle[1]);
    
    
    	WaitForMultipleObjects(2, ThreadHandle, TRUE, INFINITE);
    	CloseHandle(ThreadHandle[0]);
    	CloseHandle(ThreadHandle[1]);
    	return 0;
    }
    
    DWORD WINAPI ThreadProcIdle(LPVOID lpParameter)
    {
    	for (int i = 0; i<200; i++)
    	{
    		printf("I'm THREAD_PRIORITY_IDLE...
    ");
    	}
    	return 0;
    }
    
    DWORD WINAPI ThreadProcNormal(LPVOID lpParameter)
    {
    	for (int i = 0; i<200; i++)
    	{
    		printf("I'm THREAD_MODE_BACKGROUND_END ...
    ");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Cookie和Session
    HTTP简介
    Python赋值与深浅拷贝
    Python装饰器探险
    Python操作MongoDB数据库
    MongoDB数据库基础
    python操作mysql数据库
    USACO2012 overplanting /// 矩阵切割 递归 oj21547
    USACO2007 The Bale Tower /// DFS oj21160
    USACO2012 Moo /// 模拟 oj21548
  • 原文地址:https://www.cnblogs.com/lsh123/p/7375367.html
Copyright © 2020-2023  润新知