-4

Using threads can significantly improve the performance due to more 'workers' to perform the job. However, I've taken tests and noticed that it's the first few threads that make the biggest performance impact.

How come the time becomes almost constant for large amount of threads, say 1000 and 2000 amount of threads?

Is it because that there isn't enough work to do so most of them go to sleep waiting for work to pop up?

EDIT: I made a multithreaded finder in C which simply works as the find command in bash. I took some time with different amount of threads (phread_create) and noticed that I get biggest performance impact by just creating a few threads. However, the time is almost the same with 40 threads and 600 threads. Why is that so?

Thanks

Cows42
  • 307
  • 3
  • 12
  • 1
    No, it's because there is not enough workers. Do you have 2000 cores in your cpu? That's why. – Sergio Tulentsev Oct 14 '17 at 18:30
  • 4
    Unless you have hundreds of CPU cores creating over a thousand threads is just wasting system resources. The OS will work more switching between your threads, than the threads themselves have time to run. The more threads you create the less slice of time the threads will get to run. – Some programmer dude Oct 14 '17 at 18:31
  • 1
    This isn't really a meaningful question without more context and specifics, especially given the different possible meanings of 'thread'. – pvg Oct 14 '17 at 18:36
  • Sorry for not being clear, I've edited and added more text which explains what I'm trying to ask. @GSerg – Cows42 Oct 14 '17 at 19:04
  • I wish that SO users who comment/answer such questions would specify 'ready/running' threads when making statements about threading CPU overheads. – Martin James Oct 14 '17 at 21:24

1 Answers1

1

As already mentioned in the comments, there is no need to have significantly more threads than CPU-cores. You will actually get less performance if you have more threads than CPU-cores due to the fact that the kernel will waste CPU-cycles switching threads. The more threads you have, the more time will be wasted switching between them, in general at least, it has to do with the nature of the application as well.

Also, when you have many threads to switch between, you are more likely to cache-trashing. To clarify:

When thread1 executes it has its own data in the CPU cache. But when switched out and thread2 comes in, it will read its data from real memory (takes much longer than cache) and put it in the cache. The same thing happens when thread1 comes back, it will have to read from real memory again. This will statistically happen more often the more threads you have. But again, it depends on the nature of the work that is done, how much data each thread reads in from real memory to cache each time it is executing.

user1048576
  • 330
  • 1
  • 11
  • 'As already mentioned in the comments, there is no need to have significantly more threads than CPU-cores' my system here seems to disagree. Firefox has 78 threads, Skype 56 etc. etc. – Martin James Oct 14 '17 at 21:10
  • @Martin James: I also wrote that it has to do with the nature of the application. But for load-sharing purposes, and that is what the question was about, there is no need to have significantly more threads than cores. On the other hand if you have threads that sleep most of the time, and only wakes up handling a specific event, then it might be feasible to have many more threads – user1048576 Oct 14 '17 at 21:15