Search code examples
catomic

Atomic addition operations vs multiple variables with multithreading (in C)


When considering performance as the only factor, for extremely fast addition in a multithreaded context, is it better to use the GCC builtin sync / atomic operations to add to a single variable, or is it more performant to add to a single counter per thread?

For example, if I have 8 threads, where a total count of processed items must be incremented (at an extremely high rate), would it be better to have a single variable and increment it from each thread using the atomic operations, or would it be better to have 8 separate variables, one for each thread, and then aggregate the data from the 8 variables at some interval?


Solution

  • It would most likely be much faster for each thread to do its work separately and then aggregate it at the end. ADD instructions are some of the simplest in the instruction set and run very quickly (~1 clock cycle). The overhead to lock a mutex or similar would be larger than the actual computation. Perhaps more importantly, if it's not shared the counter can reside in a register instead of in main memory which is also significantly faster.

    In general, it's both faster and easier to avoid sharing state unless you have to.