0

My question is very simple.

Why Interlocked-Increment in multi-thread is slower than 1-thread?

Is it just because of Cache line bouncing(Cache line contention)?

Or is there another reason?

I'm using Intel i7, visual studio 2012. I tested counting number using Interlocked-Increment function. and test result was that.

1 Thread - 610385971

2 Thread - 497804468

3 Thread - 351516659

4 Thread - 333275249

  • Your question as worded is vague and unanswerable. You haven't shown us your code, which could be slow for other reasons; you haven't told us what platform/hardware architecture you're running on; and you haven't given us any numbers showing the performance you're seeing. Please see the [help center](http://stackoverflow.com/help/how-to-ask) for information on how to ask your question better. – antiduh Dec 22 '16 at 16:27
  • I'm very sorry! I'm using Intel i7, visual studio 2012. I tested counting number using Interlocked-Increment function. and test result was that. 1 Thread - 610385971 2 Thread - 497804468 3 Thread - 351516659 4 Thread - 333275249 in this test, as you see, as thread increase performance is down. I just wonder what reason of performance down. have a nice day! – chfhrqnfRhc Dec 23 '16 at 02:30
  • Put those notes in your original post. – antiduh Dec 26 '16 at 01:46
  • Thank you! I followed your advise! – chfhrqnfRhc Dec 27 '16 at 03:36

1 Answers1

2

If I understand the code from your verbal description correctly, then yes, main reason for performance degradation is competition for same cache line between different cores. I.e., for successful execution of interlocked increment a core must get cache line to E using something like https://en.wikipedia.org/wiki/MESIF_protocol that is require inter-core coordination and so visible slower in comparison to execution of interlocked increment on single core.

  • Thank you so much! I thought there is another main reason for performance degradation! I should study cache more :) – chfhrqnfRhc Dec 26 '16 at 01:05
  • @chfhrqnfRhc: See also http://stackoverflow.com/questions/39393850/can-num-be-atomic-for-int-num for more about the low-level details of how CPUs implement the x86 asm instruction `lock inc [mem]` internally, and why it's atomic but `inc [mem]` isn't (without the x86 `lock` prefix), and how this matters for C atomics. – Peter Cordes Dec 27 '16 at 23:34