C\C++:原子计数操作 之__syn_fetch_and_add性能研究

背景

        首先在多线程环境中,多线程计数操作,共享状态或者统计相关时间次数等,这些需要在多线程之间共享变量和修改变量,如此就需要在多线程间对该变量进行互斥操作和访问。

        但是如果我们要维护一个全局的线程安全的 int 类型变量 count, 下面这两行代码都是很危险的:

count ++;

count += n;

        我们知道, 高级语言中的一条语句, 并不是一个原子操作. 比如一个最简单的自增操作就分为三步:

  1. 从缓存取到寄存器
  2.  在寄存器加1
  3.  存入缓存。

        多个线程访问同一块内存时, 需要加锁来保证访问操作是互斥的。

所以, 我们可以在操作 count 的时候加一个互斥锁. 如下面的代码:

pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_lock(&count_lock);

count++;

pthread_mutex_unlock(&count_lock);

        另一个办法就是, 让 count++ 和 count+=n 这样的语句变成原子操作. 一个原子操作必然是线程安全的. 有两种使用原子操作的方式:

1. 使用 gcc 的原子操作

2. 使用 c++11中STL中的 stomic 类的函数

在这里我只介绍 gcc 里的原子操作, 这些函数分成以下几组:

返回更新前的值

type __sync_fetch_and_add (type *ptr, type value, …)

type __sync_fetch_and_sub (type *ptr, type value, …)

type __sync_fetch_and_or (type *ptr, type value, …)

type __sync_fetch_and_and (type *ptr, type value, …)

type __sync_fetch_and_xor (type *ptr, type value, …)

type __sync_fetch_and_nand (type *ptr, type value, …)

返回更新后的值

type __sync_add_and_fetch (type *ptr, type value, …)

type __sync_sub_and_fetch (type *ptr, type value, …)

type __sync_or_and_fetch (type *ptr, type value, …)

type __sync_and_and_fetch (type *ptr, type value, …)

type __sync_xor_and_fetch (type *ptr, type value, …)

type __sync_nand_and_fetch (type *ptr, type value, …)

        以下两个函数提供原子的比较和交换

bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, …)

type __sync_val_compare_and_swap (type *ptr, type oldval type newval, …)

如果*ptr == oldval,就将newval写入*ptr,

第一个函数在相等并写入的情况下返回true.

第二个函数在返回操作之前的值。

将*ptr设为value并返回*ptr操作之前的值。

type __sync_lock_test_and_set (type *ptr, type value, …)

将*ptr置0

void __sync_lock_release (type *ptr, …)

        因为 gcc 具体实现的问题, 后面的可扩展参数 (…) 没有什么用, 可以省略掉.

        gcc 保证了这些接口都是原子的. 调用这些接口时, 前端串行总线会被锁住, 锁住了它, 其它 cpu 就不能从存储器获取数据. 从而保证对内存操作的互斥. 当然, 这种操作是有不小代价, 所以只能在操作小的内存才可以这么做. 上面的接口使用的 type 只能是 1, 2, 4 或 8 字节的整形, 即:

int8_t / uint8_t

int16_t / uint16_t

int32_t / uint32_t

int64_t / uint64_t

性能上:原子操作的速度是互斥锁的6~7倍。

有了这些函数, 就可以很方便的进行原子操作了, 以 count++ 为例,

count 初始值为0, 可以这么写

__sync_fetch_and_add(&count, 1);    //返回0, count现在等于1, 类似 count ++

count 初始值为0, 或者这么写

__sync_add_and_fetch(&count, 1);    //返回1, count现在等于1, 类似 ++ count

原子操作也可以用来实现互斥锁:

int a = 0;

#define LOCK(a) while (__sync_lock_test_and_set(&a,1)) {sched_yield();}

#define UNLOCK(a) __sync_lock_release(&a);

        sched_yield()这个函数可以使用另一个级别等于或高于当前线程的线程先运行。如果没有符合条件的线程,那么这个函数将会立刻返回然后继续执行当前线程的程序。

如果去掉 sched_yield(), 这个锁就会一直自旋.

