MSDN上的描述:
SetThreadAffinityMask
The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.
DWORD_PTR SetThreadAffinityMask(
HANDLE hThread,
DWORD_PTR dwThreadAffinityMask
);
Parameters
hThread
[in] Handle to the thread whose affinity mask is to be set.
This handle must have the THREAD_SET_INFORMATION and THREAD_QUERY_INFORMATION access rights. For more information, see Thread Security and Access Rights.
dwThreadAffinityMask
[in] Affinity mask for the thread.
Windows Me/98/95: This value must be 1.
Return Values
If the function succeeds, the return value is the thread's previous affinity mask.
Windows Me/98/95: The return value is 1. To succeed, hThread must be valid and dwThreadAffinityMask must be 1.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on.
A thread affinity mask must be a proper subset of the process affinity mask for the containing process of a thread. A thread is only allowed to run on the processors its process is allowed to run on.
通过调用SetThreadAffinityMask,就能为各个线程设置亲缘性屏蔽:
DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);
该函数中的h T h r e a d参数用于指明要限制哪个线程, dwThreadAffinityMask用于指明该线程能够在哪个CPU上运行。dwThreadAffinityMask必须是进程的亲缘性屏蔽的相应子集。返回值是线程的前一个亲缘性屏蔽。
因此,若要将3个线程限制到CPU1、2和3上去运行,可以这样操作:
//Thread 0 can only run on CPU 0.
SetThreadAffinityMask(hThread0, 0x00000001); //第0位是1
//Threads 1, 2, 3 run on CPUs 1, 2, 3.//第1 2 3位是1
SetThreadAffinityMask(hThread1, 0x0000000E);
SetThreadAffinityMask(hThread2, 0x0000000E);
SetThreadAffinityMask(hThread3, 0x0000000E);
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/W511522329/archive/2010/03/06/5352597.aspx
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
void running(int seconds) {
Sleep(seconds*1000);
std::cout<<"sleep for "<<seconds<<"(s)"<<std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
SetThreadAffinityMask(GetCurrentThread(), 1);
LARGE_INTEGER start, end;
LARGE_INTEGER freq;
//timeConsuming();
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);//start
std::cout<<"start.QuadPart = "<<start.QuadPart<<std::endl; //output start
running(10); //running 10 seconds
QueryPerformanceCounter(&end); //end
std::cout<<"end.QuadPart = "<<end.QuadPart<<std::endl; //output end
std::cout<<"consume value = end.QuadPart - start.QuadPart = "<<(end.QuadPart - start.QuadPart)<<std::endl;
std::cout<<"(consume value/(double)freq.QuadPart) Time consumed = "<<(end.QuadPart - start.QuadPart)/(double)freq.QuadPart<<"(s)"<<std::endl; //output consumed time
return 0;
}
start.QuadPart = 49102789906513
sleep for10(s)
end.QuadPart = 49127801303663
consume value = end.QuadPart - start.QuadPart = 25011397150
(consume value/(double)freq.QuadPart) Time consumed = 10.0046(s)
分享到:
相关推荐
CPU亲和性是指将一个线程绑定到特定的一个或多个CPU核心上运行,这样可以避免线程在多核之间切换带来的额外开销,从而提高性能。在需要精确时间测量时,固定线程的执行核心可以减少因CPU核心间时钟频率不同而导致的...
在Linux中,可以使用`sched_setaffinity`函数来设置进程或线程的亲和性,该函数允许我们指定一个CPU掩码,决定进程或线程可以运行在哪些CPU核心上。而在Windows中,可以使用`SetThreadAffinityMask`函数来达到同样的...
但是,如果你需要更详细的CPU信息,如每个处理器的核心数,可能需要使用更复杂的API,如`EnumProcessors`和`GetLogicalProcessorInformation`。 在Linux环境下,我们通常依赖于系统调用来获取CPU信息。`/proc/...
在Windows操作系统中,线程的CPU亲和性(Thread Affinity)是指将特定线程绑定到一个或多个特定的CPU核心上,以优化系统资源的使用和提高性能。`SetThreadAffinityMask`函数是Windows API中用于设置线程亲和性的关键...
`SetThreadAffinityMask`函数则用于设置特定线程的CPU亲和性,它同样需要进程句柄和一个掩码。通过这两个函数,我们可以强制线程只在一个特定的核心上运行,从而避免因为线程在不同核心间频繁切换引发的错误。 示例...
13. **Windows线程**:Windows线程可以通过`SetThreadAffinityMask`设置CPU绑定,`_beginthreadex`和`CreateThread`创建线程的安全性与使用场景有关,线程执行函数的调用约定(`cdecl`和`stdcall`)也需匹配。...
2. 分配线程属性(可选):可以使用`GetThreadPriority`、`SetThreadPriority`等函数来设定线程的优先级,或者使用`SetThreadAffinityMask`来设置线程的处理器亲和性。 3. 创建线程:调用`CreateThread`函数,传入...
通过`TerminateProcess()`可以终止进程,`SetThreadAffinityMask()`可以设置线程的CPU亲和性,`SetThreadPriority()`可以设置线程的优先级,`CreateProcess()`可以创建新进程,`WaitForSingleObject()`可以等待单个...
通过`pthreads-w32`,开发者可以在Windows 10平台上实现与Linux类似的多线程编程,例如创建、同步、销毁线程,设置线程属性,以及使用条件变量、互斥量、读写锁等同步机制。这使得跨平台的软件开发变得更加便捷,...
易语言多线程删除垃圾源码,多线程删除垃圾,扫描文件夹,GetSize,输出列表,Del,SetProcessAffinityMask,SetProcessWorkingSetSize,SetThreadAffinityMask
在多处理器系统中,为了避免因处理器间的时钟差异导致的不准确性,可以使用`SetThreadAffinityMask`函数来确保线程始终运行在同一处理器上。 4.3.2 **游戏计时器类(GameTimer)** `GameTimer`类是一个封装了性能...
例如,可以使用`WriteFile`和`ReadFile`函数进行GPIO的读写,使用`SetThreadAffinityMask`来确保中断处理在正确的处理器上下文执行。 开发IIC-2450驱动时,还需要关注以下几个关键点: - **错误处理**:在通信过程...
无驱动进入ring0,读取cr0,操作数据,写物理内存,读物理内存,查看字节集,提升进程权限,查看字节集2,到十六进制文本,RtlAdjustPrivilege,NtSystemDebugControl,SetProcessAffinityMask,SetThreadAffinityMask