Interlocked equivalent on Linux

后端 未结 7 1815
暗喜
暗喜 2020-12-10 12:39

In a C++ Linux app, what is the simplest way to get the functionality that the Interlocked functions on Win32 provide? Specifically, a lightweight way to atomically increme

7条回答
  •  悲&欢浪女
    2020-12-10 13:30

    Just few notes to clarify the issue which has nothing to do with Linux.

    RWM (read-modify-write) operations and those that do not execute in a single-step need the hardware-support to execute atomically; among them increments and decrements, fetch_and_add, etc.

    For some architecture (including I386, AMD_64 and IA64) gcc has a built-in support for atomic memory access, therefore no external libray is required. Here you can read some information about the API.

提交回复
热议问题