c++中关于Thread Affinity(线程亲和性)示例源码

win10下,可以在任务管理器里面设置某个进程的线程亲和性,如下图:

然后选择相关的cpu,如下图:

这么做可以使得相关的线程在某些密集型计算任务中只会运行在某些指定的cpu上,以便提高性能。

以下是windwos上c++程序中应用Thread Affinity的示例:

#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <bitset>
#include <array>
#include <random>#include <intrin.h>
#include <windows.h>namespace threadAffinity
{
std::string getProcessorVendorSerialnumber()
{std::array<int, 4> cpuInfo;__cpuid(cpuInfo.data(), 1);std::ostringstream infoBuf;infoBuf<< std::uppercase << std::hex << std::setfill('0')<< std::setw(8) << cpuInfo.at(3)<< std::setw(8) << cpuInfo.at(0);return infoBuf.str();
}
std::string getThisThreadIdWithString()
{std::stringstream ss;ss << std::this_thread::get_id();return ss.str();
}
void proc(void)
{std::random_device rd{};std::mt19937       gen(rd());std::uniform_int_distribution<unsigned int> distribute(500, 1000);using namespace std::chrono_literals;std::string          info = "thread id=" + getThisThreadIdWithString() + ", apply cpu ids: ";for (auto i = 0; i < 6; ++i){//std::this_thread::sleep_for(1s);std::this_thread::sleep_for(std::chrono::milliseconds(distribute(gen)));//std::string info = "thread id=" + getThisThreadIdWithString() + ",cpuid=" + std::to_string(GetCurrentProcessorNumber()) + ", call.\n";//std::cout << info;info += std::to_string(GetCurrentProcessorNumber()) + ",";}info += "\n";std::cout << info;
}
void applyThreadAffinity(std::thread& thr, DWORD_PTR inputMask)
{std::cout << "SetThreadAffinityMask success  inputMask: " << std::bitset<32>(inputMask) << "\n";// thanks: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadaffinitymask// If the function succeeds, the return value is the thread's previous affinity mask.// If the function fails, the return value is zero. DWORD_PTR returnMask = SetThreadAffinityMask(thr.native_handle(), inputMask);if (returnMask == 0){DWORD dwErr = GetLastError();std::cerr << "SetThreadAffinityMask failed, GLE=" << dwErr << '\n';}else{std::cout << "SetThreadAffinityMask success returnMask: " << std::bitset<32>(returnMask) << "\n";}
}
void testMain()
{std::cout << "threadAffinity::testMain() begin ...\n";std::vector<std::thread> threads;unsigned int i = 0;threads.emplace_back(proc);applyThreadAffinity(threads.back(), 0x1);// 0b000001i++;threads.emplace_back(proc);applyThreadAffinity(threads.back(), 0x2);// 0b000010i++;threads.emplace_back(proc);applyThreadAffinity(threads.back(), 0x4);// 0b000100i++;threads.emplace_back(proc);applyThreadAffinity(threads.back(), 0x3);// 0b000011i++;for (; i < std::thread::hardware_concurrency(); ++i){threads.emplace_back(proc);applyThreadAffinity(threads.back(), DWORD_PTR(1) << i);}for (auto& t : threads)t.join();//std::cout << "threadAffinity::testMain() end ...\n";
}
}

可能的输出如下所示:

SetThreadAffinityMask success returnMask: 00000000000011111111111111111111
SetThreadAffinityMask success  inputMask: 00000000000000100000000000000000
SetThreadAffinityMask success returnMask: 00000000000011111111111111111111
SetThreadAffinityMask success  inputMask: 00000000000001000000000000000000
SetThreadAffinityMask success returnMask: 00000000000011111111111111111111
SetThreadAffinityMask success  inputMask: 00000000000010000000000000000000
SetThreadAffinityMask success returnMask: 00000000000011111111111111111111
thread id=30096, apply cpu ids: 18,18,18,18,18,18,
thread id=15588, apply cpu ids: 8,8,8,8,8,8,
thread id=25948, apply cpu ids: 7,7,7,7,7,7,
thread id=35928, apply cpu ids: 6,6,6,6,6,6,
thread id=15184, apply cpu ids: 9,9,9,9,9,9,
thread id=6396, apply cpu ids: 0,0,0,0,0,0,
thread id=22032, apply cpu ids: 15,15,15,15,15,15,
thread id=18116, apply cpu ids: 17,17,17,17,17,17,
thread id=23424, apply cpu ids: 11,11,11,11,11,11,
thread id=9556, apply cpu ids: 1,1,1,1,1,1,
thread id=21996, apply cpu ids: 12,12,12,12,12,12,
thread id=30992, apply cpu ids: 14,14,14,14,14,14,
thread id=22372, apply cpu ids: 5,5,5,5,5,5,
thread id=13832, apply cpu ids: 2,2,2,2,2,2,
thread id=31816, apply cpu ids: 16,16,16,16,16,16,
thread id=13332, apply cpu ids: 19,19,19,19,19,19,
thread id=35936, apply cpu ids: 13,13,13,13,13,13,
thread id=27400, apply cpu ids: 0,1,1,0,1,1,
thread id=35172, apply cpu ids: 10,10,10,10,10,10,
thread id=36296, apply cpu ids: 4,4,4,4,4,4,

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/140423.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Python 运行代码

一、Python运行代码 可以使用三种方式运行Python&#xff0c;如下&#xff1a; 1、交互式 通过命令行窗口进入 Python 并开始在交互式解释器中开始编写 Python 代码 2、命令行脚本 可以把代码放到文件中&#xff0c;通过python 文件名.py命令执行代码&#xff0c;如下&#xff…

使用 LangChain 和 Elasticsearch 对私人数据进行人工智能搜索

关于本博文的所有代码可以在地址下载&#xff1a;GitHub - liu-xiao-guo/python-vector-private 我将在本博文中其中深入研究人工智能和向量嵌入的深水区。 ChatGPT 令人大开眼界&#xff0c;但有一个主要问题。 这是一个封闭的托管系统。 在一个被大型网络公司改变的世界里生…

【轨道机器人】成功驱动伺服电机(学生电源、DCH调试软件、DH系列伺服驱动器)

1、硬件平台 工控机 学生电源 DH系列伺服驱动器 电机 调试平台&#xff1a;DCH 2、如何利用dch驱动电机 点击可驱动电机 下面的步骤是比较关键的几步&#xff1a; 3、遇到的问题 不能成功驱动电机&#xff0c;还和厂家那边打电话&#xff0c;询问 发现是这…

【C++】bitset位图的简单模拟实现及常见面试题

文章目录 前言一、 bitset模拟实现二、 常见面试题1.给你一百亿个整数&#xff0c;找到只出现一次的数字2. 给两个文件&#xff0c;分别有100亿个整数&#xff0c;我们只有1G内存&#xff0c;如何找到两个文件交集&#xff1f; 前言 快速查找某个数据是否在一个集合中排序 去重…

Hdoop伪分布式集群搭建

文章目录 Hadoop安装部署前言1.环境2.步骤3.效果图 具体步骤&#xff08;一&#xff09;前期准备&#xff08;1&#xff09;ping外网&#xff08;2&#xff09;配置主机名&#xff08;3&#xff09;配置时钟同步&#xff08;4&#xff09;关闭防火墙 &#xff08;二&#xff09…

ddns有什么作用?无公网IP怎么将内网IP端口映射外网访问

DDNS是什么&#xff1f; DDNS英文全称Dynamic Domain Name Server&#xff0c;中文含义是指动态域名服务。很多普通路由器或者智能路由器设置中&#xff0c;都可以找到DDNS&#xff08;动态DNS&#xff09;功能。 上面的解释可能过于专业&#xff0c;其实DDNS通俗点说&#xf…

小程序社区团购demo

概述 实现了用户登录或者手机号&#xff0c;加入团长&#xff0c;邀请团长&#xff0c;各种佣金明细等页面 详细 需求&#xff1a; 根据市场信息反馈&#xff0c;社区团购比较火&#xff0c;有流量的用户可以推广页面 实现了功能&#xff1a; 实现了用户微信登录自动获取…

BottomNavigationView3个以上图标不显示文字

问题 当BottomNavigationView设置的菜单中超过三个图标时&#xff0c;出现只有焦点聚集到图标时才会显示底部设置的文字描述&#xff0c;当没有焦点聚集则只显示图标&#xff0c;效果如下&#xff1a; 解决办法 设置labelVisibilityMode值 如果BottomNavigationItemView类并…

Clock时钟电路PCB设计布局布线要求

时钟电路就是类似像时钟一样准确运动的震荡电路&#xff0c;任何工作都是依照时间顺序&#xff0c;那么产生这个时间的电路就是时钟电路&#xff0c;时钟电路一般是由晶体振荡器、晶振、控制芯片以及匹配电容组成&#xff0c;如图1所示。 图1 时钟电路 针对时钟电路PCB设计有以…

k8s pod概念、分类及策略

目录 一.pod相关概念 &#xff12;.Kubrenetes集群中Pod两种使用方式 &#xff13;.pause容器的Pod中的所有容器共享的资源 &#xff14;.kubernetes中的pause容器主要为每个容器提供功能&#xff1a; &#xff16;.Pod分为两类&#xff1a; 二.Pod容器的分类 1.基础容器…

【C++心愿便利店】No.6---C++之拷贝构造函数

文章目录 一、拷贝构造函数的引入二、拷贝构造函数 &#x1f467;个人主页&#xff1a;小沈YO. &#x1f61a;小编介绍&#xff1a;欢迎来到我的乱七八糟小星球&#x1f31d; &#x1f4cb;专栏&#xff1a;C 心愿便利店 &#x1f511;本章内容&#xff1a;拷贝构造函数 记得 评…

PythonWeb服务器(HTTP协议)

一、HTTP协议与实现原理 HTTP&#xff08;Hypertext Transfer Protocol&#xff0c;超文本传输协议&#xff09;是一种用于在网络上传输超文本数据的协议。它是Web应用程序通信的基础&#xff0c;通过客户端和服务器之间的请求和响应来传输数据。在HTTP协议中连接客户与服务器的…

【Tomcat】Tomcat 运行原理

Tomcat 运行原理 一. Servlet 运行原理1. 接收请求2. 根据请求计算响应3. 返回响应 二. Tomcat 的伪代码1. Tomcat 初始化流程2. Tomcat 处理请求流程3. Servlet 的 service 方法的实现 一. Servlet 运行原理 在 Servlet 的代码中我们并没有写 main 方法, 那么对应的 doGet 代…

Oracle for Windows安装和配置——Oracle for Windows数据库创建及测试

2.2. Oracle for Windows数据库创建及测试 2.2.1. 创建数据库 1&#xff09;启动数据库创建助手&#xff08;DBCA&#xff09; 进入%ORACLE_HOME%\bin\目录并找到“dbca”批处理程序&#xff0c;双击该程序。具体如图2.1.3-1所示。 图2.1.3-1 双击“%ORACLE_HOME%\bin\dbca”…

Python之网络编程

一、网络编程 互联网时代,现在基本上所有的程序都是网络程序,很少有单机版的程序了。 网络编程就是如何在程序中实现两台计算机的通信。 Python语言中,提供了大量的内置模块和第三方模块用于支持各种网络访问,而且Python语言在网络通信方面的优点特别突出,远远领先其他语…

Spring学习笔记7 Bean的生命周期

Spring其实就是一个管理Bean对象的工厂.它负责对象的创建,对象的销毁. 这样我们才可以知道在哪个时间节点上调用了哪个类的哪个方法,知道代码该写在哪里 Bean的生命周期之粗略5步 Bean生命周期的管理可以参考Spring的源码: AbstractAutowireCapableBeanFactory Bean的生命周期…

ardupilot的编译过程

环境 树莓派4b ubuntu20.04 git 2.25.1 python3.8.10 pixhawk2.4.8 下载源码 &#xff08;已经配置好git环境和ssh&#xff09; git clone --recurse-submodules gitgithub.com:ArduPilot/ardupilot.gitcd ardupilotgit status使用git status检查是否下载完整 如果不完整&a…

自主研究,开发并产业化的一套UWB精确定位系统源码 UWB源码

UWB (ULTRA WIDE BAND) 技术是一种无线载波通讯技术&#xff0c;它不采用正弦载波&#xff0c;而是利用纳秒级的非正弦波窄脉冲传输数据&#xff0c;因此其所占的频谱范围很宽。UWB定位系统研发团队依托在移动通信&#xff0c;雷达&#xff0c;微波电路&#xff0c;云计算与大数…

黑马JVM总结(二十)

&#xff08;1&#xff09;GC_调优老年代 CMS是低响应时间的&#xff0c;并发的一个垃圾回收器 &#xff0c;有这样一个缺点&#xff0c;因为在垃圾回收的同时&#xff0c;其他的用户线程也在运行&#xff0c;就会产生新的垃圾这个新的垃圾称为浮动垃圾&#xff0c;如果浮动垃…

基于Xml方式Bean的配置-命名空间种类

Spring的标签 Spring的xml标签大体上分为两类&#xff0c;一种是默认标签&#xff0c;一种是自定义标签 默认标签&#xff1a;就是不用额外导入其它命名空间约束的标签&#xff0c;例如<bean>标签 标签作用 <beans> 一般作为xml配置根标签&#xff0c;其他标签都是…