线程安全和位域 [英] Thread safety and bit-field

查看:207
本文介绍了线程安全和位域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,位域是编译器相关的,但我没有找到与最新的G ++和Visual C ++ 2010位字段线程安全的文档。

I know that bit-field are compiler dependant, but I haven't find documentation about thread safety on bit-field with the latest g++ and Visual C++ 2010.

难道位域成员的操作都是原子?

Does the operations on a bit-field member are atomic ?

推荐答案

线程安全的不幸的是一个非常的重载的编程术语。

"Thread safe" is unfortunately a very overloaded term in programming.

如果你的意思的原子访问位字段,答案是否定的(至少在我所知道的所有处理器)。您有原子访问32位内存位置在32位机器,但这只意味着你会读或写整个32位值。这并不意味着另一个线程不会做同样的事情。如果你正在寻找停止,你可能要同步。

If you mean atomic access to bit-fields, the answer is no (at least on all processors I'm aware of). You have atomic access to 32bit memory locations on 32bit machines, but that only means you'll read or write a whole 32 bit value. This does not mean another thread won't do the same thing. If you're looking to stop that you likely want synchronization.

如果你的意思的同步以位字段的访问,得到的答案也是否定的,除非你包在一个较高的水平同步原语的访问(这往往是建立在原子操作)。

If you mean synchronized access to bit-fields, the answer is also no, unless you wrap your access in a higher level synchronization primitive (which are often built on atomic operations).

总之,编译器不提供的原子同步访问位字段,而不需要您额外的工作。

In short, the compiler does not provide atomic or synchronized access to bit fields without extra work on your part.

帮助吗?

修改博士丹·格罗斯曼对原子和同步对我UOregon的CS部门页面找到。两个漂亮的演讲

Edit: Dr. Dan Grossman has two nice lectures on atomicity and synchronization I found on UOregon's CS department page.

这篇关于线程安全和位域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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