转自:http://name5566.com/4535.html

参考文献列表:
http://en.wikipedia.org/wiki/Memory_barrier
http://en.wikipedia.org/wiki/Out-of-order_execution
https://www.kernel.org/doc/Documentation/memory-barriers.txt

本文例子均在 Linux(g++)下验证通过,CPU 为 X86-64 处理器架构。所有罗列的 Linux 内核代码也均在(或只在)X86-64 下有效。

本文首先通过范例(以及内核代码)来解释 Memory barrier,然后介绍一个利用 Memory barrier 实现的无锁环形缓冲区。

Memory barrier 简介

程序在运行时内存实际的访问顺序和程序代码编写的访问顺序不一定一致,这就是内存乱序访问。内存乱序访问行为出现的理由是为了提升程序运行时的性能。内存乱序访问主要发生在两个阶段:

  1. 编译时,编译器优化导致内存乱序访问(指令重排)
  2. 运行时,多 CPU 间交互引起内存乱序访问

Memory barrier 能够让 CPU 或编译器在内存访问上有序。一个 Memory barrier 之前的内存访问操作必定先于其之后的完成。Memory barrier 包括两类:

  1. 编译器 barrier
  2. CPU Memory barrier

很多时候,编译器和 CPU 引起内存乱序访问不会带来什么问题,但一些特殊情况下,程序逻辑的正确性依赖于内存访问顺序,这时候内存乱序访问会带来逻辑上的错误,例如:

  1. // thread 1
  2. while (!ok);
  3. do(x);
  4. // thread 2
  5. x = 42;
  6. ok = 1;

此段代码中,ok 初始化为 0,线程 1 等待 ok 被设置为 1 后执行 do 函数。假如说,线程 2 对内存的写操作乱序执行,也就是 x 赋值后于 ok 赋值完成,那么 do 函数接受的实参就很可能出乎程序员的意料,不为 42。

编译时内存乱序访问

在编译时,编译器对代码做出优化时可能改变实际执行指令的顺序(例如 gcc 下 O2 或 O3 都会改变实际执行指令的顺序):

  1. // test.cpp
  2. int x, y, r;
  3. void f()
  4. {
  5. x = r;
  6. y = 1;
  7. }

编译器优化的结果可能导致 y = 1 在 x = r 之前执行完成。首先直接编译此源文件:

  1. g++ -S test.cpp

得到相关的汇编代码如下:

  1. movl r(%rip), %eax
  2. movl %eax, x(%rip)
  3. movl $1, y(%rip)

这里我们看到,x = r 和 y = 1 并没有乱序。现使用优化选项 O2(或 O3)编译上面的代码(g++ -O2 -S test.cpp),生成汇编代码如下:

  1. movl r(%rip), %eax
  2. movl $1, y(%rip)
  3. movl %eax, x(%rip)

我们可以清楚的看到经过编译器优化之后 movl $1, y(%rip) 先于 movl %eax, x(%rip) 执行。避免编译时内存乱序访问的办法就是使用编译器 barrier(又叫优化 barrier)。Linux 内核提供函数 barrier() 用于让编译器保证其之前的内存访问先于其之后的完成。内核实现 barrier() 如下(X86-64 架构):

  1. #define barrier() __asm__ __volatile__("" ::: "memory")

现在把此编译器 barrier 加入代码中:

  1. int x, y, r;
  2. void f()
  3. {
  4. x = r;
  5. __asm__ __volatile__("" ::: "memory");
  6. y = 1;
  7. }

这样就避免了编译器优化带来的内存乱序访问的问题了(如果有兴趣可以再看看编译之后的汇编代码)。本例中,我们还可以使用 volatile 这个关键字来避免编译时内存乱序访问(而无法避免后面要说的运行时内存乱序访问)。volatile 关键字能够让相关的变量之间在内存访问上避免乱序,这里可以修改 x 和 y 的定义来解决问题:

  1. volatile int x, y;
  2. int r;
  3. void f()
  4. {
  5. x = r;
  6. y = 1;
  7. }

现加上了 volatile 关键字,这使得 x 相对于 y、y 相对于 x 在内存访问上有序。在 Linux 内核中,提供了一个宏 ACCESS_ONCE 来避免编译器对于连续的 ACCESS_ONCE 实例进行指令重排。其实 ACCESS_ONCE 实现源码如下:

  1. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))

