0

I have created a C++ windows console application using (HANDLE)_beginthreadex() in which 1000 worker threads are controlled using x handler threads.

Initially I thought that 1000 handler threads would result in the quickest time but after testing I have found that using 100 handler threads results in quickest time. The testing was carried out on a quad core intel i7 processor (supports hyperthreading)

I'm not sure what to write for my reasoning of why that number of threads results in the best performance. As my processor can only handle 8 threads simultaneously, I would have thought 8 would have been the best performance.

I'm writing a small report on the application and have to identify the number of threads that results in the best performance and explain why this is the case.

Split
  • 259
  • 2
  • 5
  • 11
  • It's worth noting in this answer http://stackoverflow.com/questions/12124586/make-two-thread-in-two-different-core-in-c, that you haven't tested how it is when a % of those threads were *actually* spread over different cores... – Moo-Juice Apr 23 '13 at 20:19

1 Answers1

3

You want to have 8 active threads at a time ideally. There are many reasons why 8 might not be the ideal number in general, but it's a good bet the work in your threads is not CPU limited. If you have too few threads in that case, you would be wasting time, while too many would, of course, cause undue contention -- and possibly be causing too many context switches.

Joel
  • 5,618
  • 1
  • 20
  • 19
  • 1
    IOW, profile the app to see where the threads are waiting for non-CPU-work to complete. (+1) – usr Apr 23 '13 at 20:27
  • In my application, I start my 1000 worker threads first then my handler threads second, does it matter the order they are started? Would the program run better if the handler threads started first and were ready to handle the worker threads when they have finished their task? – Split Apr 23 '13 at 20:47
  • Once you get past some stuttering the OS scheduler will dominate, so it probably doesn't matter after a few seconds. Of course the only way to know for sure would be to profile. – Joel Apr 24 '13 at 02:27