Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux and user level threads

Tags:

linux

How can I create user level threads in Linux. What I understand is that the Pthread library creates kernel level threads. Then how can we create user level threads.

like image 947
user3282758 Avatar asked Sep 18 '26 11:09

user3282758


1 Answers

To implement "green threads" one will need to manipulate the process context to be able to switch between emulated threads of execution. Linux provides a handy API to do so:

  • getcontext/setcontext - functions to extract the current execution context and to set it back.
  • makecontext/swapcontext - functions to create new execution context and to swap between contexts.

Apart from Linux specific (and very flexible) getcontext() and friends, C programming language specifications mandate existence of 2 curious functions, setjmp and longjmp. Those can also be used to implement "green" threads, albeit with somewhat more limited functionality.

Naturally, programming with the aforementioned API is difficult, so libraries were created to simplify the task of "green" thread management (one example being the State Threads library).

like image 103
oakad Avatar answered Sep 21 '26 05:09

oakad