此代码只是将变量 x 转换为 volatile 的而已。现在我们就有了第三个修改方案:

  1. int x, y, r;
  2. void f()
  3. {
  4. ACCESS_ONCE(x) = r;
  5. ACCESS_ONCE(y) = 1;
  6. }

到此基本上就阐述完了我们的编译时内存乱序访问的问题。下面开始介绍运行时内存乱序访问。

运行时内存乱序访问

在运行时,CPU 虽然会乱序执行指令,但是在单个 CPU 的上,硬件能够保证程序执行时所有的内存访问操作看起来像是按程序代码编写的顺序执行的,这时候 Memory barrier 没有必要使用(不考虑编译器优化的情况下)。这里我们了解一下 CPU 乱序执行的行为。在乱序执行时,一个处理器真正执行指令的顺序由可用的输入数据决定,而非程序员编写的顺序。
早期的处理器为有序处理器(In-order processors),有序处理器处理指令通常有以下几步:

  1. 指令获取
  2. 如果指令的输入操作对象(input operands)可用(例如已经在寄存器中了),则将此指令分发到适当的功能单元中。如果一个或者多个操作对象不可用(通常是由于需要从内存中获取),则处理器会等待直到它们可用
  3. 指令被适当的功能单元执行
  4. 功能单元将结果写回寄存器堆(Register file,一个 CPU 中的一组寄存器)

相比之下,乱序处理器(Out-of-order processors)处理指令通常有以下几步:

  1. 指令获取
  2. 指令被分发到指令队列
  3. 指令在指令队列中等待,直到输入操作对象可用(一旦输入操作对象可用,指令就可以离开队列,即便更早的指令未被执行)
  4. 指令被分配到适当的功能单元并执行
  5. 执行结果被放入队列(而不立即写入寄存器堆)
  6. 只有所有更早请求执行的指令的执行结果被写入寄存器堆后,指令执行的结果才被写入寄存器堆(执行结果重排序,让执行看起来是有序的)

从上面的执行过程可以看出,乱序执行相比有序执行能够避免等待不可用的操作对象(有序执行的第二步)从而提高了效率。现代的机器上,处理器运行的速度比内存快很多,有序处理器花在等待可用数据的时间里已经可以处理大量指令了。
现在思考一下乱序处理器处理指令的过程,我们能得到几个结论:

  1. 对于单个 CPU 指令获取是有序的(通过队列实现)
  2. 对于单个 CPU 指令执行结果也是有序返回寄存器堆的(通过队列实现)

由此可知,在单 CPU 上,不考虑编译器优化导致乱序的前提下,多线程执行不存在内存乱序访问的问题。我们从内核源码也可以得到类似的结论(代码不完全的摘录):

  1. #ifdef CONFIG_SMP
  2. #define smp_mb() mb()
  3. #else
  4. #define smp_mb() barrier()
  5. #endif

这里可以看到,如果是 SMP 则使用 mb,mb 被定义为 CPU Memory barrier(后面会讲到),而非 SMP 时,直接使用编译器 barrier。

在多 CPU 的机器上,问题又不一样了。每个 CPU 都存在 cache(cache 主要是为了弥补 CPU 和内存之间较慢的访问速度),当一个特定数据第一次被特定一个 CPU 获取时,此数据显然不在 CPU 的 cache 中(这就是 cache miss)。此 cache miss 意味着 CPU 需要从内存中获取数据(这个过程需要 CPU 等待数百个周期),此数据将被加载到 CPU 的 cache 中,这样后续就能直接从 cache 上快速访问。当某个 CPU 进行写操作时,它必须确保其他的 CPU 已经将此数据从它们的 cache 中移除(以便保证一致性),只有在移除操作完成后此 CPU 才能安全的修改数据。显然,存在多个 cache 时,我们必须通过一个 cache 一致性协议来避免数据不一致的问题,而这个通讯的过程就可能导致乱序访问的出现,也就是这里说的运行时内存乱序访问。这里不再深入讨论整个细节,这是一个比较复杂的问题,有兴趣可以研究http://www.rdrop.com/users/paulmck/scalability/paper/whymb.2010.06.07c.pdf 一文,其详细的分析了整个过程。

