by pron · 12y ago · 0 comments
Not at all. First, don't confuse preemptive scheduling with time-slice scheduling. All "true" user mode threads are preemptive (Erlang, Go, Quasar). Not all of them employ time-sliced preemption. In fact, when we implemented Quasar on the JVM we had time sharing but then took it out because it didn't gain us anything (other than increased implementation complexity). The reason is that fibers in interactive applications follow a certain pattern that always entails frequent blocking. You are right, though, that some types of computations do not fit well with the user-mode threading model -- long, CPU intensive computations. Those are best left for kernel threads that do time sharing well. Languages that don't give you access to kernel threads might implement time sharing for their lightweight threads in user mode (e.g. Erlang).
Also, erlang doesn't use time slices, it counts "reductions" (essentially function calls, though I don't think it's always exactly one function call). It also penalizes processes more if they send to a mailbox that is already very large.
Erlang is considered truly pre-emptive, because (assuming you aren't writing your own functions in C or something) a function can't loop or use any operators or really do anything without potentially being pre-empted.
Go is generally considered partially pre-emptive, because it only pre-empts on a function call or during a memory allocation. You can write a simple loop that doesn't terminate, and it will never get pre-empted.
Well, in Quasar we started taking the "fully preemptive" route, but we saw that threads fall into two categories: those that block very often, and those that don't. Because the JVM, unlike Erlang and Go, gives you access to kernel threads, too, Quasar will simply warn you if you're using a fiber for a CPU-intensive operation that doesn't block often. Using reductions didn't work out so well because a "forcefully preempted" fiber still wants more CPU, which normally means it's doing something wrong.
Erlang is designed for reliability, not CPU efficiency for CPU-intensive tasks.
It's not a corner case -- it means any call to a C library needs to be carefully vetted so as to not cause an overly long computation or blocking because just a single one will totally ruin your concurrent experience. ALL VM:s using user mode threads/fibers suffer from that problem.
Real kernel threads doesn't have a lot of overhead and are almost always a much better choice.
Yes, and that's the definition of preemptive. There's nothing explicit about it if the VM handles it for you. What you're saying is that it cannot preempt kernel-thread blocking code.
> Real kernel threads doesn't have a lot of overhead and are almost always a much better choice.
Well, as usual, that depends. A kernel thread task-switch is 20-50us, while a fiber task-switch is a couple nanos at most; that's a 1-2 orders of magnitude difference.
Also can you please find a reference that shows (Linux) kernel threads being 1-2 orders slower than user threads?