Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are assignment = and subtraction assignment -= atomic operations in C?

Tags:

c

atomic

int b = 1000;
b -= 20;

Is any of the above an atomic operation? What is an atomic operation in C?

like image 712
Registered User Avatar asked Dec 10 '22 07:12

Registered User


2 Answers

It depends on the implementation. By the standard, nothing is atomic in C. If you need atomic ops you can look at your compiler's builtins.

like image 123
BenjaminB Avatar answered May 22 '23 09:05

BenjaminB


It is architecture/implementation dependent.

If you want atomic operations, I think sig_atomic_t type is standardized by C99, but not sure.

From the GNU LibC docs:

In practice, you can assume that int and other integer types no longer than int are atomic. You can also assume that pointer types are atomic; that is very convenient. Both of these are true on all of the machines that the GNU C library supports, and on all POSIX systems we know of.

like image 34
Vinicius Kamakura Avatar answered May 22 '23 07:05

Vinicius Kamakura