Hi,
for some reasons, the vectors are slowing down the threads. When i use 1 thread to use threadmethod1(int z), it takes about 8 secs for the program to finish but when i'm using 12 thread, it takes 12 seconds to finish.
Why is this happening and what can i do to make the threads run faster?
The program doesn't slow down if the threads are using the treadmethod2(int i), which is why i think it has something to do with vectors.
I am using visual studio diagnostic tools to measure the time.
When im using 12 threads, it uses about 75% cpu.
Off the top of my head, the function threadmethod2 quite possibly doesn't do anything at all, or does a lot less than you might think. A smart compiler could optimise out the entire thing (in fact, I'm surprised that it takes any time at all - a smart compiler really should optimise out the entire thing).
That aside, is your 12 thread version simply doing twelve times as much work as the one thread version? 12 times the work for a 50% increase in time seems pretty good.
the 12 thread version is just doing threadmethod1 12 times more. but they all are doing the methods at the same time so there shouldnt be any increasement of time right? they arent trying to access the same information and arent blocking each other so i dont understand the increasement in time.
Well, your computer running 100 different processes at the same time is really just an illusion. Your processor can maybe run 4 jobs simultaneously with 4 cores, but it just makes it look like it's actually running them all at the same time.
When it comes to executing your threads, when your threads execute is ultimately at the mercy of the CPU scheduler. The more jobs you schedule, the longer it'll take for the OS to get back to you when your turn runs out. There are a plethora of CPU scheduling algorithms out there. From what I can tell, Windows uses a Round-Robin scheduling algorithm, with a multi-level feedback queue to keep CPU-hungry programs from taking all of the CPU's time.
When you schedule 12 threads, you're shoving 12 more jobs at the CPU. You will experience an increase in performance, but it isn't linearly related to the number of threads you use. Think of multitasking yourself. You might get more done, but it could take you a little longer. But you'll still get more done in that extended period of time than you would if you didn't multitask.
> the 12 thread version is just doing threadmethod1 12 times more.
> but they all are doing the methods at the same time so there shouldnt be any increasement of time right?
Right. Assuming that 12 processors available, no debugger is attached to the process and nothing else of significance is running concurrently.