How to perform atomic operations on Linux that work on x86, arm, GCC and icc?

前端 未结 9 1574
無奈伤痛
無奈伤痛 2020-12-02 11:52

Every Modern OS provides today some atomic operations:

  • Windows has Interlocked* API
  • FreeBSD has
9条回答
  •  生来不讨喜
    2020-12-02 12:34

    I recently did an implementation of such a thing and I was confronted to the same difficulties as you are. My solution was basically the following:

    • try to detect the gcc builtins with the feature macro
    • if not available just implement something like cmpxch with __asm__ for the other architectures (ARM is a bit more complicated than that). Just do that for one possible size, e.g sizeof(int).
    • implement all other functionality on top of that one or two primitives with inline functions

提交回复
热议问题