博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
32位与64位原子操作的问题
阅读量:7127 次
发布时间:2019-06-28

本文共 1238 字,大约阅读时间需要 4 分钟。

这是微软介绍The Interlocked API的部分,不过里面讲到了原子操作的问题,虽然应该只是针对微软操作系统,不过我倒是觉得所有操作系统应该是一致的。
对CPU位数相同或低于CPU位数且字节对齐的一个数值操作,应该可以完成赋值取值操作,赋值后该数一定为要赋的值,取值后取到的也一定是该值被赋的一个值,都不会出现中间值。
所以最终觉得32位整数,倘若保证只有一个线程会写值,其他线程读值,是不可能会出错的。
倘若多线程读写,不过倘若多线程只是用它作为一个bool值,且通过bool值通知而已,又不在乎多线程相应,我觉得这样用也不应没有什么不可以的。
 
Interlocked Variable Access

Applications must synchronize access to variables that are shared by multiple threads. Applications must also ensure that operations on these variables are performed atomically (performed in their entirety or not at all.)

Simple reads and writes to properly-aligned 32-bit variables are atomic operations. In other words, you will not end up with only one portion of the variable updated; all bits are updated in an atomic fashion. However, access is not guaranteed to be synchronized. If two threads are reading and writing from the same variable, you cannot determine if one thread will perform its read operation before the other performs its write operation.

Simple reads and writes to properly aligned 64-bit variables are atomic on 64-bit Windows. Reads and writes to 64-bit values are not guaranteed to be atomic on 32-bit Windows. Reads and writes to variables of other sizes are not guaranteed to be atomic on any platform.

转载地址:http://sjhel.baihongyu.com/

你可能感兴趣的文章
Selenium2(webdirver)入门之环境搭建(Java版)
查看>>
MySQL入门-10:子查询与联结表
查看>>
VMware三个版本workstation、server、esxi的区别
查看>>
Nginx 安装及调优
查看>>
hbase shell基本操作命令详解
查看>>
网络编程学习——数据链路访问
查看>>
CentOS6.2下搭建LVS(NAT)+Keepalived实现高性能高可用负载均衡服务
查看>>
Centos7下安装及配置PPTP ***
查看>>
oracle 配置监听器
查看>>
Red Hat 配置本地 yum源
查看>>
[翻译]CloudBees 设计原则
查看>>
MFS分布式文件系统的管理和使用维护(二)
查看>>
Spring实现AOP的几种方式详解
查看>>
海量数据处理专题(五)——堆
查看>>
我的友情链接
查看>>
Linux下双网卡双网关配置路由表
查看>>
Windows Server 2012 和 System Center 2012 SP1,Virtual Machine Manager 中启用的软件定义的网络...
查看>>
跟我学习dubbo-Dubbo管理控制台的安装(3)
查看>>
Divice eth0 dose not seem to be present ,delaying initalization解决办法
查看>>
02-03-linux根文件系统详解
查看>>