How to implement thread library?
Is writing a code for implementing the thread library a part of kernel code ? Is the function im开发者_如何学Goplementation of pthread_create() et al a part of kernel ?
In Linux, pthread_create()
et al. is implemented as part of the glibc project. It uses the (non-portable, Linux-specific) syscall clone()
. (Linux's fork()
is also implemented in terms of clone()
). Some of the BSDs also have similar syscall called rfork()
.
My understanding is that clone()
or rfork()
will both create a new process, but you can specify a flag that says, "use copy-on-write semantics to give this a different address space". So, if you want fork()
, you specify that flag, but if you want to create a thread, you don't, and you end up with a shared address space.
(edited to provide more detail)
Threads are sometimes implemented purely in user space (then also called "green threads"), but typically in kernel space. The wikipedia article explains it nicely.
精彩评论