In C is “i+=1;” atomic?

后端 未结 12 2127
天命终不由人
天命终不由人 2020-12-28 12:58

In C, is i+=1; atomic?

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-28 13:48

    Whether the expression is atomic or not depends only on the machine code that the compiler generates, and the CPU architectre that it will run on. Unless the addition can be achieved in one machine instruction, its unlikely to be atomic.

    If you are using Windows then you can use the InterlockedIncrement() API function to do a guaranteed atomic increment. There are similar functions for decrement, etc.

提交回复
热议问题