现在通过一个例子来说明多 CPU 下内存乱序访问:

  1. // test2.cpp
  2. #include <pthread.h>
  3. #include <assert.h>
  4. // -------------------
  5. int cpu_thread1 = 0;
  6. int cpu_thread2 = 1;
  7. volatile int x, y, r1, r2;
  8. void start()
  9. {
  10. x = y = r1 = r2 = 0;
  11. }
  12. void end()
  13. {
  14. assert(!(r1 == 0 && r2 == 0));
  15. }
  16. void run1()
  17. {
  18. x = 1;
  19. r1 = y;
  20. }
  21. void run2()
  22. {
  23. y = 1;
  24. r2 = x;
  25. }
  26. // -------------------
  27. static pthread_barrier_t barrier_start;
  28. static pthread_barrier_t barrier_end;
  29. static void* thread1(void*)
  30. {
  31. while (1) {
  32. pthread_barrier_wait(&barrier_start);
  33. run1();
  34. pthread_barrier_wait(&barrier_end);
  35. }
  36. return NULL;
  37. }
  38. static void* thread2(void*)
  39. {
  40. while (1) {
  41. pthread_barrier_wait(&barrier_start);
  42. run2();
  43. pthread_barrier_wait(&barrier_end);
  44. }
  45. return NULL;
  46. }
  47. int main()
  48. {
  49. assert(pthread_barrier_init(&barrier_start, NULL, 3) == 0);
  50. assert(pthread_barrier_init(&barrier_end, NULL, 3) == 0);
  51. pthread_t t1;
  52. pthread_t t2;
  53. assert(pthread_create(&t1, NULL, thread1, NULL) == 0);
  54. assert(pthread_create(&t2, NULL, thread2, NULL) == 0);
  55. cpu_set_t cs;
  56. CPU_ZERO(&cs);
  57. CPU_SET(cpu_thread1, &cs);
  58. assert(pthread_setaffinity_np(t1, sizeof(cs), &cs) == 0);
  59. CPU_ZERO(&cs);
  60. CPU_SET(cpu_thread2, &cs);
  61. assert(pthread_setaffinity_np(t2, sizeof(cs), &cs) == 0);
  62. while (1) {
  63. start();
  64. pthread_barrier_wait(&barrier_start);
  65. pthread_barrier_wait(&barrier_end);
  66. end();
  67. }
  68. return 0;
  69. }

这里创建了两个线程来运行测试代码(需要测试的代码将放置在 run 函数中)。我使用了 pthread barrier(区别于本文讨论的 Memory barrier)主要为了让两个子线程能够同时运行它们的 run 函数。此段代码不停的尝试同时运行两个线程的 run 函数,以便得出我们期望的结果。在每次运行 run 函数前会调用一次 start 函数(进行数据初始化),run 运行后会调用一次 end 函数(进行结果检查)。run1 和 run2 两个函数运行在哪个 CPU 上则通过 cpu_thread1 和 cpu_thread2 两个变量控制。
先编译此程序:g++ -lpthread -o test2 test2.cpp(这里未优化,目的是为了避免编译器优化的干扰)。需要注意的是,两个线程运行在两个不同的 CPU 上(CPU 0 和 CPU 1)。只要内存不出现乱序访问,那么 r1 和 r2 不可能同时为 0,因此断言失败表示存在内存乱序访问。编译之后运行此程序,会发现存在一定概率导致断言失败。为了进一步说明问题,我们把 cpu_thread2 的值改为 0,换而言之就是让两个线程跑在同一个 CPU 下,再运行程序发现断言不再失败。

最后,我们使用 CPU Memory barrier 来解决内存乱序访问的问题(X86-64 架构下):

  1. int cpu_thread1 = 0;
  2. int cpu_thread2 = 1;
  3. void run1()
  4. {
  5. x = 1;
  6. __asm__ __volatile__("mfence" ::: "memory");
  7. r1 = y;
  8. }
  9. void run2()
  10. {
  11. y = 1;
  12. __asm__ __volatile__("mfence" ::: "memory");
  13. r2 = x;
  14. }

准备使用 Memory barrier

Memory barrier 常用场合包括:

  1. 实现同步原语(synchronization primitives)
  2. 实现无锁数据结构(lock-free data structures)
  3. 驱动程序

实际的应用程序开发中,开发者可能完全不知道 Memory barrier 就可以开发正确的多线程程序,这主要是因为各种同步机制中已经隐含了 Memory barrier(但和实际的 Memory barrier 有细微差别),这就使得不直接使用 Memory barrier 不会存在任何问题。但是如果你希望编写诸如无锁数据结构,那么 Memory barrier 还是很有用的。

通常来说,在单个 CPU 上,存在依赖的内存访问有序:

  1. Q = P;
  2. D = *Q;

这里内存操作有序。然而在 Alpha CPU 上,存在依赖的内存读取操作不一定有序,需要使用数据依赖 barrier(由于 Alpha 不常见,这里就不详细解释了)。

