On Sep 5, 2007, at 8:50 PM, Cd Cd wrote:
Shouldn’t the output be all done at once since the code is running in
what would that look like exactly though?
seriously there are more than one reason that cannot happen.
the console being printed to can only allow one thing to write to
it at a time. generally speaking the console is line buffered up to
some limit so, as long as a program writes a chunk of chars less that
that limit the lines will not appear to intermixed. things get more
complicated when programs are not writing to the console, but to file
threads are never run in parallel from the perspective of the
computer: the cpu can only run one program at once. it simply does
very intelligent switching very quickly to make you think it’s doing
more than one thing ;-). with ruby’s threads, which are known as
‘green’ threads, ruby itself does this switching for you. with
‘native’ threads the switching is done by the operating system. in
either case the concept of ‘parallel’ is really from the perspective
of the programmer. this isn’t strictly true as sometimes a program
might be able to write to disk or the network but not need the cpu -
in that case it might do two things at once. the central concept is
basically that a thread is a programming abstraction for
programmers to imagine themselves getting more done. sometimes
it’s a useful one.
now, in a multi-cpu machine this gets even murkier - threads (native
ones not green ones) are actually very close to a whole process and
the operating system may in fact allow to bits of your program to run
on two cpus. in the end you must assume that only one piece of a
program is using a given piece of computer hardware at once, since we
simply cannot defy physics, but there are various approaches and
abstractions that help us let the computer help us, like threads, by
structuring our program such that the operating system might be
able to run two bit s of our code on two bits of hardware at once.
- threads are evil and best understood via meditation - not actual
a @ http://drawohara.com/