刘收获

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

线程优先级

通过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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 线程优先级.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...\n");
    }
    return 0;
}
 
DWORD WINAPI ThreadProcNormal(LPVOID lpParameter)
{
    for (int i = 0; i<200; i++)
    {
        printf("I'm THREAD_MODE_BACKGROUND_END ...\n");
    }
    return 0;
}

  

posted on   沉疴  阅读(299)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示