1 #ifndef ATOMIC_UTILS_H
2 #define ATOMIC_UTILS_H
3
4 #include<cstddef>
5
6 class AtomicUtils
7 {
8 public:
9
10 /**
11 * check if the value at addr is equal to oldval, if so replace it with newval
12 * and return the oldval
13 */
14 static size_t compareAndExchange( volatile size_t* addr, size_t oldval, size_t newval )
15 {
16 size_t ret;
17 __asm__ volatile( "lock cmpxchgl %2, %1\n\t"
18 :"=a"(ret), "+m"(*addr)
19 : "r"(newval), "0"(oldval)
20 : "memory" );
21 return ret;
22 }
23 };
24
25 #endif
In file included from atomics_test.cpp:1: ./atomics.hpp:17:25: error: invalid operand for instruction __asm__ volatile( "lock cmpxchgl %2, %1\n\t" ^ :1:16: note: instantiated into assembly here lock cmpxchgl %rsi, (%rdx) ^~~~~
我完全不知道这个错误。有人可以帮助PLZ? 我使用64位mac book pro和clang。
答案 0 :(得分:3)
错误是您正在使用具有64位值的cmpxchgl(cmpxchgl需要32位值)。尝试使用cmpxchgq for 64-bit。