可以_sync_val_compare_and_swap返回Int类型以外的东西吗? [英] can _sync_val_compare_and_swap return anything other than int?
问题描述
我想实现一个锁空闲列表中。对于这个项目,我需要和原子比较并交换指令,可以在32位指针比较我的'节点'结构。
的节点结构是如下所示:
typedef结构节点
{
int数据;
结构节点*接下来的;
结构节点*反向链接;
} node_lf;
我使用_sync_val_compare_and_swap()进行比较和交换操作。我的问题是,可能这个函数返回Int类型以外的值?
这就是我要做的:
node_lf CS(node_lf *地址,cs_arg * old_val,cs_arg * new_val)
{
node_lf PTR;
PTR =(node_lf)__ sync_val_compare_and_swap(为(int *)的地址,old_val->节点,new_val->节点);
返回(PTR);
}
其中的 cs_arg 的是另一种结构来保持节点指针和其他记录信息。
如果有任何其他的方法来实现原子比较和交换,请建议。
我的问题是,可以在此函数返回其他的值大于INT?
块引用>答案是肯定的,
__ sync_val_compare_and_swap
可以比INT
其他类型,包括工作焦
,短
,长长
和__ int128
(在x64)。请注意,您可能需要投非整数类型合适大小的整数为
__ sync_val_compare_and_swap
来与他们合作。I am trying to implement a lock free list. For this project I need and atomic compare and swap instruction that can compare a 32 bit pointer to my 'node' struct.
The node struct is as follows:
typedef struct node { int data; struct node * next; struct node * backlink; }node_lf;
I am using _sync_val_compare_and_swap() to perform compare and swap operation. My question is, can this function return a value other than int? This is what I am trying to do:
node_lf cs(node_lf * address, cs_arg *old_val, cs_arg *new_val) { node_lf ptr; ptr = (node_lf)__sync_val_compare_and_swap ((int *)address, old_val->node, new_val->node); return (ptr); }
where cs_arg is another struct to hold the node pointer and other bookkeeping information.
If there are any other methods to implement atomic compare and swap, please suggest.
解决方案My question is, can this function return a value other than int?
The answer is yes,
__sync_val_compare_and_swap
can work with types other thanint
, includingchar
,short
,long long
and__int128
(on x64).Note that you may need to cast non-integer types to an appropriately-sized integer for
__sync_val_compare_and_swap
to work with them.这篇关于可以_sync_val_compare_and_swap返回Int类型以外的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!