网站首页  词典首页

请输入您要查询的函数:

 

术语 setthreadpriority
释义 SetThreadPriority
语法:
C++
BOOL WINAPI SetThreadPriority(
__in HANDLE hThread,
__in int nPriority
);
SetThreadPriority函数
设置指定线程的优先级值。此值,连同线程的进程的优先级,决定了线程的基本优先级的Level。
参数
hThread [in]
一个句柄,线程的优先级值是设置。
句柄必须有THREAD_SET_INFORMATION或THREAD_SET_LIMITED_INFORMATION访问权限。有关更多信息,请参见线程安全和访问权限。
Windows Server 2003和Windows XP/2000操作系统:在处理必须有THREAD_SET_INFORMATION访问权限。
nPriority [in]
线程的优先级值。此参数可以是下列值之一。
PriorityMeaning
THREAD_MODE_BACKGROUND_BEGIN
0x00010000Begin后台处理模式。该系统降低了资源调度线程的优先次序,以便它可以在没有任何显着影响在前台活动的背景工作。
此值可以指定只有hThread是一个句柄到当前线程。函数失败如果线程已经在后台处理模式。
Windows Server 2003和Windows XP/2000操作系统:此值不支持。
THREAD_MODE_BACKGROUND_END
0x00020000End后台处理模式。该系统恢复线程的资源调度优先,因为他们在线程进入后台处理模式的。
此值可以指定只有hThread是一个句柄到当前线程。函数失败如果线程没有在后台处理模式。
Windows Server 2003和Windows XP/2000操作系统:此值不支持。
THREAD_PRIORITY_ABOVE_NORMAL
1Priority 1分以上的优先级。
THREAD_PRIORITY_BELOW_NORMAL
- 1Priority 1点下面的优先级。
THREAD_PRIORITY_HIGHEST
2Priority 2个百分点以上的优先级。
THREAD_PRIORITY_IDLE
- 15Base优先1 IDLE_PRIORITY_CLASS,BELOW_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS,ABOVE_NORMAL_PRIORITY_CLASS,或HIGH_PRIORITY_CLASS流程,为REALTIME_PRIORITY_CLASS 16个基本优先级进程。
THREAD_PRIORITY_LOWEST
- 2Priority 2点下面的优先级。
THREAD_PRIORITY_NORMAL
0Normal优先的优先级。
THREAD_PRIORITY_TIME_CRITICAL
15Base优先15 IDLE_PRIORITY_CLASS,BELOW_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS,ABOVE_NORMAL_PRIORITY_CLASS,或HIGH_PRIORITY_CLASS流程,为REALTIME_PRIORITY_CLASS了31基本优先级进程。
如果线程有REALTIME_PRIORITY_CLASS基类,这个参数也可以-7,-6,-5,-4,-3,3,4,5或6。有关更多信息,请参阅调度优先级。
返回值
如果函数成功,返回值为非零。
如果函数失败,返回值是零。为了获得更多错误信息,调用GetLastError。
备注
每个线程都有一个基本优先级的线程的优先级值和其进程的优先级确定。该系统使用的所有可执行线程的基本优先级的Level,以确定哪个线程获取未来的CPU时间片。线程定于1轮在每个优先级循环方式,只有当有更高层次上没有不安排在一个较低的Level发生的线程执行线程。
该SetThreadPriority功能,可以设置线程的优先级的基础相对于它的进程的优先级。例如,指定在给SetThreadPriority呼吁THREAD_PRIORITY_HIGHEST为1 IDLE_PRIORITY_CLASS进程中的线程设置线程的基本优先级至6级。对于一个表,显示了每个优先级和线程的优先级值组合的基本优先级的Level,见调度优先级。
对于IDLE_PRIORITY_CLASS,BELOW_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS,ABOVE_NORMAL_PRIORITY_CLASS和HIGH_PRIORITY_CLASS过程,系统动态boosts线程的基本优先级Level,当时事件发生时,是很重要的线索。 REALTIME_PRIORITY_CLASS进程不会收到动态boosts。
所有线程最初开始THREAD_PRIORITY_NORMAL。使用GetPriorityClass和SetPriorityClass函数获取和设置一个进程的优先级。使用GetThreadPriority函数来获取线程的优先级值。
使用的进程优先级来区分应用程序的关键时候,以及那些正常或偏低调度的要求。使用线程的优先级值,以区分一个过程的任务的优先次序。例如,一个线程来处理一个窗口输入可能比执行的线程在CPU密集型计算更高的优先级。
当操作的优先事项,必须非常小心,以确保高优先级的线程不会消耗可用的CPU时间的。一个有上述11个基本优先级线程干扰操作系统的正常运作。使用REALTIME_PRIORITY_CLASS可能会导致磁盘缓存不冲洗,挂鼠标等。
在THREAD_PRIORITY_ *值影响线程的CPU调度优先级。对于线程执行诸如背景工作文件I / O,网络的I / O,或数据处理,它是不够的调整CPU调度优先级,即使是空闲的CPU优先级的线程可以很容易地干扰与系统响应时,使用磁盘和内存。线程执行后台工作应该使用THREAD_MODE_BACKGROUND_BEGIN和THREAD_MODE_BACKGROUND_END值,以调整其资源调度的优先事项;线程与用户交互不应使用THREAD_MODE_BACKGROUND_BEGIN。
当一个线程在后台处理模式时,它应尽量共享,例如关键部分,堆资源,并在此过程中的其他线程处理,否则可能会出现优先级倒置。如果有高优先级的线程,在后台处理模式线程执行可能无法及时安排,但它永远不会饿死。
Windows Server 2008和Windows Vista的:这个系统虽然开始,SetThreadPriority函数返回成功返回值,但不改变是开始从系统启动文件夹或在HKEY_LOCAL_MACHINE上市申请线程的优先级\\软件\\微软\\窗口\\ CurrentVersion \\ Run注册表项。降低优先级运行在很短的时间(大约60秒),使这一制度更加启动过程中对用户操作这些应用。
实例
下面的例子演示了使用线程的背景模式。
#include
#include
void main()
{
DWORD dwError, dwThreadPri;
if(!SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN))
{
dwError = GetLastError();
if( ERROR_THREAD_MODE_ALREADY_BACKGROUND == dwError)
printf("Already in background mode\\n");
else printf("Failed to enter background mode (%d)\\n", dwError);
goto Cleanup;
}
// Display thread priority
dwThreadPri = GetThreadPriority(GetCurrentThread());
printf("Current thread priority is 0x%x\\n", dwThreadPri);
//
// TODO: Perform background work
//
;
if(!SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END))
{
printf("Failed to end background mode (%d)\\n", GetLastError());
}
Cleanup:
// TODO: Clean up
;
}
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinbase.h(头文件:winuser.h)
LibraryKernel32.lib
DLLKernel32.dll
参见
GetPriorityClass
GetThreadPriority
进程和线程函数
调度优先级
SetPriorityClass
主题
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年8月27日
==英文原文==SetThreadPriority Function
Sets the priority value for the specified thread. This value, together with the priority class of the thread's process, determines the thread's base priority level.
Syntax
C++
BOOL WINAPI SetThreadPriority(
__in HANDLE hThread,
__in int nPriority
);
Parameters
hThread [in]
A handle to the thread whose priority value is to be set.
The handle must have the THREAD_SET_INFORMATION or THREAD_SET_LIMITED_INFORMATION access right. For more information, see Thread Security and Access Rights .
Windows Server 2003 and Windows XP/2000: The handle must have the THREAD_SET_INFORMATION access right.
nPriority [in]
The priority value for the thread. This parameter can be one of the following values.
PriorityMeaning
THREAD_MODE_BACKGROUND_BEGIN
0x00010000Begin 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 and Windows XP/2000: This value is not supported.
THREAD_MODE_BACKGROUND_END
0x00020000End 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 and Windows XP/2000: This value is not supported.
THREAD_PRIORITY_ABOVE_NORMAL
1Priority 1 point above the priority class.
THREAD_PRIORITY_BELOW_NORMAL
-1Priority 1 point below the priority class.
THREAD_PRIORITY_HIGHEST
2Priority 2 points above the priority class.
THREAD_PRIORITY_IDLE
-15Base 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
-2Priority 2 points below the priority class.
THREAD_PRIORITY_NORMAL
0Normal priority for the priority class.
THREAD_PRIORITY_TIME_CRITICAL
15Base 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.