示例

        有了这些函数,对于多线程对全局变量进行操作(自加、自减等)问题,我们就不用考虑线程锁,可以考虑使用上述函数代替,和使用pthread_mutex保护的作用是一样的,线程安全且性能上完爆线程锁。

        下面是对线程锁和原子操作使用对比,并且进行性能测试与对比。弄懂后并稍微改动一点点。代码中分别给出加锁、加线程锁、原子计数操作三种情况的比较。

代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <linux/types.h>
#include <time.h>
#include <sys/time.h>#define INC_TO 1000000 // one million__u64 rdtsc ()
{__u32 lo, hi;__asm__ __volatile__("rdtsc":"=a"(lo),"=d"(hi));return (__u64)hi << 32 | lo;
}int global_int = 0;pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;//初始化互斥锁pid_t gettid ()
{return syscall(__NR_gettid);
}void * thread_routine1 (void *arg)
{int i;int proc_num = (int)(long)arg;__u64 begin, end;struct timeval tv_begin, tv_end;__u64 time_interval;cpu_set_t set;CPU_ZERO(&set);CPU_SET(proc_num, &set);if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &set)){fprintf(stderr, "failed to set affinity\n");return NULL;}begin = rdtsc();gettimeofday(&tv_begin, NULL);for (i = 0; i < INC_TO; i++){__sync_fetch_and_add(&global_int, 1);}gettimeofday(&tv_end, NULL);end = rdtsc();time_interval = (tv_end.tv_sec - tv_begin.tv_sec) * 1000000 + (tv_end.tv_usec - tv_begin.tv_usec);fprintf(stderr, "proc_num : %d, __sync_fetch_and_add cost %llu CPU cycle, cost %llu us\n", proc_num, end - begin, time_interval);return NULL;
}void *thread_routine2(void *arg)
{int i;int proc_num = (int)(long)arg;__u64 begin, end;struct timeval tv_begin, tv_end;__u64 time_interval;cpu_set_t set;CPU_ZERO(&set);CPU_SET(proc_num, &set);if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &set)){fprintf(stderr, "failed to set affinity\n");return NULL;}begin = rdtsc();gettimeofday(&tv_begin, NULL);for (i = 0; i < INC_TO; i++){pthread_mutex_lock(&count_lock);global_int++;pthread_mutex_unlock(&count_lock);}gettimeofday(&tv_end, NULL);end = rdtsc();time_interval = (tv_end.tv_sec - tv_begin.tv_sec) * 1000000 + (tv_end.tv_usec - tv_begin.tv_usec);fprintf(stderr, "proc_num : %d, pthread_mutex_lock cost %llu CPU cycle, cost %llu us\n", proc_num, end - begin, time_interval);return NULL;  
}void *thread_routine3(void *arg)
{int i;int proc_num = (int)(long)arg;__u64 begin, end;struct timeval tv_begin, tv_end;__u64 time_interval;cpu_set_t set;CPU_ZERO(&set);CPU_SET(proc_num, &set);if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &set)){fprintf(stderr, "failed to set affinity\n");return NULL;}begin = rdtsc();gettimeofday(&tv_begin, NULL);for (i = 0; i < INC_TO; i++){global_int++;}gettimeofday(&tv_end, NULL);end = rdtsc();time_interval = (tv_end.tv_sec - tv_begin.tv_sec) * 1000000 + (tv_end.tv_usec - tv_begin.tv_usec);fprintf(stderr, "proc_num : %d, no lock cost %llu CPU cycle, cost %llu us\n", proc_num, end - begin, time_interval);return NULL;
}int main()
{int procs = 0;int all_cores = 0;int i;pthread_t *thrs;procs = (int)sysconf(_SC_NPROCESSORS_ONLN);if (procs < 0){fprintf(stderr, "failed to fetch available CPUs(Cores)\n");return -1;}all_cores = (int)sysconf(_SC_NPROCESSORS_CONF);if (all_cores < 0){fprintf(stderr, "failed to fetch system configure CPUs(Cores)\n");return -1;}printf("system configure CPUs(Cores): %d\n", all_cores);printf("system available CPUs(Cores): %d\n", procs);thrs = (pthread_t *)malloc(sizeof(pthread_t) * procs);if (thrs == NULL){fprintf(stderr, "failed to malloc pthread array\n");return -1;}printf("starting %d threads...\n", procs);for (i = 0; i < procs; i++){if (pthread_create(&thrs[i], NULL, thread_routine1, (void *)(long) i)){fprintf(stderr, "failed to pthread create\n");procs = i;break;}}for (i = 0; i < procs; i++){pthread_join(thrs[i], NULL);}printf("after doing all the math, global_int value is: %d\n", global_int);printf("expected value is: %d\n", INC_TO * procs);free (thrs);return 0;
}

