win32 線程優先級 SetThreadPriority


 

參數說明

  hThread 要設置的線程句柄

  nPriority 優先級別參數 可設置爲一下參數

  THREAD_PRIORITY_ABOVE_NORMAL  比一般優先級高一個等級

  THREAD_PRIORITY_BELOW_NORMAL 比一般低一個等級

  THREAD_PRIORITY_HIGHEST                 比一般高2個等級(最高)

  THREAD_PRIORITY_IDLE                          空閒

  THREAD_PRIORITY_LOWEST                  比一般低2個等級(最低)

  THREAD_PRIORITY_NORMAL                  一般等級

  THREAD_PRIORITY_TIME_CRITICAL     實時

 

#include <Windows.h>  
#include <iostream>  
using namespace std; 
 
DWORD WINAPI ThreadNormal(LPVOID lpParam) 

    int i=0; 
    while(i++ < 10) 
        cout<<"Normal Thread is running"<<endl; 
    return 0; 

 
DWORD WINAPI ThreadAboveNormal(LPVOID lpParam) 

    int i=0; 
    while(i++ < 10) 
        cout<<"Above Normal Thread is running"<<endl; 
    return 0; 

 
int main() 

    DWORD dwThreadID; 
    HANDLE hThread[2]; 
    //Create a above Normal Thread  
    hThread[0] = CreateThread(NULL,0,ThreadAboveNormal,NULL,CREATE_SUSPENDED,&dwThreadID); 
    //set priority is above normal  
    SetThreadPriority(hThread[0],THREAD_PRIORITY_ABOVE_NORMAL); 
    //resume thread  
    ResumeThread(hThread[0]); 
    //Create a Normal Thread  
    hThread[1] = CreateThread(NULL,0,ThreadNormal,NULL,0,&dwThreadID); 
    //wait two  
    WaitForMultipleObjects(2,hThread,TRUE,INFINITE); 
    //close thread   
    CloseHandle(hThread[0]); 
    CloseHandle(hThread[1]); 
    return 0; 

發佈了35 篇原創文章 · 獲贊 103 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章