互锁的功能和原子性:仍然感到困惑 [英] Interlocked functions and atomicity: still confused

查看:102
本文介绍了互锁的功能和原子性:仍然感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于原子地读写变量,我仍然仍然感到困惑.因此,对那些曾经试图帮助您解决先前问题的人,表示歉意.

I'm still confused about reading and writing variables atomically. So sorry in advance to those who have tried to help in previous questions.

今天我已经被告知 ,在读写32位数据时不需要任何Interlocked函数调用long值,使我的先前的信念暴露于窗外.实际上,这备份了 MSDN 所说的

I have been told today that there is no need for any Interlocked function call when reading and writing a 32bit long value, which blows my previous beliefs out of the window. Indeed, this backs up what MSDN says

简单读取和写入正确对齐的32位变量是 原子操作.换句话说,您将不会只得到一个 变量的一部分已更新;所有位都在原子中更新 时尚

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

因此,我然后看了VS2017中的atomic_long的作用;它的operator=对具有新值的当前值进行_InterlockedExchange().

So I then looked at what atomic_long in VS2017 does; its operator= does an _InterlockedExchange() on the current value with the new value.

  1. 这与MSDN并不矛盾;如果读/写是原子的,为什么需要此互锁功能?

  1. Doesn't this contradict MSDN; if the read/write is atomic, why is this Interlocked function needed?

在同一MSDN页面上,我们也有

On the same MSDN page, we also have

简单读取和写入正确对齐的64位变量是 在64位Windows上是原子的.读取和写入64位值不是 保证在32位Windows上是原子的. 读写 其他大小的变量不保证在任何情况下都是原子的 平台.

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.

所以我接着进入atomic_bool的'operator=;它执行_InterlockedExchange8调用,其中 MSDN 告诉我仅在Windows 8及更高版本上可用.

So I then step into atomic_bool's 'operator=; it executes a _InterlockedExchange8 call which MSDN tells me is only available on Windows 8 and above.

这似乎备份了第二个MSDN报价.

This seems to back up the second MSDN quote.

如果我使用VS 2005并以Windows XP为目标,我如何确保对布尔变量进行原子读/写?我需要使用互斥锁还是类似的互斥锁?

If I am using VS 2005 and targeting Windows XP, how would I ensure an atomic read/write of a boolean variable? Would I have to use a mutex or similar?

如果读/写操作不是原子操作,则很容易被撕裂;正确吗?

If a read/write operation is not atomic, that leaves it susceptible to being torn; is that correct?

我已阅读此PAQ ,其中说原始类型不是原子类型.同样,这与我在问题中告诉我的内容以及MSDN告诉我的内容相矛盾.谁是正确的?

I have read this PAQ that says primitive types are not atomic. Again this contradicts what I have been told in my questions and what MSDN has told me. Who is correct?

推荐答案

好吧,您引用了文档的摘录,但没有引用紧随其后的句子:

Well, you quoted an excerpt of the documentation, but not the sentence just following it:

但是,不能保证访问是同步的.如果两个线程正在使用同一变量进行读写,则无法确定一个线程是否将在另一个线程执行其写操作之前执行其读取操作.

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.

也就是说,虽然操作是原子的,即保证一个线程可以写入所有4个字节,但在允许另一个字节读取之前,这并不意味着您将获得正确的值和顺序.执行情况未知.

That is, while the operations are atomic, ie a thread is guaranteed to write all 4 bytes, before another one is allowed to read it, this doesn't mean that you will be getting the correct values, and the order of execution is unknown.

互锁函数确保操作以事务"方式执行,即它将是原子操作,并且所有线程/处理器都将获取最后更新的值-否则由于缓存(每次缓存,每个方法都不能保证) CPU/核心可能会在其本地缓存中维护一个副本.互锁功能以及所有同步功能可确保缓存内容已同步.

The Interlocked functions make sure that the operation is carried out in a "transactional" manner, ie it will be atomic and all threads/processors will be getting the last updated values - otherwise this is not guaranteed, because of caching (each CPU/core may maintain a copy in its local cache). The Interlocked functions, as well as all synchronization functions make sure that cache contents are synchronized.

要读取/更新任何共享数据,是的,您需要使用互斥锁.对于相同进程的线程,最好使用关键部分,因为它的速度非常快(不可测量的时间跨度,使用它们来确保屏幕输出操作的原子性,所以我可以确认这一点.)

For reading/updating any shared data, yes, you need to employ a mutex. For threads of the same process, a critical section would be preferable, as it's incredibly fast (non-measurable timespans, used them to ensure atomicity in screen-output operations, so i can confirm this).

这篇关于互锁的功能和原子性:仍然感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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