在 Linux 内核中,除了前面说到的编译器 barrier — barrier() 和 ACCESS_ONCE(),还有 CPU Memory barrier:

  1. 通用 barrier,保证读写操作有序的,mb() 和 smp_mb()
  2. 写操作 barrier,仅保证写操作有序的,wmb() 和 smp_wmb()
  3. 读操作 barrier,仅保证读操作有序的,rmb() 和 smp_rmb()

注意,所有的 CPU Memory barrier(除了数据依赖 barrier 之外)都隐含了编译器 barrier。这里的 smp 开头的 Memory barrier 会根据配置在单处理器上直接使用编译器 barrier,而在 SMP 上才使用 CPU Memory barrier(也就是 mb()、wmb()、rmb(),回忆上面相关内核代码)。

最后需要注意一点的是,CPU Memory barrier 中某些类型的 Memory barrier 需要成对使用,否则会出错,详细来说就是:一个写操作 barrier 需要和读操作(或数据依赖)barrier 一起使用(当然,通用 barrier 也是可以的),反之依然。

Memory barrier 的范例

读内核代码进一步学习 Memory barrier 的使用。
Linux 内核实现的无锁(只有一个读线程和一个写线程时)环形缓冲区 kfifo 就使用到了 Memory barrier,实现源码如下:

  1. /*
  2. * A simple kernel FIFO implementation.
  3. *
  4. * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/log2.h>
  27. /**
  28. * kfifo_init - allocates a new FIFO using a preallocated buffer
  29. * @buffer: the preallocated buffer to be used.
  30. * @size: the size of the internal buffer, this have to be a power of 2.
  31. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  32. * @lock: the lock to be used to protect the fifo buffer
  33. *
  34. * Do NOT pass the kfifo to kfifo_free() after use! Simply free the
  35. * &struct kfifo with kfree().
  36. */
  37. struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
  38. gfp_t gfp_mask, spinlock_t *lock)
  39. {
  40. struct kfifo *fifo;
  41. /* size must be a power of 2 */
  42. BUG_ON(!is_power_of_2(size));
  43. fifo = kmalloc(sizeof(struct kfifo), gfp_mask);
  44. if (!fifo)
  45. return ERR_PTR(-ENOMEM);
  46. fifo->buffer = buffer;
  47. fifo->size = size;
  48. fifo->in = fifo->out = 0;
  49. fifo->lock = lock;
  50. return fifo;
  51. }
  52. EXPORT_SYMBOL(kfifo_init);
  53. /**
  54. * kfifo_alloc - allocates a new FIFO and its internal buffer
  55. * @size: the size of the internal buffer to be allocated.
  56. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  57. * @lock: the lock to be used to protect the fifo buffer
  58. *
  59. * The size will be rounded-up to a power of 2.
  60. */
  61. struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock)
  62. {
  63. unsigned char *buffer;
  64. struct kfifo *ret;
  65. /*
  66. * round up to the next power of 2, since our 'let the indices
  67. * wrap' technique works only in this case.
  68. */
  69. if (!is_power_of_2(size)) {
  70. BUG_ON(size > 0x80000000);
  71. size = roundup_pow_of_two(size);
  72. }
  73. buffer = kmalloc(size, gfp_mask);
  74. if (!buffer)
  75. return ERR_PTR(-ENOMEM);
  76. ret = kfifo_init(buffer, size, gfp_mask, lock);
  77. if (IS_ERR(ret))
  78. kfree(buffer);
  79. return ret;
  80. }
  81. EXPORT_SYMBOL(kfifo_alloc);
  82. /**
  83. * kfifo_free - frees the FIFO
  84. * @fifo: the fifo to be freed.
  85. */
  86. void kfifo_free(struct kfifo *fifo)
  87. {
  88. kfree(fifo->buffer);
  89. kfree(fifo);
  90. }
  91. EXPORT_SYMBOL(kfifo_free);
  92. /**
  93. * __kfifo_put - puts some data into the FIFO, no locking version
  94. * @fifo: the fifo to be used.
  95. * @buffer: the data to be added.
  96. * @len: the length of the data to be added.
  97. *
  98. * This function copies at most @len bytes from the @buffer into
  99. * the FIFO depending on the free space, and returns the number of
  100. * bytes copied.
  101. *
  102. * Note that with only one concurrent reader and one concurrent
  103. * writer, you don't need extra locking to use these functions.
  104. */
  105. unsigned int __kfifo_put(struct kfifo *fifo,
  106. const unsigned char *buffer, unsigned int len)
  107. {
  108. unsigned int l;
  109. len = min(len, fifo->size - fifo->in + fifo->out);
  110. /*
  111. * Ensure that we sample the fifo->out index -before- we
  112. * start putting bytes into the kfifo.
  113. */
  114. smp_mb();
  115. /* first put the data starting from fifo->in to buffer end */
  116. l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
  117. memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
  118. /* then put the rest (if any) at the beginning of the buffer */
  119. memcpy(fifo->buffer, buffer + l, len - l);
  120. /*
  121. * Ensure that we add the bytes to the kfifo -before-
  122. * we update the fifo->in index.
  123. */
  124. smp_wmb();
  125. fifo->in += len;
  126. return len;
  127. }
  128. EXPORT_SYMBOL(__kfifo_put);
  129. /**
  130. * __kfifo_get - gets some data from the FIFO, no locking version
  131. * @fifo: the fifo to be used.
  132. * @buffer: where the data must be copied.
  133. * @len: the size of the destination buffer.
  134. *
  135. * This function copies at most @len bytes from the FIFO into the
  136. * @buffer and returns the number of copied bytes.
  137. *
  138. * Note that with only one concurrent reader and one concurrent
  139. * writer, you don't need extra locking to use these functions.
  140. */
  141. unsigned int __kfifo_get(struct kfifo *fifo,
  142. unsigned char *buffer, unsigned int len)
  143. {
  144. unsigned int l;
  145. len = min(len, fifo->in - fifo->out);
  146. /*
  147. * Ensure that we sample the fifo->in index -before- we
  148. * start removing bytes from the kfifo.
  149. */
  150. smp_rmb();
  151. /* first get the data from fifo->out until the end of the buffer */
  152. l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
  153. memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
  154. /* then get the rest (if any) from the beginning of the buffer */
  155. memcpy(buffer + l, fifo->buffer, len - l);
  156. /*
  157. * Ensure that we remove the bytes from the kfifo -before-
  158. * we update the fifo->out index.
  159. */
  160. smp_mb();
  161. fifo->out += len;
  162. return len;
  163. }
  164. EXPORT_SYMBOL(__kfifo_get);

