本文執行個體講述了C++線程優先順序SetThreadPriority的使用方法,分享給大家供大家參考。具體方法如下:
複製代碼 代碼如下:
// ThreadPriority.cpp : 定義控制台應用程式的進入點。
//
#include "stdafx.h"
#include <Windows.h>
DWORD WINAPI ThreadProcIdle(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread IDLE...\n");
}
return 0;
}
DWORD WINAPI ThreadProcNormal(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread Normal...\n");
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadIdIdle;
DWORD dwThreadIdNormal;
HANDLE hThread[2];
//開啟兩個線程
hThread[0] = ::CreateThread(NULL,0, ThreadProcIdle, NULL, CREATE_SUSPENDED, &dwThreadIdIdle);
::SetThreadPriority(hThread[0],THREAD_PRIORITY_IDLE);
::ResumeThread(hThread[0]);
hThread[1] = ::CreateThread(NULL,0, ThreadProcNormal, NULL, CREATE_SUSPENDED, &dwThreadIdNormal);
::SetThreadPriority(hThread[1],THREAD_PRIORITY_NORMAL);
::ResumeThread(hThread[1]);
//等待兩個線程結束
::WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
::CloseHandle(hThread[0]);
::CloseHandle(hThread[1]);
return 0;
}
希望本文所述對大家的C++程式設計有所協助。