2
votes

I got an application which can start any number of threads, each thread does the same task : going through a vector which contains 5000 messages and then process each one of them.

between threads, there is no resource competion , no race condition at all. and there are 4 cpu Cores on the box where I run my application. no other process is doing any CPU consuming task at the time when I run my application.

however, the result I got is as below.

if there is only one thread running, it took the thread 0.45 seconds to process those 5000 messages.

if there are 4 threads running, it took each thread around o.55 seconds to process those message, more than 20% increase.

if there are more messages to process, say 150,000 messages, then there is no difference between processing time with running 1 thread or 4 threads.

I don't understand what caused the time increasing while there were 4 threads running, there were 4 CPU cores, enough for 4 threads.

and why there was no time increasing while processing time was longer?

I did my test using Linux 2.6.26. the scheduler has been improved since 2.6.18. I did the same test using 2.6.18 too, the result was worse while there were 4 threads running which proves that the scheduler did was improved.

1
Threads don't magically make your app faster.Cody Gray♦
I understand that. but in my case, each thread should be able to run on different core, and there is no race condition at all. all threads should just run as fast as when there is only thread running, shouldn't they?宇 雷

1 Answers

1
votes

Threads bring most benefits in IO-bound processes. However, given the setup you describe a 20% performance decrease is probably a sign that your threads are running into contention on some library/system calls.