为了更好的理解上面的源码,这里顺带说一下此实现使用到的一些和本文主题无关的技巧:

  1. 使用与操作来求取环形缓冲区的下标,相比取余操作来求取下标的做法效率要高不少。使用与操作求取下标的前提是环形缓冲区的大小必须是 2 的 N 次方,换而言之就是说环形缓冲区的大小为一个仅有一个 1 的二进制数,那么 index & (size – 1) 则为求取的下标(这不难理解)
  2. 使用了 in 和 out 两个索引且 in 和 out 是一直递增的(此做法比较巧妙),这样能够避免一些复杂的条件判断(某些实现下,in == out 时还无法区分缓冲区是空还是满)

这里,索引 in 和 out 被两个线程访问。in 和 out 指明了缓冲区中实际数据的边界,也就是 in 和 out 同缓冲区数据存在访问上的顺序关系,由于未使用同步机制,那么保证顺序关系就需要使用到 Memory barrier 了。索引 in 和 out 都分别只被一个线程修改,而被两个线程读取。__kfifo_put 先通过 in 和 out 来确定可以向缓冲区中写入数据量的多少,这时,out 索引应该先被读取后才能真正的将用户 buffer 中的数据写入缓冲区,因此这里使用到了 smp_mb(),对应的,__kfifo_get 也使用 smp_mb() 来确保修改 out 索引之前缓冲区中数据已经被成功读取并写入用户 buffer 中了。对于 in 索引,在 __kfifo_put 中,通过 smp_wmb() 保证先向缓冲区写入数据后才修改 in 索引,由于这里只需要保证写入操作有序,故选用写操作 barrier,在 __kfifo_get 中,通过 smp_rmb() 保证先读取了 in 索引(这时候 in 索引用于确定缓冲区中实际存在多少可读数据)才开始读取缓冲区中数据(并写入用户 buffer 中),由于这里只需要保证读取操作有序,故选用读操作 barrier。

到这里,Memory barrier 就介绍完毕了。

转载于:https://www.cnblogs.com/sky-heaven/p/5019660.html

