开发者

C: How can a threaded program run faster than a non-threaded on the same single core?

I have a server and a client program. The server program runs all the time waiting for requests from clients. For the server to respond to each client it takes 5 seconds using the sleep() function.

On the multithreaded version if I invoke two clients at the same time it takes abo开发者_C百科ut 5 seconds for each to get a response. The same test gives 5 secs for the first client and 10 to the second client in the non-multithreaded version. Something to be expected obviously. Though there is some problem. The processor is a single core Athlon!

How is it possible for the multithreaded server module to run faster than the non-threaded when the core on the cpu is only one?


Because you are simulating the "work" by sleeping. So the scheduler need only put one thread to sleep for 5 seconds; afterwards it is free to "service" another thread.

In short, you aren't using the CPU so in theory you could service a lot of clients this way. Now if you were to do something CPU-bound for 5 seconds, you would see the difference.

The same would happen if the "work" would be I/O. Since the CPU isn't actually involved, many many threads appear to work concurrently.


The threading is handled by the OS, and is not directly related to the number of processors you have. While one thread sleeps, other threads still run.

From wiki:

In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system.


For the server to respond to each client it takes 5 seconds using the sleep() function.

Two threads can both be sleeping at the same time. Sleeping doesn't take up a whole core...

If your code were CPU-bound then you wouldn't see a benefit from running with multiple threads. But realistically, even with "real work" instead of sleeping, you can often get a benefit from using threading with a single core - because any time one operation is waiting for IO or something like that, another operation can use the processor.

"Single core" just means "one CPU operation at a time" (assuming there's no hyperthreading) - not "one overall operation at a time".


It's not running faster, it's your sleep function is called sequentially on single threaded application and in parallel on multithreaded.


Sleep doesn't do anything.

Therefore you aren't doing anything for 5 seconds. So each thread is waiting 5 seconds and they won't stop each other from waiting or running while sleeping.

If you had it actually DOING something for 5 seconds ( a really long loop or reading from disk) it would go back to 10 seconds total.


Threads are like people. Suppose you need a hole to be dug, you have one shovel, and it takes two hours. A worker can dig for 1 hour, but then has to sleep for 5 hours. Should you hire one worker or two?

One worker can dig for 1 hour, sleep for 5 hours, dig for 1 hour, sleep for 5 hours, for a total of 12 hours.

With two workers, one can dig for an hour and go to sleep, then the second one can dig for an hour and also go to sleep, while the first one's sleeping. So the total time is 7 hours, before they both wake up and are ready for another job.


Keep in mind that there are resources in use besides the CPU; disk access, memory I/O, peripheral I/O can all be utilized simultaneously but not by a single thread. Multiple threads can use more resources at the same time to accomplish more in less time.

Additionally, the CPU and operating system are designed to compartmentalize "time shares" of the cpu resources... a single thread will never get the processors undivided attention. Having more threads can actually get more CPU use because each will be allocated its own cpu time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