运行结果

     每次修改不同thread_routine?()函数,重新编译即可测试不同情况。

g++ main.cpp -D _GNU_SOURCE -l pthread
 ./a.out

         不加锁下运行结果:

system configure CPUs(Cores): 8
system available CPUs(Cores): 8
starting 8 threads...
proc_num : 5, no lock cost 158839371 CPU cycle, cost 66253 us
proc_num : 6, no lock cost 163866879 CPU cycle, cost 68351 us
proc_num : 2, no lock cost 173866203 CPU cycle, cost 72521 us
proc_num : 7, no lock cost 181006344 CPU cycle, cost 75500 us
proc_num : 1, no lock cost 186387174 CPU cycle, cost 77728 us
proc_num : 0, no lock cost 186698304 CPU cycle, cost 77874 us
proc_num : 3, no lock cost 196089462 CPU cycle, cost 81790 us
proc_num : 4, no lock cost 200366793 CPU cycle, cost 83576 us
after doing all the math, global_int value is: 1743884
expected value is: 8000000

 线程锁下运行结果:

system configure CPUs(Cores): 8
system available CPUs(Cores): 8
starting 8 threads...
proc_num : 1, pthread_mutex_lock cost 9752929875 CPU cycle, cost 4068121 us
proc_num : 5, pthread_mutex_lock cost 10038570354 CPU cycle, cost 4187272 us
proc_num : 7, pthread_mutex_lock cost 10041209091 CPU cycle, cost 4188374 us
proc_num : 0, pthread_mutex_lock cost 10044102546 CPU cycle, cost 4189546 us
proc_num : 6, pthread_mutex_lock cost 10113533973 CPU cycle, cost 4218541 us
proc_num : 4, pthread_mutex_lock cost 10117540197 CPU cycle, cost 4220212 us
proc_num : 3, pthread_mutex_lock cost 10160384391 CPU cycle, cost 4238083 us
proc_num : 2, pthread_mutex_lock cost 10164464784 CPU cycle, cost 4239778 us
after doing all the math, global_int value is: 8000000
expected value is: 8000000

 原子操作__sync_fetch_and_add下运行结果:

system configure CPUs(Cores): 8
system available CPUs(Cores): 8
starting 8 threads...
proc_num : 3, __sync_fetch_and_add cost 2364148575 CPU cycle, cost 986129 us
proc_num : 1, __sync_fetch_and_add cost 2374990974 CPU cycle, cost 990652 us
proc_num : 2, __sync_fetch_and_add cost 2457930267 CPU cycle, cost 1025247 us
proc_num : 5, __sync_fetch_and_add cost 2463027030 CPU cycle, cost 1027373 us
proc_num : 7, __sync_fetch_and_add cost 2532240981 CPU cycle, cost 1056244 us
proc_num : 4, __sync_fetch_and_add cost 2555055054 CPU cycle, cost 1065760 us
proc_num : 0, __sync_fetch_and_add cost 2561248971 CPU cycle, cost 1068331 us
proc_num : 6, __sync_fetch_and_add cost 2558781396 CPU cycle, cost 1067314 us
after doing all the math, global_int value is: 8000000
expected value is: 8000000

通过测试结果可以看出:
 
        1. 不加锁的情况下,不能获得正确结果。
                测试结果表明,正确结果为8000000,而实际为1743884。表明多线程下修改全局计数,不加锁的话是错误的;

        2. 加锁情况下,无论是线程锁还是原子性操作,均可获得正确结果。

        3. 性能上__sync_fetch_and_add()完爆线程锁。


        从性能测试结果上看,__sync_fetch_and_add()速度大致是线程锁的4-5倍。

测试结果对比
类型平均CPU周期(circle)平均耗时(us)
不加锁18089006675449.13
线程锁100540919014193740.875
原子操作24834279061035881.25

