Thread.MemoryBarrier 方法

Thread.MemoryBarrier 方法classFoo{int_answer;bool_complete;voidA(){_answer=123;_complete=true;}voidB(){if(_complete)Console.Wri…

大家好,又见面了,我是你们的朋友全栈君。

class Foo
{
    int _answer;
    bool _complete;
    void A()
    {
        _answer = 123;
        _complete = true;
    }
 
    void B()
    {
        if (_complete) Console.WriteLine(_answer);
    }
}
 
如果A和B是运行在2个并行的线程上,B有可能打印出0。
出于2中原因:
 
1,编译器,CLR或者CPU可能重新排序了程序指令,以此提高效率。
2,编译器,CLR或者CPU引入缓存优化导致其他的线程不能马上看到变量值的更改。
 
使用Memory barrier可以防止重新排序程序指令或者缓存。调用Thread.MemoryBarrier就是实现一个Memory barrier。
 
class Foo
{
    int _answer;
    bool _complete;
    void A()
    {
        _answer = 123;
        Thread.MemoryBarrier(); // Barrier 1
        _complete = true;
        Thread.MemoryBarrier(); // Barrier 2
    }
    void B()
    {
        Thread.MemoryBarrier(); // Barrier 3
        if (_complete)
        {
            Thread.MemoryBarrier(); // Barrier 4
            Console.WriteLine(_answer);
        }
    }
}
 
Thread.MemoryBarrier()按如下方式同步内存访问:执行当前线程的处理器在对指令重新排序时,不能采用先执行 MemoryBarrier 调用之后的内存访问,再执行 MemoryBarrier 调用之前的内存访问的方式。
调用一次MemoryBarrier大约花费10纳秒.



















本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/526398
,如需转载请自行联系原作者



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157914.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月12日 下午5:00
下一篇 2022年7月12日 下午5:00


相关推荐

  • repos_there are no enabled repos

    repos_there are no enabled repos配置Base源,使用163或者阿里的1、mirrors.163.com/2、mirrors.aliyun.com/or https://developer.aliyun.com/mir

    2022年8月4日
    9
  • 计算机网络实验 局域网的组建,计算机网络实验-局域网组建及测试实验

    计算机网络实验 局域网的组建,计算机网络实验-局域网组建及测试实验局域网组建及测试实验一 实验目的 1 掌握使用双绞线作为传输介质的网络连接方法 学会制作两种类型的接头 学会测线器的使用方法 2 以双绞线为传输介质连接多台计算机 要求掌握基本的计算机网络知识 TCP IP 协议常识 3 使用常用网络命令 测试分析网络状态 要求掌握基本的网络命令使用要点 二 实验设备及环境交换机 带 RJ 45 接口网卡的微机 5 类双绞线 水晶头 压线钳 测线器 三 实验原理 1 理解 10

    2026年3月18日
    2
  • 零基础入门微信小程序开发 (2020 版)_GitChat(微信小程序开发入门书籍)

    本课程是一个系列入门教程,目标是从0开始带领读者上手实战,课程以微信小程序的核心概念作为主线,介绍配置文件、页面样式文件、JavaScript的基本知识并以指南针为例对基本知识进行扩展,另外加上开发工具的安装、小程序发布等内容,共9篇文章。

    2022年4月13日
    105
  • python easyOCR 使用案例

    python easyOCR 使用案例先上效果图下面是图文提取的代码 frompathlibi url r 识别图片 jpg 需识别的图片 split symbol 默认空格为分隔符 row space 15 默认字符高度为 15px 当识别出来的字符间距超过这个数值时会换行 defmake reader 将模型加载到内存中 模型文件地址 C Users 用户

    2026年3月17日
    2
  • go语言 ssh_websshpro

    go语言 ssh_websshprogowebssh推荐使用文章目录gowebssh推荐使用1.前言2.项目地址3.简单修改4.交叉编译5.结果展示1.前言一般ssh的登录使用需要ssh客户端进行登录,比如xshell、putty等,之前我们也推荐过九款:https://blog.csdn.net/weixin_39510813/article/details/118722265。目前我们有一个需求是在web上登录设备的ssh,一番搜索后找到一个比较小巧合适的,并且做了一点简单的修改,基于go+vue的,最后可以打

    2025年6月2日
    7
  • vue全局变量、全局方法

    vue全局变量、全局方法1 全局变量 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp main js 中设置如下内容页面中使用如下 2 全局方法 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号