博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET:CLR via C# User-Mode Constructs
阅读量:6235 次
发布时间:2019-06-22

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

The CLR guarantees that reads and writes to variables of the following data types are atomic: Boolean, Char, (S)Byte, (U)Int16, (U)Int32, (U)IntPtr, Single, and reference types. This means that all bytes within that variable are read from or written to all at once.

Although atomic access to variable guarantees that the read or write happens all at once, it does not guarantee when the read or write will happen due to compiler and CPU optimizations. The primitive user-mode constructs discussed in this section are used to enforce the timing of these atomic read and write operations. In addition, these constructs can also force atomic and timed access to variables of additional data types: (U)Int64 and Double.

There are two kinds of primitive user-mode thread synchronization constructs:

  • Volatile constructs, which perform an atomic read or write operation on a variable containing a simple data type at a specific time
  • Interlocked constructs, which perform an atomic read and write operation on a variable containing a simple data type at a specific time

All the volatile and interlocked constructs require you to pass a reference (memory address) to a variable containing a simple data type.

Volatile Constructs

Back in the early days of computing, software was written using assembly language. Assembly language is very tedious, because programmers must explicitly state everything—use this CPU register for this, branch to that, call indirect through this other thing, and so on. To simplify programming, CHAPTER 29 Primitive Thread Synchronization Constructs 763 higher-level languages were introduced. These higher-level languages introduced common useful constructs, like if/else, switch/case, various loops, local variables, arguments, virtual method calls, operator overloads, and much more. Ultimately, these language compilers must convert the high-level constructs down to the low-level constructs so that the computer can actually do what you want it to do.

In other words, the C# compiler translates your C# constructs into Intermediate Language (IL), which is then converted by the just-in-time (JIT) compiler into native CPU instructions, which must then be processed by the CPU itself. In addition, the C# compiler, the JIT compiler, and even the CPU itself can optimize your code.

The static System.Threading.Volatile class offers two static methods that look like this.

1 public static class Volatile {2   public static void Write(ref Int32 location, Int32 value);3   public static Int32 Read(ref Int32 location);4 }

These methods are special. In effect, these methods disable some optimizations usually performed by the C# compiler, the JIT compiler, and the CPU itself. Here’s how the methods work:

  • The Volatile.Write method forces the value in location to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to Volatile.Write.
  • The Volatile.Read method forces the value in location to be read from at the point of the call. In addition, any later program-order loads and stores must occur after the call to Volatile.Read.

Important I know that this can be very confusing, so let me summarize it as a simple rule. When threads are communicating with each other via shared memory, write the last value by calling Volatile.Write and read the first value by calling Volatile.Read.

Interlocked Constructs

Volatile’s Read method performs an atomic read operation, and its Write method performs an atomic write operation. That is, each method performs either an atomic read operation or an atomic write operation. In this section, we look at the static System.Threading.Interlocked class’s methods. Each of the methods in the Interlocked class performs an atomic read and write operation. In addition, all the Interlocked methods are full memory fences. That is, any variable writes before the call to an Interlocked method execute before the Interlocked method, and any variable reads after the call execute after the call.

 

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

你可能感兴趣的文章
NodeJs如何全局统一处理异常,实现RestFull风格
查看>>
算法基础之经典算法
查看>>
从外部连接Broadleaf Demo数据库
查看>>
编程大牛 Bruce Eckel 对新程序员的忠告
查看>>
一次踩坑经历看vue几个组件通信的适用场景
查看>>
MySQL的语句执行顺序
查看>>
JavaScript基础语法 变量 常量 数据类型
查看>>
Java™ 教程(仔细看看“Hello World!”应用程序)
查看>>
flutter中的异步
查看>>
tensorflow学习之Anaconda开发环境搭建
查看>>
[JS]《你不知道的Javascript·上》——this关键字
查看>>
如何理解 (object.getName = object.getName)() 这段代码?
查看>>
Spring AOP 源码分析系列文章导读
查看>>
Linux - 系统 - 文件目录
查看>>
[LeetCode] 267. Palindrome Permutation II
查看>>
前端妹纸的进阶之路——redux源码分析
查看>>
Centos7下使用gitolite搭建git服务器
查看>>
如何更好的编写async函数
查看>>
【前端工程师手册】JavaScript之this的笔记
查看>>
使用nginx来为你在一台服务器部署多个Web Server
查看>>