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.mzph.cn/news/79828.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解决js加减乘除精度丢失问题

公共类, 将科学计数法的数字转为字符串(以下加减乘除依赖该方法) var toNonExponential (num)> {if(num null) {return num;}if(typeof num "number") {var m num.toExponential().match(/\d(?:\.(\d*))?e([-]\d)/);return num.toFixed(Math.max(0, (m[1] …

腾讯mini项目-【指标监控服务重构】2023-08-20

今日已办 PPT制作 答辩流程 概述&#xff1a;对项目背景、架构进行介绍&#xff08;体现我们分组的区别和需求&#xff09;人员&#xff1a;小组成员进行简短的自我介绍和在项目中的定位&#xff0c;分工进展&#xff1a;对项目进展介绍&#xff0c;其中a、b两组的区别和工作…

STM32——SPI通信

文章目录 SPI&#xff08;Serial Peripheral Interface&#xff09;概述&#xff1a;SPI的硬件连接&#xff1a;SPI的特点和优势&#xff1a;SPI的常见应用&#xff1a;SPI的工作方式和时序图分析&#xff1a;工作模式传输模式与时序分析工作流程 SPI设备的寄存器结构和寄存器设…

Linux四种I/O模型

一.四种模型 阻塞式IO&#xff0c;非阻塞式IO&#xff0c;信号驱动IO&#xff0c;IO多路复用 二.阻塞式IO 特点&#xff1a;最简单&#xff0c;最常用&#xff0c;效率低 阻塞I/O 模式是最普遍使用的I/O 模式 系统默认状态&#xff0c;套接字建立后所处于的模式就是阻塞I/O 模式…

ActiveRecord::Migration.maintain_test_schema!

测试gem&#xff1a; rspec-rails 问题描述 在使用 rspec-rails 进行测试时&#xff0c;出现了以下错误 ActiveRecord::StatementInvalid: UndefinedFunction: ERROR: function init_id() does not exist这个错误与数据库架构有关。 schema.rb中 create_table "users…

国家网络安全周2023时间是什么时候?有什么特点?谁举办的?

国家网络安全周2023时间是什么时候&#xff1f; 2023年国家网络安全宣传周将于9月11日至17日在全国范围内统一开展。其中开幕式等重要活动将在福建省福州市举行。今年网安周期间&#xff0c;除开幕式外&#xff0c;还将举行网络安全博览会、网络安全技术高峰论坛、网络安全微视…

【Git】万字git与gitHub

&#x1f384;欢迎来到边境矢梦的csdn博文&#x1f384; &#x1f384;本文主要梳理在git和GitHub时的笔记与感言 &#x1f384; &#x1f308;我是边境矢梦&#xff0c;一个正在为秋招和算法竞赛做准备的学生&#x1f308; &#x1f386;喜欢的朋友可以关注一下&#x1faf0;&…

【FAQ】安防监控/视频汇聚/云存储/智能视频分析平台EasyCVR显示CPU过载,如何解决?

视频云存储/安防监控/视频汇聚平台EasyCVR基于云边端智能协同&#xff0c;支持海量视频的轻量化接入与汇聚、转码与处理、全网智能分发、视频集中存储等。安防视频监控系统EasyCVR拓展性强&#xff0c;视频能力丰富&#xff0c;具体可实现视频监控直播、视频轮播、视频录像、云…

Linux网络和安全:配置、远程访问与防御指南

文章目录 Linux 网络和安全引言网络配置IP地址配置配置网络接口防火墙设置安全性加强 Linux网络配置及端口管理网络配置命令端口管理 防火墙和安全性设置防火墙管理工具安全性设置 Linux远程访问技术&#xff1a;SSH和VPNSSHVPN Linux软件和服务网络工具文件传输VPN技术安全审计…

线性代数的本质(四)——行列式

文章目录 行列式二阶行列式 n n n 阶行列式行列式的性质克拉默法则行列式的几何理解 行列式 二阶行列式 行列式引自对线性方程组的求解。考虑两个方程的二元线性方程组 { a 11 x 1 a 12 x 2 b 1 a 21 x 1 a 22 x 2 b 2 \begin{cases} a_{11}x_1a_{12}x_2b_1 \\ a_{21}x_…

SpringMvc向request域中设置数据

目录 &#xff08;1&#xff09;使用原生的HttpServletRequest &#xff08;2&#xff09;使用Model&#xff0c;ModelMap &#xff08;3&#xff09;使用Map集合 SpringMyvc有三种方式可以向request域中设置数据 &#xff08;1&#xff09;使用原生的HttpServletRequest Re…

ActiveMQ安装

1.多种语言和协议编写客户端 语言: Java, C, C, C#, Ruby, Perl, Python, PHP 应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP 2.完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务) 3.对Spring的支持,ActiveMQ可以很容得内嵌到使用Spring的系统里面去…

无涯教程-JavaScript - COLUMNS函数

描述 COLUMNS函数返回数组或引用中的列数。 语法 COLUMNS (array)争论 Argument描述Required/OptionalarrayAn array or array formula, or a reference to a range of cells for which you want the number of Columns.Required Notes COLUMNS(1:1)返回Excel中的列数,即…

【python手写算法】numpy实现简易神经网络和反向传播算法【1】

import numpy as npdef dense(A,W):Znp.matmul(A,W)#矩阵乘法return 1/(1np.exp(-Z))if __name__ __main__:leanring_rate100Anp.array([[200.0,17.0]])# Wnp.array([[1,-3,5],# [-2,4,-6]])# bnp.array([[-1,1,2]])W1 np.array([[0., -10, 4],[-1,3,2]])W2np.ar…

STM32单片机——串口通信(轮询+中断)

STM32单片机——串口通信&#xff08;轮询中断&#xff09; 串口通信相关概念HAL库解析及CubeMX工程配置与程序设计常用函数介绍CubeMX工程配置HAL库程序设计&#xff08;轮询中断&#xff09;轮询数据收发中断收发数据 固件库程序设计及实现固件库配置流程结构体配置及初始化程…

React复习日志大纲

文章目录 React基础篇创建项目启动项目项目目录说明调整项目src剩余目录01基本使用02 列表渲染03 条件渲染04 样式处理05 函数和类组件创建和渲染06 事件绑定07 事件对象e08 传递额外参数09 组件状态修改10 受控组件11 非受控组件12 组件通信父传子13 Props说明14 组件通信子传…

Golang代码漏洞扫描工具介绍——govulncheck

Golang Golang作为一款近年来最火热的服务端语言之一&#xff0c;深受广大程序员的喜爱&#xff0c;笔者最近也在用&#xff0c;特别是高并发的场景下&#xff0c;golang易用性的优势十分明显&#xff0c;但笔者这次想要介绍的并不是golang本身&#xff0c;而且golang代码的漏洞…

Linux网络编程

一.协议 1.1什么是协议 从应用的角度出发&#xff0c;协议可理解为“规则”&#xff0c;是数据传输和数据的解释的规则。 假设&#xff0c;A、B双方欲传输文件。规定: 第一次&#xff0c;传输文件名&#xff0c;接收方接收到文件名&#xff0c;应答OK给传输方; 第二次&#xff…

【每日一题】34. 在排序数组中查找元素的第一个和最后一个位置

34. 在排序数组中查找元素的第一个和最后一个位置 - 力扣&#xff08;LeetCode&#xff09; 给你一个按照非递减顺序排列的整数数组 nums&#xff0c;和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。 如果数组中不存在目标值 target&#xff0c;返回 […

虚拟机下载与Ubuntu安装

下载VMware 进入官网资源 -> 产品下载 -> Desktop & End-User Computing选择 VMware Workstation Pro 这一栏&#xff0c;点击右边的下载产品&#xff0c;跳到新页面选择版本&#xff08;我选的是 16.0&#xff09;&#xff0c;然后点击下面对应系统的转至下载&…