The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.

  1. DWORD_PTR SetThreadAffinityMask(
  2. HANDLE hThread,
  3. DWORD_PTR dwThreadAffinityMask
  4. );

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,就能为各个线程设置亲缘性屏蔽:

  1. DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);

该函数中的h T h r e a d参数用于指明要限制哪个线程, dwThreadAffinityMask用于指明该线程能够在哪个CPU上运行。dwThreadAffinityMask必须是进程的亲缘性屏蔽的相应子集。返回值是线程的前一个亲缘性屏蔽。

因此,若要将3个线程限制到CPU1、2和3上去运行,可以这样操作:

  1. //Thread 0 can only run on CPU 0.
  2. SetThreadAffinityMask(hThread0, 0x00000001); //第0位是1
  3. //Threads 1, 2, 3 run on CPUs 1, 2, 3.//第1 2 3位是1
  4. SetThreadAffinityMask(hThread1, 0x0000000E);
  5. SetThreadAffinityMask(hThread2, 0x0000000E);
  6. SetThreadAffinityMask(hThread3, 0x0000000E);

http://blog.csdn.net/yanjiaye520/article/details/7638219

SetThreadAffinityMask设置线程亲缘性的更多相关文章

  1. Windows核心编程 第七章 线程的调度、优先级和亲缘性(下)

    7.6 运用结构环境 现在应该懂得环境结构在线程调度中所起的重要作用了.环境结构使得系统能够记住线程的状态,这样,当下次线程拥有可以运行的C P U时,它就能够找到它上次中断运行的地方. 知道这样低层 ...

  2. Linux CPU亲缘性详解

    前言 在淘宝开源自己基于nginx打造的tegine服务器的时候,有这么一项特性引起了笔者的兴趣.“自动根据CPU数目设置进程个数和绑定CPU亲缘性”.当时笔者对CPU亲缘性没有任何概念,当时作者只是 ...

  3. 【记录一个问题】android ndk下设置线程的亲缘性,总有两个核无法设置成功

    参考了这篇文章:https://blog.csdn.net/lanyzh0909/article/details/50404664 大体的代码如下: #include <pthread.h> ...

  4. CPU affinity 进程和线程的亲缘性

    设置Processor Affinity 作用: 1.进程和线程的亲缘性(affinity),使进程或线程在指定的CPU(核)上运行.(比如程序A,在第4个核心上运行) 2.设置进程 或者 线程, 使 ...

  5. Windows内核之线程的调度,优先级,亲缘性

    1 调度 Windows不是实时操作系统,它是抢占式多线程操作系统.在如果全部优先级同样的情况下,CPU对线程的调度原则是每隔20m就会切换到下一个线程,依据Context中的IP和SP来接着运行上次 ...

  6. Windows核心编程 第七章 线程的调度、优先级和亲缘性(上)

    第7章 线程的调度.优先级和亲缘性 抢占式操作系统必须使用某种算法来确定哪些线程应该在何时调度和运行多长时间.本章将要介绍Microsoft Windows 98和Windows 2000使用的一些算 ...

  7. KVM虚拟机cpu资源限制和vcpu亲缘性绑定

    前言 KVM中添加的实例存在资源分布不均的情况,这样如果有消耗资源的实例会影响到其他实例的服务正常运行,所以给kvm做资源限制是很有必要的,下面记录一下在centos7中KVM环境下使用cgroup限 ...

  8. Kubernetes使用节点亲缘性将POD调度到特定节点上

    节点污点可以用来让pod远离特定的节点,尽量在不修改已有pod信息的前提,通过在节点添加污点信息,来拒绝pod在某些节点上的部署. 而现在介绍一种叫做节点亲缘性,通过明确的在pod中添加的信息,来决定 ...

  9. Nginx设置线程数为整机内核数的俩倍!

    Nginx设置线程数为整机内核数的俩倍!

随机推荐

  1. java.io.IOException: open failed: ENOENT (No such file or directory)open failed: EISDIR (Is a directory)

    这一类的错误,原因一般有两点: 1.没有给相应读写文件权限 2.给了权限,但是文件的路径写的不对,比如少了“/”,这样就会报错了. Caused by: android.system.ErrnoExc ...

  2. Python之路第九天,高级(1)-网络编程

    SOCKET编程 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...

  3. 在右键添加Cmder here选项,添加启动Cmder的快捷键

    右键菜单添加“Cmder here” 打开cmder,在其中输入: cmder /register user 或 cmder /register all 即可   设置启动cmder的快捷键 右键 C ...

  4. ZJUTACM(hd1259)

    ZJUTACM 点我 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. you can Solve a Geometry Problem too(hdoj1086)

    Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...

  6. Linux进程间通信——使用信号

    一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...

  7. HDOJ-1007 Quoit Design(最近点对问题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具 实际就是要求最近的两个坐标的距离 ...

  8. poj1458 dp入门

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37551   Accepted: 15 ...

  9. 电脑中已有VS2005和VS2010安装.NET3.5失败的解决方案

    1.重启 MSI 安装服务: 运行-输入“CMD”命令,在弹出的对话框中输入命令: msiexec/unregserver ,回车,并再次输入 msiexec/regserver . 2.启用 Pri ...

  10. hdu 2254 奥运(邻接矩阵应用)

    Problem Description 北京迎来了第一个奥运会,我们的欢呼声响彻中国大地,所以今年的奥运金牌 day day up! 比尔盖兹坐上鸟巢里,手里摇着小纸扇,看的不亦乐乎,被俺们健儿的顽强 ...