注:如上的性能测试结果,表明__sync_fetch_and_add()速度大致是线程锁的4-5倍。

         24cores实体机测试结果,表明__sync_fetch_and_add()速度大致只有线程锁的2-3倍。

24 cores实体机测试结果
类型平均CPU周期(circle)平均耗时(us)
不加锁535457026233310.5
线程锁93319154804066156.667
原子操作37699007951643463.625

总结

总体看来,原子操作__sync_fetch_and_add()大大的优于线程锁。

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

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

相关文章

Vue3生命周期函数(简述题)

1.图示 2.说明 3.补充 1.在vue3组合式API中&#xff0c;我们需要将生命周期函数先导入&#xff0c;然后才能使用。 import {onMounted} from vue2.beforeCreate和created被setup()方法所代替

re:Invent 构建未来:云计算生成式 AI 诞生科技新局面

文章目录 前言什么是云计算云计算类型亚马逊云科技云计算最多的功能最大的客户和合作伙伴社区最安全最快的创新速度最成熟的运营专业能力 什么是生成式 AI如何使用生成式 AI后记 前言 在科技发展的滚滚浪潮中&#xff0c;我们见证了云计算的崛起和生成式 AI 的突破&#xff0c…

基于ssm亚盛汽车配件销售业绩管理系统

摘 要 如今的信息时代&#xff0c;对信息的共享性&#xff0c;信息的流通性有着较高要求&#xff0c;因此传统管理方式就不适合。为了让亚盛汽车配件销售信息的管理模式进行升级&#xff0c;也为了更好的维护亚盛汽车配件销售信息&#xff0c;亚盛汽车配件销售业绩管理系统的开…

RNN-T Training,RNN-T模型训练详解——语音信号处理学习(三)(选修三)

参考文献&#xff1a; Speech Recognition (option) - RNN-T Training哔哩哔哩bilibili 2020 年 3月 新番 李宏毅 人类语言处理 独家笔记 Alignment Train - 8 - 知乎 (zhihu.com) 本次省略所有引用论文 目录 一、如何将 Alignment 概率加和 对齐方式概率如何计算 概率加和计…

C#中 怎么检测Tcp网线断开?

在 C# 中&#xff0c;如果使用 TcpClient 或 TcpListener 这样的套接字进行通信&#xff0c;并且网络连接断开&#xff0c;不发送心跳是无法立即检测到断开的。这是因为 TCP 协议本身没有内置的机制来检测连接是否还活动中。 当使用 TCP 进行通信时&#xff0c;通常是通过发送…

PyQt6第一个程序HelloWorld实现

锋哥原创的PyQt6视频教程&#xff1a; 2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~共计12条视频&#xff0c;包括&#xff1a;2024版 PyQt6 Python桌面开发 视频教程(无废话版…

LeetCode63. Unique Paths II

文章目录 一、题目二、题解 一、题目 You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only m…

three.js球体实现

作者&#xff1a;baekpcyyy&#x1f41f; 使用three.js渲染出可以调节大小的立方体 1.搭建开发环境 1.首先新建文件夹用vsc打开项目终端 2.执行npm init -y 创建配置文件夹 3.执行npm i three0.152 安装three.js依赖 4.执行npm I vite -D 安装 Vite 作为开发依赖 5.根…

网络协议系列:TCP三次握手,四次挥手的全过程,为什么需要三次握手,四次挥手

TCP三次握手&#xff0c;四次挥手的全过程&#xff0c;为什么需要三次握手&#xff0c;四次挥手 一. TCP三次握手&#xff0c;四次挥手的全过程&#xff0c;为什么需要三次握手&#xff0c;四次挥手前言TCP协议的介绍三次握手三次握手流程&#xff1a;1. A 的 TCP 向 B 发送 连…

【嵌入式Linux开发一路清障-连载04】虚拟机VirtualBox7.0安装Ubuntu22.04后挂载Windows平台共享文件夹

虚拟机安装Ubuntu22.04后挂载Windows平台共享文件夹 障碍07-虚拟机VirtualBox7.0完装完Ubuntu22.04后&#xff0c;无法成功挂载Windows平台中共享文件夹&#xff0c;无法访问电脑中的各类重要文件&#xff0c;我该怎么办&#xff1f;一、问题的模样&#xff1a;VirtualBox7.0设…

