如何保证64位的写是原子? [英] How to guarantee 64-bit writes are atomic?

查看:132
本文介绍了如何保证64位的写是原子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当可64位写入保证是原子的,基于x86的Intel平台上的C编程(特别是基于Intel的Mac上运行的MacOSX 10.4采用了英特尔编译器)是什么时候?例如:

When can 64-bit writes be guaranteed to be atomic, when programming in C on an Intel x86-based platform (in particular, an Intel-based Mac running MacOSX 10.4 using the Intel compiler)? For example:

unsigned long long int y;
y = 0xfedcba87654321ULL;
/* ... a bunch of other time-consuming stuff happens... */
y = 0x12345678abcdefULL;

如果另一个线程第一次转让后,检查y的值到y已经执行完毕,我想确保它认为无论是价值0xfedcba87654321或价值0x12345678abcdef,而不是他们的一些融合。我想这样做没有任何锁定,如果可能的话没有任何额外的code。我的希望是,使用64位编译器(64位Intel编译器),当操作系统能够支持64位code(MacOSX的10.4)上,这些64位的写操作是原子的。这是总是正确的?

If another thread is examining the value of y after the first assignment to y has finished executing, I would like to ensure that it sees either the value 0xfedcba87654321 or the value 0x12345678abcdef, and not some blend of them. I would like to do this without any locking, and if possible without any extra code. My hope is that, when using a 64-bit compiler (the 64-bit Intel compiler), on an operating system capable of supporting 64-bit code (MacOSX 10.4), that these 64-bit writes will be atomic. Is this always true?

推荐答案

您最好的选择是避免试图建立自己的系统出原语,而是使用锁定,除非它的真的显示为分析时的一个热点。 (如果你认为你可以巧妙避免锁,不知道。你是不是,这是一般的你,其中包括我和其他人。)你应该在最低限度地使用一个自旋锁,请参阅spinlock(3).不管你做什么,不要尝试实施自己的锁。你会得到它错了。

Your best bet is to avoid trying to build your own system out of primitives, and instead use locking unless it really shows up as a hot spot when profiling. (If you think you can be clever and avoid locks, don't. You aren't. That's the general "you" which includes me and everybody else.) You should at minimum use a spin lock, see spinlock(3). And whatever you do, don't try to implement "your own" locks. You will get it wrong.

最后,你需要使用任何锁定或原子操作操作系统提供。获取这些事情中的完全正确 所有的情况下极其困难即可。通常情况下,它可以包括的东西,如勘误表特定处理器的特定版本的知识。 (哦,那处理器没做高速缓存一致性在正确的时间探听2.0版,它的固定在版本2.0.1但2.0则需要插入 NOP )就在拍打在C变量挥发性关键词几乎总是不够的。

Ultimately, you need to use whatever locking or atomic operations your operating system provides. Getting these sorts of things exactly right in all cases is extremely difficult. Often it can involve knowledge of things like the errata for specific versions of specific processor. ("Oh, version 2.0 of that processor didn't do the cache-coherency snooping at the right time, it's fixed in version 2.0.1 but on 2.0 you need to insert a NOP.") Just slapping a volatile keyword on a variable in C is almost always insufficient.

在Mac OS X上,这意味着你需要使用<上市职能href=\"http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/atomic.3.html\">atomic(3)在32位,64位和指针大小的数量进行真正的原子跨所有的CPU操作。 (使用后者为任何原子操作上的指针,让您自动是64分之32位兼容。)那去是否要做事像原子比较并交换,递增/递减,自旋锁,或堆栈/队列管理。幸运的是,<一个href=\"http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/spinlock.3.html\">spinlock(3), <一href=\"http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/atomic.3.html\">atomic(3),和<一个href=\"http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/barrier.3.html\">barrier(3)功能应正确对所有CPU所有的工作是由Mac OS X的支持。

On Mac OS X, that means you need to use the functions listed in atomic(3) to perform truly atomic-across-all-CPUs operations on 32-bit, 64-bit, and pointer-sized quantities. (Use the latter for any atomic operations on pointers so you're 32/64-bit compatible automatically.) That goes whether you want to do things like atomic compare-and-swap, increment/decrement, spin locking, or stack/queue management. Fortunately the spinlock(3), atomic(3), and barrier(3) functions should all work correctly on all CPUs that are supported by Mac OS X.

这篇关于如何保证64位的写是原子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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