2016-06-10 51 views
0

我想實現一個鎖定空閒列表。對於這個項目,我需要和原子比較和交換指令,可以將32位指針與我的'節點'結構進行比較。可以_sync_val_compare_and_swap返回int以外的任何東西嗎?

節點結構如下:

typedef struct node 
{ 
    int data; 
    struct node * next; 
    struct node * backlink; 
}node_lf; 

我使用_sync_val_compare_and_swap()執行比較和交換操作。我的問題是,這個函數可以返回一個非int的值嗎? 這就是我要做的:

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); 
} 

其中cs_arg是另一種結構來保持節點指針和其他記錄信息。

如果還有其他方法來實現原子比較和交換,請建議。

回答

1

我的問題是,該函數是否可以返回int以外的值?

答案是肯定的,__sync_val_compare_and_swap可以用比其他int類型,包括charshortlong long__int128(在x64上)工作。

請注意,您可能需要將非整數類型轉換爲合適大小的整數,以便__sync_val_compare_and_swap與它們一起使用。

相關問題