Are incrementers / decrementers (var++, var–) etc thread safe?

No, incrementing is not thread-safe. Neither are the INC and DEC instructions. They all require a load and a store, and a thread running on another CPU could do its own load or store on the same memory location interleaved between those operations.

Some languages have built-in support for thread synchronization, but it’s usually something you have to ask for, not something you get automatically on every variable. Those that don’t have built-in support usually have access to a library that provides similar functionality.

Leave a Comment