C 和 gcc 中原子变量的相等测试
我有一个关于使用 gcc 的原子变量的虚拟问题。 我的机器支持__sync_add_and_fetch
功能;我在线程 A 中使用此调用来设置 my_variable (int) 。
我希望线程 B 读取该共享变量以根据值(例如 20)对其进行测试。 编写以下内容是否正确
if( __sync_bool_compare_and_swap( &my_variable, 20, 20 ) ){
//..Ok! It is 20 so go ahead!
}else{
// wrong: it is not ok.
}
如果在 gcc 中没有错误,当共享变量中存在竞争时,__sync_val_compare_and_swap
可能会失败,但我不知道它会返回什么;它如何与__sync_bool_compare_and_swap
一起使用?
问题是,当线程 A 同时使用 __sync_fetch_and_add 更改值时,会发生什么情况?当 __sync_bool_compare_and_swap 并发运行时,是否始终保证返回该 sum 事件的值?
理想情况下,出于我的目的,我确实需要一个函数来执行原子只读操作,而不是交换操作。 C 或 gcc 有类似的东西吗?
非常感谢
AFG
I have a dummy question about atomic variables using gcc.
My machine supports __sync_add_and_fetch
function; I use this call in thread A to set my_variable (int)
.
I want Thread B reading that shared variable to test it against a value e.g. 20.
Is it correct to write the following
if( __sync_bool_compare_and_swap( &my_variable, 20, 20 ) ){
//..Ok! It is 20 so go ahead!
}else{
// wrong: it is not ok.
}
If am not wrong in gcc the __sync_val_compare_and_swap
might fail when there is a race in the shared variable, but I don't know what it does return; how does it work with __sync_bool_compare_and_swap
?
Problem is what happens also when at the same time Thread A is changing the value using __sync_fetch_and_add
? Is it always guaranteed that it will return the value of that sum event when __sync_bool_compare_and_swap
is running concurrently?
Ideally speaking and for my purpose, I would really need a function doing exactly an atomic READ only, nor a Swap. Has C or gcc something like this?
Thanks A lot
AFG
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就原子操作而言,它们应该不间断地完成。如果您希望同时运行多个原子操作,则它们的顺序将存在竞争条件,但每个操作都将运行完成。例如,假设
__sync_fetch_and_add
是第一个,它会在__sync_bool_compare_and_swap
执行其比较和交换之前执行提取和添加。您还可以考虑查看其他信号发送方法。您可能会在一个线程中更新的值与让另一个线程检查该值是否准确为 20 之间存在竞争条件。换句话说,它可能会直接更新到 20,而另一个线程却永远不知道这一点。
但如果这些都不太重要,您可以在不使用任何 __sync* 函数的情况下完成所有这些操作,因为只有一个线程正在写入,另一个线程正在读取;无需担心数据损坏。也无需担心变量是否是最新的,因为您有一个涉及该变量的单独竞争条件(值的更新速度比其检查速度快)。
As far as atomic operations go, they are meant to complete without interruption. If you have multiple atomic operations you expect to run concurrently, there will be a race condition concerning their order, but each will be run to completion. For example, say
__sync_fetch_and_add
was first, it would perform both the fetch and the add before the__sync_bool_compare_and_swap
would perform its compare and swap.You might also consider looking at other methods of signaling. You may have a race condition between the value being updated in one thread and having the other thread check it for the exact value 20. In other words, it could be updated right passed 20 with the other thread never knowing it.
But if none of this is too important, you could just do all this without any
__sync*
functions since only one thread is writing and the other reading; no need to worry about corrupt data. Also no need to worry about the variable being up to date since you have a separate race condition involving that (value is updated faster than its checked).免责声明:我没有使用过 GCC _sync 操作,但我想它们与 MSVC 中的操作相同。我确实相信这应该有效。原子指令始终作为单个单元执行,以防止竞争条件。现在,假设您关心的变量没有映射到某些物理硬件。
Disclaimer: I haven't used the GCC _sync operations but I would imagine they are the same as the ones in MSVC. I do believe that should work. Atomic instructions are always executed as a single unit to prevent race conditions. Now, this assumes that the variable you are concerned about is not mapped to some physical hardware.