The Performance Edge: More
On Multithreading & Multitasking - How It Works And What
It Does In OS X To Make Computing More Efficient
Sunday,
October 21, 2001
Last week we posted an article
on how Multitasking and Multithreading can better utilize
a computer's processing potential. We asked several questions
about multithreading, such as does it require dual processors
or can multithreading occur if just one processor is present.
Below we post some of the informative and thoughtful responses
we received. We would like to also pose another question,
about making a program multithreaded under Mac OS X; How much
work is it as a percentage of over-all application development?
If you have information in regards to this or any feedback
on the issue of OS X performance please either e-mail
us, or use this link
to post your thoughts on our bulletin board.
Hi there,
I just thought I'd give you some clarifications regarding your article
on multithreading and multitasking in OS X.
First of all, Mac OS 9 does multitasking as well; it's a variant called
cooperative multitasking. The difference is that with preemptive
multitasking (which Unix, BeOS, newer Windows versions...) the kernel
(or more properly, the scheduler, which is part of the kernel) gives CPU
time to a process, and then takes it away a certain amount of time
later. This means that all processes will get some time (it doesn't
guarantee that all processes will get ENOUGH time, because you're still
dividing up a finite pie; too many processes can still bog down any
system).
With cooperative multitasking, there is no scheduler; each process runs
until it hands control over to the next process in the queue. This means
that a poorly behaved program may never hand control over, and can
monopolize the machine.
However, this has some interesting side-effects: for a single purpose
(i.e. crunching SETI data, perhaps?) that doesn't rely on multiple
processes interacting, a cooperatively multitasking system can be faster
than a preemptively multitasking system. This is because there isn't the
overhead of a scheduler. For most uses, though, a preemptively
multitasking system is superior.
If processes are different paths of execution in an operating system,
then threads are different paths of execution in an application. In
other words, threads are to a process what processes are to an entire
system. Just as two different processes (even if non-threaded) can be
run on each of two different processors on a multiprocessor system (thus
gaining some advantage on an MP system even with non-MP-aware apps), two
different threads within a process can each run on a different
processor. This is what it means for an application to be MP-aware.
Note that programming threads is quite tricky, and requires some
sophisticated techniques for making sure the two threads don't step on
each others' memory. Why is this? Because in OS X (like any Unix) while
two processes are separated in memory, and can't access each other's
memory, two threads are NOT separated in memory, and can easily step on
each other and cause hard-to-fix bugs.
Another note - Mac OS 9 also has multithreading; that is, any
application can have several threads. Thus, a multithreaded application
(like Photoshop) can be MP-aware in OS 9 as well. However, thread
programming is not as easy in 9 as in X.
So, is there any advantage to multithreading on a single-processor
system? Yes! Even if there's only one processor, a well-written
multithreaded application can have major advantages. To take a simple
example, look at the Finder in Mac OS 8 and later. You can do multiple
copies at once - that's an example of threading; the 'copy' facility (or
subroutine) is reentrant (can be called several times in parallel) and
is a separate thread (making it possible to do other things in the
Finder while a copy is going on). Compare this to the System 6 Finder,
which was not multithreaded.
Basically, good application design includes taking parts of your program
that may take a lot of CPU time (or may simply need to run for a while
even if they're not CPU-bound, because they're waiting for network
transactions or something), and making them separate threads so they can
go off and work by themselves, without making the entire application
unresponsive. It's just like how a multitasking OS (cooperative or
preemptive) is useful even on a single-processor system :-)
--
Noah Daniels
Systems Engineer
I just wanted to give you a quick, and simple, answer to your question
about multithreading. I'm sure plenty of people have already answered you,
but just in case, I wanted to throw in my 2 cents... :)
Basically, multithreading is breaking a large, complex, program into many
different pieces that can run concurrently, or even independently. An
example would be a web browser that is able to load pages in two windows at
once, or even able to load and display all the graphics on a single page,
simultaneously.
System 7 introduced multithreading, but it was poorly implemented and was
hamstrung by the parent applications' reliance on cooperative scheduling.
Threads could only execute within the parent application, so IE could load
2 pages at once, but if another application took the processor, all the IE
threads would stall until it got the processor back.
Mac OS X introduces finer granularity to multithreading. Now the kernel
itself is multithreaded, and all programs execute as at least one
thread. Even if there is only ONE processor, the multiple threads allow
the system to do many things, in many different programs, at once.
Multithreaded applications (including the kernel) can be divided for
execution among available processors, but the kernel can also move single
threaded applications between processors. So if you have 2 single threaded
programs running, one can run on the first processor while the second
executes on the second processor.
I hope I put that in straight forward enough terms. Thanks for the article.
With so many people spouting buzzwords these days, it's nice to have
someone explain them to the unwashed masses. :)
Chris McCusker
UC Riverside
Computing & Communications
Can multithreading occur if only one processor is present?
Yes. Multithreaded code runs independently of how many processors
are present. (It will run faster on multi-CPU machines though).
Here is the basic model: At any given time there is a collection of
threads and processes "running" on the computer. Threads and
processes are almost the same (the main difference is that a group of
threads can easily share data whereas processes each have their own
separate data space).
Within the OS there is a component called the scheduler. The
scheduler decides how much time each process and each thread will get
to run before switching to the next process or thread. On a machine
with multiple CPUs, the scheduler also decides which CPUs run which
threads or processes. Usually the scheduling is done to balance the
threads and processes across CPUs so that you don't have one CPU
handling tons of work while the other CPUs sit idle. (If there is
only one thread or process that is doing lots of work, though, then
you will have one CPU doing lots of work while the others sit there
doing not much.)
On a single-CPU machine, all the threads and processes have to (of
course) run only on one CPU. The scheduler schedules all of them for
that one CPU, but it still switches among different threads and
processes giving each one its own time slice in which to run.
-Nick
Nicholas Coult, Ph.D.
Assistant Professor, Department of Mathematics, Augsburg College
...the reason dual processors make no difference 90% of the time is simple,
the application in question is not written to be multithreaded. OSX
does allow "load balancing" - ie it can run 2 separate applications at
100% load each on 2 chips, but it cannot make an unthreaded application
multithreaded by magic. The same situation pertains under Windows 2000.
If Apple cannot persuade developers to write multithreaded applications
for OSX, then MP Macs won't have much of a future. Power applications
that benefit from dual chips now include Cinema 4D (raytracing only)
Adobe Premiere (excellent load balancing) Adobe Photoshop (full-on
multithreading) Discreet Combustion (multithreading) Discreet Cleaner 5
(marginal multi-threading) etc.
An application that shows excellent scalability for benchmarking puposes
is the Distributed.net client, dnetc. Visit their website for some very
impressive MP G4 achievements.
--
Adrian CC Thomas
London
Copyright 1996-2007 by Cider Press Publishing LLC all rights reserved.
MacSpeedZone is not authorized, sponsored, or otherwise approved by Apple
Computer. Apple, the Apple logo, Macintosh, iPod, iBook, iMac, eMac, and
PowerBook are registered trademarks of Apple Computer, Inc. Additional
company
and product names may be trademarks or registered trademarks and are hereby
acknowledged.