kerravon said:
Assuming I am running a C program that is doing some cpu-
intensive work such as zip -9, I can understand:
If I have 8 CPUs, then it will make no difference at all
to the zip program, it will only run on one of the CPUs,
although this does allow me to run 8 separate zips
simultaneously, which would be cool on a large site.
That's only true if your compiler has no multi-threading
capability, or if it does but you don't use it. To get
multi-threading going, you have to have a compiler that
supports it, and you have to actually use its multi-threaded
capabilities by starting tasks in their own threads.
A single-threaded program is unlikely to run much faster
just because you run it on a multi-core CPU.
But what I don't understand is the concept of a "core",
as in "dual core".
As I understand it, a "core" is almost an entire CPU, with
it's own ALU, registers, etc; as for exactly what is shared
and what is separate, you'd have to RTFM* for your CPU.
What implications does that have for a C program like
zip? Does it have the ability to look at the instructions
ahead of time and pipeline them or something? Pipelining
is something that has been around for a long time.
Did someone just get the bright idea to call it dual core
instead or what?
No, generally a multi-threading compiler will run one thread
per core. Each thread is defined by an entry function, which
usually consists of a for loop which loops until either the
thread is no-longer needed or the application exits. Any
functions called by the thread function will also be a part
of that thread and run on that thread's core.
Assume the zip in question is written in C89, no fancy
parallelism - at least not inherent in the language itself.
The C language itself neither supports nor thwarts parallel
processing. That's a function of your compiler. RTFM* for
your compiler.
My compiler at work (National Instruments LabWindows/CVI) *does*
support parallel processing. It comes with a multi-threading
library, which I make heavy use of, because the program I'm
working on needs to be doing multiple things in real-time, and
it is not acceptable for one task to have to stop and wait for
another to complete. So I put the different tasks in different
threads.
A multi-threaded compiler such as LabWindows/CVI will first look
for multiple cores to run threads in, and if it finds them, it
will attempt to distribute one thread per core.
If it runs out of cores (or if there is only one), it then shares
multiple threads per core by time-sharing (jumping execution
rapidly back and forth between two threads on the same core).
For example, here's how one program I'm working on launches
a couple of new threads:
// Start communications thread:
CmtScheduleThreadPoolFunction
(
DEFAULT_THREAD_POOL_HANDLE, // thread pool handle
CommThread, // thread function
(void*)0,
&thrdCommunications // thread id
);
// Start background thread:
CmtScheduleThreadPoolFunction
(
DEFAULT_THREAD_POOL_HANDLE, // thread pool handle
BackgroundThread, // thread function
(void*)0,
&thrdBackground // thread id
);
If separate CPU cores are available, those will run in
separate cores; otherwise, they'll be time-shared on
the same core.
Multi-threaded programming does take some getting used to, though.
You need to watch out for booboos such as thread A getting to a
certain point then waiting for thread B to complete, with
thread B also having a point where it stops and waits for
thread A to complete. Ooops! Deadlock.
Also, you need to be aware that multiple thread can access
the same global variables. Say thread A stores "57" in
global variable "int argle" and leaves it there for a while
without changing it. Later, thread A comes back and reads
argle and finds that its value is now "87375629". What
happened??? I'll tell you what happened! Thread B wrote
to it, overwriting what thread A had written to it. Gotta
watch out for that. Only use global variables for things
which absolutely MUST be global, especially when doing
multi-threaded programming.
Footnotes:
*RTFM: "Read the Fine Manual". (Or substitute another word for
"fine" if that suits your fancy better.)