x86 spinlock using cmpxchg

后端 未结 3 1621
北海茫月
北海茫月 2020-12-13 11:11

I\'m new to using gcc inline assembly, and was wondering if, on an x86 multi-core machine, a spinlock (without race conditions) could be implemented as (using AT&T synta

3条回答
  •  抹茶落季
    2020-12-13 11:41

    This will put less contention on the memory bus:

    void spin_lock(int *p)
    {
        while(!__sync_bool_compare_and_swap(p, 0, 1)) while(*p);
    }
    

提交回复
热议问题