#include <stdio.h>
#include <Windows.h>
DWORD WINAPI ThreadIdle(LPVOID lpParam)
{
int i = 0;
while (i++<10)
{
printf_s("Idle Thread is running \n");
Sleep(100);
}
return 0;
}
DWORD WINAPI ThreadNormal(LPVOID lpParam)
{
int i = 0;
while (i++< 10)
{
printf_s("Normal is running \n");
Sleep(100);
}
return 0;
}
int main(void)
{
DWORD dwThreadID;
HANDLE h[2];
h[0] = ::CreateThread(NULL, 0, ThreadIdle, NULL,
CREATE_SUSPENDED, &dwThreadID);
if (h[0])
{
//创建一个优先级为Idle的线程
::SetThreadPriority(h[0], THREAD_PRIORITY_IDLE/**/);
::ResumeThread(h[0]);//唤醒一个进程
}
//创建一个优先级为Normal 的线程
h[1] = ::CreateThread(NULL, 0, ThreadNormal, NULL, 0, &dwThreadID);
::WaitForMultipleObjects( //等待两个线程内核对象都变成授信状态
2,//DWORD nCount 要等待的内核对象的数量
h,//const handle *lphandles 句柄数组
true,//bool bwaitall 指定是否等待所有内核对象编程授信状态
INFINITE);//DWORD dwMilliseconds 要等待的时间
if (h[0]&&h[1])
{
::CloseHandle(h[0]);
::CloseHandle(h[1]);
}
return 0;
}
简易的多线程优先级
最新推荐文章于 2022-03-30 18:24:43 发布