If the thread has the REALTIME_PRIORITY_CLASS base class, this parameter can also be -7, -6, -5, -4, -3, 3, 4, 5, or 6. For more information, see Scheduling Priorities .
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError .
Remarks
Every thread has a base priority level determined by the thread's priority value and the priority class of its process. The system uses the base priority level of all executable threads to determine which thread gets the next slice of CPU time. Threads are scheduled in a round-robin fashion at each priority level, and only when there are no executable threads at a higher level does scheduling of threads at a lower level take place.
The SetThreadPriority function enables setting the base priority level of a thread relative to the priority class of its process. For example, specifying THREAD_PRIORITY_HIGHEST in a call to SetThreadPriority for a thread of an IDLE_PRIORITY_CLASS process sets the thread's base priority level to 6. For a table that shows the base priority levels for each combination of priority class and thread priority value, see Scheduling Priorities .
For IDLE_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, and HIGH_PRIORITY_CLASS processes, the system dynamically boosts a thread's base priority level when events occur that are important to the thread. REALTIME_PRIORITY_CLASS processes do not receive dynamic boosts.
All threads initially start at THREAD_PRIORITY_NORMAL. Use the GetPriorityClass and SetPriorityClass functions to get and set the priority class of a process. Use the GetThreadPriority function to get the priority value of a thread.
Use the priority class of a process to differentiate between applications that are time critical and those that have normal or below normal scheduling requirements. Use thread priority values to differentiate the relative priorities of the tasks of a process. For example, a thread that handles input for a window could have a higher priority level than a thread that performs intensive calculations for the CPU.
When manipulating priorities, be very careful to ensure that a high-priority thread does not consume all of the available CPU time. A thread with a base priority level above 11 interferes with the normal operation of the operating system. Using REALTIME_PRIORITY_CLASS may cause disk caches to not flush, hang the mouse, and so on.
The THREAD_PRIORITY_* values affect the CPU scheduling priority of the thread. For threads that perform background work such as file I/O, network I/O, or data processing, it is not sufficient to adjust the CPU scheduling priority; even an idle CPU priority thread can easily interfere with system responsiveness when it uses the disk and memory. Threads that perform background work should use the THREAD_MODE_BACKGROUND_BEGIN and THREAD_MODE_BACKGROUND_END values to adjust their resource scheduling priorities; threads that interact with the user should not use THREAD_MODE_BACKGROUND_BEGIN.
When a thread is in background processing mode, it should minimize sharing resources such as critical sections, heaps, and handles with other threads in the process, otherwise priority inversions can occur. If there are threads executing at high priority, a thread in background processing mode may not be scheduled promptly, but it will never be starved.
Windows Server 2008 and Windows Vista: While the system is starting, the SetThreadPriority function returns a success return value but does not change thread priority for applications that are started from the system Startup folder or listed in the HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run registry key. These applications run at reduced priority for a short time (approximately 60 seconds) to make the system more responsive to user actions during startup.
Examples
The following example demonstrates the use of thread background mode.
#include
#include
void main()
{
DWORD dwError, dwThreadPri;
if(!SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN))
{
dwError = GetLastError();
if( ERROR_THREAD_MODE_ALREADY_BACKGROUND == dwError)
printf("Already in background mode\\n");
else printf("Failed to enter background mode (%d)\\n", dwError);
goto Cleanup;
}
// Display thread priority
dwThreadPri = GetThreadPriority(GetCurrentThread());
printf("Current thread priority is 0x%x\\n", dwThreadPri);
//
// TODO: Perform background work
//
;
if(!SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END))
{
printf("Failed to end background mode (%d)\\n", GetLastError());
}
Cleanup:
// TODO: Clean up
;
}
Requirements
Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
See Also
GetPriorityClass
GetThreadPriority
Process and Thread Functions
Scheduling Priorities
SetPriorityClass
Threads
Send comments about this topic to Microsoft
Build date: 8/27/2009
==原始网址==http://msdn.microsoft.com/en-us/library/ms686277(VS.85).aspx\n
随便看

 

windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。

 

Copyright © 2004-2023 Winrtm.com All Rights Reserved
京ICP备2021023879号-40 更新时间:2024/10/6 11:22:24