当前位置:编程学习 > C/C++ >>

win32 线程优先级

SetThreadPriority  设置指定线程的优先级
[cpp]  
BOOL SetThreadPriority(  HANDLE hThread, // handle to the thread 
  int nPriority   // thread priority level); 
参数说明
  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     实时
[cpp]
#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  www.zzzyk.com
    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; 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,