Limit the current thread to one processor
2010-01-05 16:25
267 查看
//--------------------------------------------------------------------------------------
// Limit the current thread to one processor (the current one). This ensures that timing code
// runs on only one processor, and will not suffer any ill effects from power management.
// See "Game Timing and Multicore Processors" for more details
//--------------------------------------------------------------------------------------
void LimitThreadAffinityToCurrentProc()
{
HANDLE hCurrentProcess = GetCurrentProcess();
// Get the processor affinity mask for this process
DWORD_PTR dwProcessAffinityMask = 0;
DWORD_PTR dwSystemAffinityMask = 0;
if( GetProcessAffinityMask( hCurrentProcess, &dwProcessAffinityMask, &dwSystemAffinityMask ) != 0 &&
dwProcessAffinityMask )
{
// Find the lowest processor that our process is allows to run against
DWORD_PTR dwAffinityMask = ( dwProcessAffinityMask & ( ( ~dwProcessAffinityMask ) + 1 ) );
// Set this as the processor that our thread must always run against
// This must be a subset of the process affinity mask
HANDLE hCurrentThread = GetCurrentThread();
if( INVALID_HANDLE_VALUE != hCurrentThread )
{
SetThreadAffinityMask( hCurrentThread, dwAffinityMask );
CloseHandle( hCurrentThread );
}
}
CloseHandle( hCurrentProcess );
}
// Limit the current thread to one processor (the current one). This ensures that timing code
// runs on only one processor, and will not suffer any ill effects from power management.
// See "Game Timing and Multicore Processors" for more details
//--------------------------------------------------------------------------------------
void LimitThreadAffinityToCurrentProc()
{
HANDLE hCurrentProcess = GetCurrentProcess();
// Get the processor affinity mask for this process
DWORD_PTR dwProcessAffinityMask = 0;
DWORD_PTR dwSystemAffinityMask = 0;
if( GetProcessAffinityMask( hCurrentProcess, &dwProcessAffinityMask, &dwSystemAffinityMask ) != 0 &&
dwProcessAffinityMask )
{
// Find the lowest processor that our process is allows to run against
DWORD_PTR dwAffinityMask = ( dwProcessAffinityMask & ( ( ~dwProcessAffinityMask ) + 1 ) );
// Set this as the processor that our thread must always run against
// This must be a subset of the process affinity mask
HANDLE hCurrentThread = GetCurrentThread();
if( INVALID_HANDLE_VALUE != hCurrentThread )
{
SetThreadAffinityMask( hCurrentThread, dwAffinityMask );
CloseHandle( hCurrentThread );
}
}
CloseHandle( hCurrentProcess );
}
相关文章推荐
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
- getCurrentSession 出现No hibernate bound to the thread
- the limit to the amount of thread from a process
- 【问题】The coprocessor thread stopped itself due to scan timeout or scan threshold
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.报该错误的一种原因。
- maven: Unable to add module to the current project
- Ubuntu wine / winetricks解决this msxml 6.0 is not supported on the current processor type
- Activity not started, its current task has been brought to the front
- how to get the space size of some tables in one database?
- Eclipse导入Android工程出现Project has no default.properties file! Edit the project properties to set one.的解决方法
- Operation is not valid due to the current state of the object.
- how to get the return value from a thread in python?
- Project has no default.properties file! Edit the project properties to set one.错误解决方案
- SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”
- Eclipse错误:Project has no default.properties file! Edit the project properties to set one
- Controls created on one thread cannot be parented to a control on a different thread
- ActivityManager: Warning: Activity not started, its current task has been brought to the front
- swift 第三方库迁移错误解决“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choo
- Attempting to initialize hardware acceleration outside of the main thread 处理