> The costs depend on the particular method Pth uses for the context
> implementation, of course. But all available methods Pth uses are very
> cheap, because they are user-space only methods. Keep also in mind that
> because Pth is a non-preemtive threading implementation, the context
> switching is only performed if really required by the I/O.

        There is no reason a user-space context switch should be any faster than a
kernel-space context switch unless the user-space context switch saves a
kernel call. Measurements under Linux (at least) bear this out.

        In most real world cases, user-space context switches actually result in
additional kernel calls. For example, consider threads blocking in 'read'. A
packet is received.

        The user-space implementation will probably be blocked in poll or select.
The kernel will have to create a reply for the process and copy that reply
into user space. Then poll/select will return -- one context switch. The
user-space code will then have to check the reply, find the right thread,
then do a second context switch. Since the thread blocked in 'read', it will
then have to do another kernel call to do the actual 'read'.

        The kernel space implementation, on the other hand will block in 'read'.
The kernel will simply switch to the process and do the read.

        So the user-space implementation requires one more system call, the same
number of kernel context switches, and the extra user-space context switch.
To be fair, this is a kind of worst-case scenario, but it should be
sufficient to show the point.

        The only reason a user-space context switch will be faster than a
kernel-space context switch is that the kernel-space context switch requires
entering and leaving the kernel. So unless it saves at least one syscall,
there's no net benefit.

        This, of course, assumes the kernel is very smart about context switches
between threads of the same process and has a very efficient scheduler. Not
all OSes are this smart.

        This has been discussed extensively on the Linux Kernel Mailing List.
Anyone who really believes that user-space context switches must be faster
than kernel ones should read the archives. This myth is thoroughly
dispelled. There is no magic user-space can do that the kernel can't.

        By the way, if you're having context switches so often that the difference
matters, your performance is in the toilet anyway. Good design is to keep
your threads running as long as possible.

        DS


______________________________________________________________________
GNU Portable Threads (Pth)            http://www.gnu.org/software/pth/
Development Site                      http://www.ossp.org/pkg/lib/pth/
Distribution Files                          ftp://ftp.gnu.org/gnu/pth/
Distribution Snapshots                 ftp://ftp.ossp.org/pkg/lib/pth/
User Support Mailing List                            [EMAIL PROTECTED]
Automated List Manager (Majordomo)           [EMAIL PROTECTED]

Reply via email to