原子操作会阻塞其他线程吗? [英] Will atomic operations block other threads?

查看:177
本文介绍了原子操作会阻塞其他线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使原子与非原子"的概念摆在我的脑海.我的第一个问题是我找不到关于它的现实类比".就像客户/餐厅与原子操作或类似事物之间的关系.

I am trying to make "atomic vs non atomic" concept settled in my mind. My first problem is I could not find "real-life analogy" on that. Like customer/restaurant relationship over atomic operations or something similar.

我还想了解原子操作如何将自己置于线程安全的编程中.

Also I would like to learn about how atomic operations places themselves in thread-safe programming.

在此博客文章中; http://preshing.com/20130618/atomic-vs-non-atomic-操作/ 它被提及为:

In this blog post; http://preshing.com/20130618/atomic-vs-non-atomic-operations/ it is mentioned as:

如果在共享内存中完成操作,则对共享内存执行的操作是原子操作. 相对于其他线程的单步执行. 当原子存储是 在共享变量上执行,没有其他线程可以观察到 修改完成了一半.在原子负载上执行原子加载时 共享变量,它读取单个出现的整个值 这一刻.非原子负载和存储不会使这些 保证.

An operation acting on shared memory is atomic if it completes in a single step relative to other threads. When an atomic store is performed on a shared variable, no other thread can observe the modification half-complete. When an atomic load is performed on a shared variable, it reads the entire value as it appeared at a single moment in time. Non-atomic loads and stores do not make those guarantees.

没有其他线程可以观察到修改的一半完成"是什么意思?

What is the meaning of "no other thread can observe the modification half-complete"?

这意味着线程将等待直到原子操作完成?该线程如何知道该操作是原子的?例如在.NET中,我可以理解是否锁定了对象,并设置了一个标志来阻止其他线程.但是原子呢?其他线程如何知道原子操作和非原子操作之间的区别?

That means thread will wait until atomic operation is done? How that thread know about that operation is atomic? For example in .NET I can understand if you lock the object you set a flag to block other threads. But what about atomic? How other threads know difference between atomic and non-atomic operations?

如果以上声明为真,那么所有原子操作是否都是线程安全的?

Also if above statement is true, do all atomic operations are thread-safe?

推荐答案

让我们澄清一下什么是原子的以及什么是块.原子性意味着该操作要么完全执行且其所有副作用都可见,要么根本不执行.因此,所有其他线程可以在操作之前或之后看到状态.互斥量保护的代码块也是原子的,我们只是不将其称为操作.原子操作是特殊的CPU指令,从概念上讲它类似于由互斥锁保护的常规操作(您知道什么是互斥锁,尽管事实上它是使用原子操作实现的,但我仍将使用它.) CPU具有有限的操作集,可以自动执行,但是由于硬件的支持,它们的运行速度非常快.

Let's clarify a bit what is atomic and what are blocks. Atomicity means that operation either executes fully and all it's side effects are visible, or it does not execute at all. So all other threads can either see state before the operation or after it. Block of code guarded by a mutex is atomic too, we just don't call it an operation. Atomic operations are special CPU instructions which conceptually are similar to usual operation guarded by a mutex (you know what mutex is, so I'll use it, despite the fact that it is implemented using atomic operations). CPU has a limited set of operations which it can execute atomically, but due to hardware support they are very fast.

当我们讨论线程块时,我们通常在会话中涉及互斥体,因为它们所保护的代码可能要花相当长的时间才能执行.因此,我们说线程在互斥体上等待.对于原子操作,情况是相同的,但是它们很快,而且我们通常不在这里延迟,因此不太可能一起听到阻塞"和原子操作"两个词.

When we discuss thread blocks we usually involve mutexes in conversation because code guarded by them can take quite a time to execute. So we say that thread waits on a mutex. For atomic operations situation is the same, but they are fast and we usually don't care for delays here, so it is not that likely to hear words "block" and "atomic operation" together.

那意味着线程将等待直到原子操作完成?

That means thread will wait until atomic operation is done?

是的,它将等待. CPU将限制对该变量所在的内存块的访问,而其他CPU内核将等待.请注意,出于性能原因,仅在原子操作本身之间保留块.允许CPU内核缓存变量以供读取.

Yes it will wait. CPU will restrict access to a block of memory where the variable is located and other CPU cores will wait. Note that for performance reasons that blocks are held only between atomic operations themselves. CPU cores are allowed to cache variables for read.

该线程如何知道该操作是原子的?

How that thread know about that operation is atomic?

使用特殊的CPU指令.只是在您的程序中写明,应该以原子方式执行特定的操作.

Special CPU instructions are used. It is just written in your program that particular operation should be performed in atomic manner.

其他信息:

原子操作还有更多棘手的部分.例如,在现代CPU上,通常所有原始类型的读取和写入都是原子的.但是允许CPU和编译器对它们重新排序.因此,有可能您更改了一些结构,设置了一个标志,告知已更改,但是在结构实际提交给内存之前,CPU会对写入进行重新排序并设置标志.当您使用原子操作时,通常会做一些额外的努力来防止意外的重新排序.如果您想了解更多信息,则应该阅读有关内存障碍的信息.

There are more tricky parts with atomic operations. For example on modern CPUs usually all reads and writes of primitive types are atomic. But CPU and compiler are allowed to reorder them. So it is possible that you change some struct, set a flag that telling that it is changed, but CPU reorders writes and sets flag before the struct is actually committed to memory. When you use atomic operations usually some additional efforts are done to prevent undesired reordering. If you want to know more, you should read about memory barriers.

简单的原子存储和写入不是那么有用.为了最大程度地利用原子操作,您需要更复杂的东西.最常见的是CAS-比较和交换.您可以将变量与一个值进行比较,并且只有在比较成功后才能对其进行更改.

Simple atomic stores and writes are not that useful. To make maximal use of atomic operations you need something more complex. Most common is a CAS - compare and swap. You compare variable with a value and change it only if comparison was successful.

这篇关于原子操作会阻塞其他线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