C++ 并发编程指南(11)原子操作 | 11.2、atomic_flag

c++11 atomic 内存序 5.test_and_set() 函数检查 std::atomic_flag 标志,如果 std::atomic_flag 之前没有被设置过,则设置 std::atomic_flag 的标志,并返回先前该 std::atomic_flag 对象是否被设置过,如果之前 std::atomic_flag 对象已被设置,则返回 true,否则返回 false。3. 初始状态c++20之前不确定 ,c++20之后是false。7. c++20以前只提供了上面两个方法。 阅读详情


前言

C++ atomic_flag是C++标准库中的一个原子标志类,用于实现线程间的同步和互斥。它是C++11引入的,提供了一种轻量级的原子操作方式,可以用于实现简单的互斥锁和条件变量等功能。

一、atomic_flag

std::atomic_flag 是 C++11 标准库中的一个类,它提供了一种简单的、无锁的、低开销的原子操作。std::atomic_flag 通常用于那些只需要两个状态的场景,例如作为一个标志或者是一个锁。它只能进行两种操作:设置(clear())和清除(test_and_set())。

1、特点

  • 简单性:std::atomic_flag 提供了最基础的原子操作,没有提供复杂的比较和交换(compare-and-swap)或加载/存储操作。
  • 无锁:使用 std::atomic_flag 可以实现无锁编程,这通常意味着更高的性能,但也可能导致更复杂的代码。
  • 低开销:由于 std::atomic_flag 的操作非常简单,所以它的开销通常比其他更复杂的原子类型要小。

2、成员函数

  • clear():将标志设置为false。
  • test_and_set():设置标志为true,并返回之前的值。
  • test_and_set_ex
C++并发编程: std::atomic_flag 与自旋锁 是C++中用于轻量级线程同步的基本原子类型。它非常适合实现简单的自旋锁等同步机制。在x64架构上,的操作依赖于XCHG和BTR等硬件指令,这些指令通过LOCK前缀确保操作的原子性。通过使用,开发者可以实现高效的线程同步机制,避免数据竞争问题。 阅读详情

相关推荐

C++11多线程学习(1)atomic_flag

d学了这么久我才刚刚知道C++11居然对多线程有如此丰富的支持,一方面感慨自己学的还是太少了,一方面真的很开心可以告别自己封装mutex,线程,currentthread这些东西时代了。最重要的一点,在windows上写的代码和在linux上也可以编译运行,这意味着可以使用宇宙第一IDE来调试多线程程序。我会用这个系列记录自己学习C++11多线程的过程。 C++11中引入了thread这个头文件...

CL_XYZ 994

C++11多线程(三):atomic与atomic_flag的使用与原理分析

C++11提供了线程的原子操作,这些在boost库中也有,在多个线程时原子操作保证了数据的安全性。C++11的原子操作主要是atomic和atomic_flag,使用时需要包含如下头文件: #include<atomic> 一.atmoic类的使用 template<>struct atomic<_ITYPE> ,...

琅嬛福地 4378

图形学基础|基于SDF的卡通阴影图

图形学基础|基于SDF的卡通阴影图 文章目录图形学基础|基于SDF的卡通阴影图一、前言二、SDF(Signed Distance Field)三、阴影图生成3.1 SDF生成3.2 插值参考博文 一、前言 回顾之前做的卡通渲染,角色面部阴影是靠一张阴影贴图实现的。 为了更好地控制角色的脸部阴影,通过计算脸部朝向与光照方向的夹角和一张单色的FaceMap来判断。 FaceMap的每个像素表示:该像素判断为阴影时候需要达到的夹角。 例如: 当方向光正对角色的时候,夹角为0°,对应的值为0,此时,FaceMap

【黑键】 1万+

景观格局指数计算方法及代表的生态学意义(待补充)

景观指数 斑块密度(PD) 斑块密度(PD)表现某种斑块在景观中的密度,可反映出景观整体的异质性与破碎度[17],公式为PD=NP/A(1)式中NP———斑块数量,个A———景观或斑块的总面积,hm2PD———斑块密度,个/hm2 最大斑块指数(LPI) 最大斑块指数(LPI)用于确定景观中的优势斑块类型,间接反映人类活动干扰的方向和大小,公式为LPI=amaxA×100%(0≤LPI≤100%)(2)式中amax———景观或某一种斑块类型中最大斑块的面积,hm2LPI———最大斑块指数,% 凝聚

soderayer的博客 13万+

GPU算力租用平台推荐

阿里云作为中国领先的云计算服务提供商,提供了丰富的GPU云服务器实例,包括NVIDIA Tesla V100、A100等多种高性能GPU型号。阿里云GPU云服务器适用于深度学习、科学计算、图形渲染等多种场景,能够满足不同用户的计算需求。

hong161688的博客 8175

记一次wireshark抓取QQ好友IP和火绒抓取微信IP

事情是这样的,,上个周,手机离奇的被盗,但没有关机,为了找回手机,粗学了一手QQ抓ip的手段。 这里我就直接做个演示,解析关键的点。 三种思路i. wireshark 抓取手机在线IPii 火绒抓取 微信在线接听IPiii 火绒抓取 微信在线不接听IP i. wireshark 抓取手机在线IP wireshark抓好友QQ的ip要求和对方打语音的时候要手机在线。 这里说一下,打语音和视频是直接能和对方建立连接,如果发文件,是直接发给腾讯的,抓不到包。 1.打开wireshark,通过电脑Tim给对方打语音

qq_29437513的博客 1万+

Device Mapper 代码分析

Device Mapper(DM)是Linux 2.6全面引入的块设备新构架,通过DM可以灵活地管理系统中所有的真实或虚拟的块设备。DM以块设备的形式注册到Linux内核中,凡是挂载(或者说“映射”)于DM结构下的块设备,不管他们是如何组织,如何通讯,在Linux看来都是一个完整的DM块设备。因此DM让不同组织形式的块设备或者块设备集群在Linux内核面前有一个完整统一的DM表示。一、DM

SonicLing的专栏 1万+

2021年03月 C/C++(五级)真题解析#中国电子学会#全国青少年软件编程等级考试

给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0。n的位数为m。现在从m位中删除k位(0

人生就是一个不断学习的过程 1011

YOLOv8 WIOU

本文介绍了如何在YOLOv8中实现WIOU(Weighted Intersection over Union)的修改方法。主要修改了ultralytics/utils/metrics.py文件中的bbox_iou函数,新增了WIoU_Scale类,支持单调性FM v2、非单调性FM v3两种模式,并提供了动态调整的动量参数。修改后的函数支持多种IoU变体(GIoU、DIoU、CIoU等),通过数学公式实现了更精确的边界框回归损失计算,特别针对小目标检测场景进行了优化。该实现使用了PyTorch框架,包含完整

FJN110的博客 2053

OpenCV计算机视觉实战(3)——计算机图像处理基础

本文全面介绍了计算机视觉和数字图像处理的基础知识。从最基本的像素概念入手,详细讲解了图像在计算机中的表示方式,包括RGB、CMYK等色彩空间的工作原理及其应用场景。还系统分析了常见图像和视频文件格式的特点,梳理了计算机图像处理技术的发展历程,并重点介绍了OpenCV这一强大的开源计算机视觉库的功能与应用领域。

qq_30167691的博客 1741

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

上一篇: Qt 实战(2)搭建开发环境 | 2.1、Windows下安装QT
下一篇: C++ 并发编程指南(11)原子操作 | 11.3、原子变量与内存序
一个不务正业的程序猿
博客等级 码龄11年 1879粉丝 563原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值