Are increments and decrements (++ and --) atomic on SMP and multi-core?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Are increments and decrements (++ and --) atomic on SMP and multi-core?
I'm writing a simple reference count and garbage collector in C++. Every object has a field called refcount, which gets incremented every time the object is referenced.
My question is: what happens if two threads reference the object at the same time? All it does is call refcount++. Do I need to protect it with a mutex? Does declaring the refcount volatile help?
The deref function is already protected by a mutex, but I want to avoid an expensive lock if I don't need it.
Edit: I tested it, and it is atomic on single-core x86.
Nope: the "++" and "--" are *not* guaranteed to be atomic; you *must* synchronize access whenever you increment or decrement any global object in a multithreaded environment. This is especially true in an SMP environment (either two CPUs, or a single dual-core CPU), but it is also true (in general) for uniprocessors.
It seems possible that the thread could be interrupted in the middle of an increment, but on my single-core AMD Athlon, testval is never greater than 5 or less than 0.
"Register" won't work. If I have 18,000 objects, their refcounts can't all be in registers. The ref() method is inline, but that doesn't stop two threads from running it at the same time.
I did find a solution which works for x86 (and can probably be adapted to work with x86-64):
Code:
#if defined(__i386__) && defined(__GNUC__)
#define atomic_increment(var) __asm__ volatile ("lock incl %0" : "+m" (var))
#define atomic_decrement(var) __asm__ volatile ("lock decl %0" : "+m" (var))
#else
#warning "no atomic increment/decrement available, using slower mutex version instead"
#define atomic_increment(var) do { \
GC::lock(); \
++(var); \
GC::unlock(); \
} while (0)
#define atomic_decrement(var) do { \
GC::lock(); \
--(var); \
GC::unlock(); \
} while (0)
#endif
The semantics for the lock prefix specify that no other processor can access the memory location until the instruction has completed.
Every CPU architecture I'm aware of has some kind of atomic "test and set" assembly instruction, and that's absolutely the highest-performing solution.
In general its safe to modify a global integer variable for example in one (and only one) thread, and let all the other threads read it (and only read it) whenever they want. Although this is not a recommended practice, and it doesn't fit all possible uses.
This is how its done in interrupt service routines on embedded microcontrollers without an OS, don't have a mutex, and its not needed as long as the only one writing the variable is the ISR. Its a similar situation to threads in an OS.
There's one other problem with SMP that I've heard about but I'm not an expert on this, is you can have a system where a variable is inc. via one processor and it doesn't make it to the memory space of the other processor until some kind of "memory boundary" condition is met. I believe locking/unlocking a mutex does this in some way. Does anyone know if this is true.
These operators are not atomic. There are no guaranteed-atomic operators.
Use the synchronization primitives provided by the operating system. That's what they're there for. Don't try to get "cute." (Voice of painful experience.)
You came up with what appears to me to be an excellent solution. If I ever had to implement my own low-level mutex (and sadly, there are often very good reasons for doing so, even in sophisticated OS's like Linux or Windows), I would be proud to have reached a similar solution.
Here are a few references that might explain further:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.