What are user threads?

930 views Asked by At

What are user threads? Below explanation says they are managed by userspace... Please explain how?

Threads are sometimes implemented in userspace libraries, thus called user threads. The kernel is not aware of them, so they are managed and scheduled in userspace.

3

There are 3 answers

5
Ernest Friedman-Hill On

Every modern server or desktop OS, and all major mobile OSs, have a native thread library these days, so this question is not very relevant anymore. But basically, before this was the case, there were libraries -- most famously, the "Green threads library" -- which implemented cooperatively-multitasking threads as a user library. That "cooperatively multitasking" part is the important part: in general, such a library switches from one thread to another only when the thread calls some method that allows a switch to happen ("sleep", "yield", etc.) A user library generally can't do preemptive time-slicing; that's something that has to be done at the OS level.

0
James On

Symbian OS has an Active Object framework that allows async event handling in a single thread http://en.wikipedia.org/wiki/Active_object_%28Symbian_OS%29

Windows also has Fibres: http://msdn.microsoft.com/en-us/library/ms682661%28v=vs.85%29.aspx

1
Ben On

Kernel threads (also called lightweight process) are handeled by the system. They offer several interesting benefits, the main one being that two threads can be scheduled on two different processors in the hope that this will reduce the execution time of your process.

However threads are often used as a programming model. A typical example is a multi-client webserver that waits for incoming connexion and simultaneously exchange data with its connected clients. In this case the programmer may want to create a lot of threads and switch between them very quickly. System threads are not very adapted to this. The number of kernel threads is limited (to few undreads) and any basic operation (creation destruction switching locking) is costly since it must be executed in kernel space.

The user threads on the other hand, can be implemented using set_jmp() and long_jmp() inside a user library. Since they don't involve the kernel an application can create/destroy and switch between user threads very efficiently.

As Ernest said, user threads are not very common any more, however there exists a hybrid solution that can take advantages of the two worlds.

http://en.wikipedia.org/wiki/Thread_(computer_science)#N:M_.28Hybrid_threading.29