理解 Memory barrier(内存屏障)【转】相关推荐

  1. 理解 Memory barrier(内存屏障)无锁环形队列

    Memory barrier 简介 程序在运行时内存实际的访问顺序和程序代码编写的访问顺序不一定一致,这就是内存乱序访问.内存乱序访问行为出现的理由是为了提升程序运行时的性能.内存乱序访问主要发生在两 ...

  2. 理解 Memory barrier(内存屏障)

    转自:http://name5566.com/4535.html 参考文献列表: http://en.wikipedia.org/wiki/Memory_barrier http://en.wikip ...

  3. 内存屏障 Memory Barriers

    内存屏障 Memory Barriers 在上一篇文章中我们提到了编译时的内存序重排导致的问题以及解决方法,即添加编译器屏障或处理器屏障指令.这篇文章将探讨内存屏障的语义. 内存屏障的类型 Types ...

  4. 一、barrier指令DSB,DMB,ISB,fence——内存屏障,指令屏障

    最近工作中遇到一个问题,大致描述一下: 我们SOC用的arm cortex m7内核,在设计时设计人员图方便,将SPI controller的寄存器(即原本应该是APB空间)放在了0x60000000 ...

  5. # 内存屏障:骇客的硬件视角(1)

    翻译自内存屏障 那么到底是什么让CPU的设计大师们着了魔,要把内存屏障这个鬼东西强行塞给了毫不知情的多处理器系统的软件开发者? 简单点说,就是因为内存访问顺序的重排会带来更好的性能.同步原语的正确操作 ...

  6. 浅谈内存屏障,C++内存序与内存模型

    本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可. 本作品 (李兆龙 博文, 由 李兆龙 创作),由 李兆龙 确认,转载请注明版权. 文章目录 引言 一个有意思的问题 ...

  7. 初步认识Volatile-CPU层面的内存屏障

    什么是内存屏障?从前面的内容基本能有一个初步的猜想,内存屏障就是将 store bufferes中的指令写入到内存,从而使得其他访问同一共享内存的线程的可见性. X86的memory barrier指 ...

  8. 搞懂Linux内存屏障(值得收藏)

    1.编译器 编译器将符合人类思考的逻辑(c代码)翻译成了符合CPU运算规则的汇编指令,编译器了解底层CPU的思维模式,因此,它可以在将c翻译成汇编的时候进行优化(例如内存访问指令的重新排序),让产出的 ...

  9. 十一、kotlin的协程 - 缓存、volatile、内存屏障和cas(四) --- 跑题篇

    本章写着写着就跑题了, 又不舍得删除, 新手看 # 协程的共享变量安全问题简单入门和## volatile 不保证原子性部分代码, 其他可以不看, 太乱, 也没用 协程的共享变量安全问题简单入门 在使 ...

最新文章

  1. python增删改查人名管理_python3字典列表的增删改查(名片管理系统函数版)
  2. JQuery中ajax的相关方法总结
  3. .NET Core运行时和基础类库性能提升
  4. 面试官系统精讲Java源码及大厂真题 - 01 开篇词:为什么学习本专栏
  5. NSNtification 在多线程中的运用
  6. bzoj 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题(DP)
  7. JEESZ分布式框架--单点登录集成方案
  8. paip.提升用户体验---防止windows假死之CPU 100%解决
  9. javaScript 面向对象与原型
  10. Udemy上Gephi教程笔记2
  11. 冒险岛开服服务端教程自己搭建服务器需要那些东西
  12. Winform UI界面设计例程——侧边框栏折叠
  13. 计算机win32时间问题,电脑无法修改时间并提示Windows找不到文件rundll32.exe怎么办...
  14. 前端怎么加粗字体_【推荐】皮卡丘怎么画?教你如何轻松绘画出可爱的宠物小精灵!...
  15. nodejs 安装模块失败 解决方法
  16. 磁碟机变种简单分析(lsass.exe、smss.exe、dnsq.dll、NetApi000.sys)
  17. “照骗”是如何炼成的?
  18. 电脑输入英文字母间距太大
  19. OpenCV 获取摄像头并显示摄像头视频
  20. docker (九)promethues的服务发现和grafana

热门文章

  1. NYNU_省赛选拔题(10)
  2. DNS resource record的写法
  3. 系统技巧之如何巧妙的整理磁盘碎片
  4. raw socket java_记一次蛋疼的Raw socket发送经历。附:Raw socket编程总结
  5. 编程修养 阅读笔记三
  6. 数据结构之各排序算法
  7. springboot冲突导致的发版失败
  8. Android Lazy url
  9. Android 创世纪 第三天
  10. 联想内部工程师 Vista自学手册