Are Perl6 threads preemptive or cooperative?
Last week, I asked whether Perl6 threads support SMP. There were a
handful of responses, mostly to the effect that
- we don't know
- we don't care
- we get whatever the native thread library gives us
This assumes that Perl6 uses the native thread library on each
platform, and begs the question that I was really trying to get at:
Are Perl6 threads preemptive or cooperative?
Here are outlines of the two cases
preemptive
- Perl6 uses the native thread library on each platform
- the interpreter protects all its internal data structures with
critical sections, mutexes, etc.
- we get SMP, if the platform has it
- we get preemptive thread switching, even in XSUBs
cooperative
- Perl6 ignores the native thread library
- the interpreter implements its own threads
- the interpreter calls yield() internally to switch threads
- we don't get SMP, even if it is available
- XSUBs may hog the CPU, unless they return quickly or call yield()
Now here's the trade off
performance implementation
preemptive high hard
cooperative low easy
Given the choice, I'd certainly prefer preemptive threads over
cooperative threads. But no one is handing me a free choice: we have
to write this thing. And I think that implementing preemptive threads
in Perl6 is a Hard Problem. Specific issues include
- variable, weak, or absent native thread support
- synchronization code throughout the interpreter
- deadlock
Preemptive threads may be the Right Thing for Perl6. But we should
understand the problems we are buying into, and we should understand
the alternatives.
- SWM