Ancient_Hacker said:
Righto! But:
(0) Most OS's have more tasks running than they have CPU's. So our hog
task will still get interrupted, especially if it's set to LOW
priority.
Depends. If I was to set a long running job going and then walk away on
some of my boxes that would be the *only* task taking significant
processing time.
(1) It's not going to be an *undetected* problem if the program shuts
off the computer when it thinks things are idle!
I would rather not find that the software is faulty through having a
machine shut down when it should not. Especially if it is not required.
(2) That code was written on one of the first dual CPU platforms, a
dual 200MHz Pentium Pro machine (Still running nicely BTW). First
enhancement was to spin off 1, 2, ... 8 threads each doing the cpu
hogging thing.
So you are using at least two system specific routines. Why not actually
ask the OS for the information in a system specific manner since you
*are* doing system specific things anyway?
> From the results one can usually tell in a portable
manner how many CPU's are in the machine,
No you can't. Unless you can point me to the function in the C standard
or even some other standard that Windows, SCO, AIX, IRIX, Linux and
every other OS that is in current use supports. I'll give you a clue,
there isn't one.
> or at least how many the OS
will give you (on Windows, SetProcessCPUAffinityMask() IIRC.
See, you are having to use a system specific call on Windows. So why not
just use a proper system specific solution that does the job *properly*
instead of doing something that tries to use a little standard C to do a
poor job and then has to keep adding more and more system specific stuff
to get around some of the problems?
You are also adding a lot more complexity over what you originally
proposed as the solution to deal with problems you were *already* aware
of. So why post what it seems you *knew* was at the very least an
incomplete solution with out at least telling the OP it was incomplete
and that s/he would have to use various system specific stuff (setting
priority, counting processors, making sure your code really does test
all processors, synchronising across multiple processors, finding out
how many processors and possibly a few other things that I forget) to
get something that actually might work?
I know for a fact you can find out the *real* information because on the
common systems because there are lots of programs that actually report
it. Something that sounds far easier to me. Hell, on *nix it would be
simpler to use system to run one command with the output redirected to a
file and then read that file, or if you want to avoid the security risks
of using a file use the non-standard popen function to run it, then you
are using exactly *two* things beyond the standard instead of many.