On 6/15/2012 6:33 PM, Aaron W. Hsu wrote:
> I am considering moving one of my projects from C to Java, but I am
> hoping to find a high-performance threading implementation, or something
> along the lines of libqthread, which offers Fill-Empty bit blocking and
> good cooperative lightweight threading as a library.
>[...]
Others have covered the essentials, but I'd like to raise an
eyebrow at the word "cooperative." In the usual argot, "cooperative
threading" refers to schemes where a thread runs until it chooses
to relinguish the CPU, voluntarily. This may be contrasted to
"preemptive" schemes, where an external scheduler decides which
threads will run on which CPU's and when, and may choose to evict
a running thread or start a stalled thread at any arbitrary moment.
The "collegial" nature of cooperative threading offers a degree
of calmness, and some freedom from worry about critical sections
(if you're on a uniprocessor, you may be able to avoid taking an
explicit lock to guard a critical section: Just don't yield the CPU
while you're in it). But this cooperation is fragile in the extreme,
and becomes almost completely untenable once you start assembling an
application out of classes from disparate sources. Unless you're
writing for a very tightly controlled environment where you have
audited all the components or developed them yourself -- embedded
software in a Mars rover, say -- I think "cooperative multithreading"
is too brittle for most practical applications.
If that's not what you meant by "cooperative," well, never mind.
But if it is, my advice is Don't Do That.
--
Eric Sosman
d