main() {
int* W = malloc(10);
int uu = 1, i = 3;
__sync_val_compare_and_swap(&W[uu], 1, 2);
}
編譯罰款,但是:
main() {
float* W = malloc(10);
int uu = 1, i = 3;
__sync_val_compare_and_swap(&W[uu], 1.0f, 2.0f);
}
不編譯給我你寫的完全一樣的消息。這表明浮動不支持:
The definition given in the Intel documentation allows only for the use of the types int, long, long long as well as their unsigned counterparts. GCC will allow any integral scalar or pointer type that is 1, 2, 4 or 8 bytes in length.
它看起來像這證實了這一點。
如果你不使用安騰那麼也許
The four non-arithmetic functions (load, store, exchange, and compare_exchange) all have a generic version as well. This generic version works on any data type.
您可以使用__atomic_compare_exchange*
,因爲這些應該在任何類型根據文檔工作。儘管如此,我還沒有嘗試過。
編輯:
main() {
float* W = malloc(10);
float target;
float val = 5.0f;
__atomic_exchange(&W[4], &val, &target, __ATOMIC_RELAXED);
}
^- 這至少編譯。
我不認爲CAS函數支持浮點。無論是2D還是1D陣列都無關緊要,因爲您只能將一個元素傳遞給該函數。 – interjay
其實它支持浮動,我試着浮動*數組,它的工作。 – masab
對不起,我不相信你。該函數不知道你傳入的是一維或二維數組的一部分。 – interjay