LeetCode:907. 子数组的最小值之和(单调栈 C++ 、Java)

目录 907. 子数组的最小值之和 题目描述&#xff1a; 实现代码与解析&#xff1a; 单调栈 原理思路&#xff1a; 907. 子数组的最小值之和 题目描述&#xff1a; 给定一个整数数组 arr&#xff0c;找到 min(b) 的总和&#xff0c;其中 b 的范围为 arr 的每个&#xff08;…

【算法训练营】算法分析实验(递归实现斐波那契+插入排序、分治思想实现归并排序+快排)附代码+解析

![0 &#x1f308;欢迎来到算法专栏 &#x1f64b;&#x1f3fe;‍♀️作者介绍&#xff1a;前PLA队员 目前是一名普通本科大三的软件工程专业学生 &#x1f30f;IP坐标&#xff1a;湖北武汉 &#x1f349; 目前技术栈&#xff1a;C/C、Linux系统编程、计算机网络、数据结构、M…

SpringBoot : ch08 自动配置原理

前言 在现代的Java开发中&#xff0c;Spring Boot已经成为了一个备受欢迎的框架。它以其简化开发流程、提高效率和强大的功能而闻名&#xff0c;使得开发人员能够更加专注于业务逻辑的实现而不必过多地关注配置问题。 然而&#xff0c;你是否曾经好奇过Spring Boot是如何做到…

白盒测试 接口测试 自动化测试

一、什么是白盒测试 白盒测试是一种测试策略&#xff0c;这种策略允许我们检查程序的内部结构&#xff0c;对程序的逻辑结构进行检查&#xff0c;从中获取测试数据。白盒测试的对象基本是源程序&#xff0c;所以它又称为结构测试或逻辑驱动测试&#xff0c;白盒测试方法一般分为…

Python编程基础:数据类型和运算符解析

想要学习Python编程语言&#xff1f;本文将为您介绍Python中常见的数据类型和运算符&#xff0c;为您打下坚实的编程基础。了解不同的数据类型和运算符&#xff0c;掌握它们之间的配合方式&#xff0c;让您能够更轻松地进行数据处理和计算任务。无论您是初学者还是有一定经验的…

电能量数据采集终端是电表采集器吗?

随着科技的发展和能源管理的日益精细化&#xff0c;电能量数据采集终端——电表采集器在保障电力系统稳定运行、实现节能减排等方面发挥着越来越重要的作用。下面&#xff0c;小编来为大家全面介绍电表采集器的功能、应用场景及其在我国能源领域的价值。 一、电表采集器的定义与…

Golang rsa 验证

一下代码用于rsa 签名的验签&#xff0c; 签名可以用其他语言产生。也可以用golang生成。 package mainimport ("crypto""crypto/rsa""crypto/sha256""crypto/x509""encoding/pem""errors""fmt" )fun…

分治法之快速排序

思路: 选择一个基准值&#xff0c;通常是数组中的第一个元素。 将数组分为两部分&#xff0c;一部分是小于基准值的元素&#xff0c;另一部分是大于基准值的元素。 对这两部分分别进行递归排序&#xff0c;直到子数组长度为 1 或 0。 合并排序好的两部分&#xff0c;得到最终…

第二十章Java博客

如果一次只完成一件事情&#xff0c;很容易实现。但现实生活中&#xff0c;很多事情都是同时进行的。Java中为了模拟这种状态&#xff0c;引入了线程机制。简单地说&#xff0c;当程序同时完成多件事情时&#xff0c;就是所谓的多线程。多线程应用相当广泛&#xff0c;使用多线…

【Java学习笔记】 74 - 本章作业

1.验证电子邮件格式是否合法 规定电子邮件规则为 1.只能有一个 2. 前面是用户名,可以是a-z A-Z 0-9 _ - 字符 3. 后面是域名&#xff0c;并且域名只能是英文字母&#xff0c;比如sohu.com或者tsinghua.org.cn 4.写出对应的正则表达式&#xff0c;验证输入的字符串是否为满…