If I understand: multi-threading permits to have multiple threads of execution. So during a thread do something, another thread do something else without waiting the other thread. But when I use getcontext() and setcontext(), I have the impression the thread waits the other thread to continue its execution. Could you give me an example of how to use getcontext() and setcontext() to do multi-threading please?
Asked
Active
Viewed 630 times
0
-
1You should show your [mcve] and give us the full context: what compiler, and runtime environment, as well as any special libraries you may be using. Note that the C language is not intrinsically multi-threaded, but depends on other APIs for this. This question may, in fact, be a duplicate: https://stackoverflow.com/q/7774778/1531971 – Feb 04 '19 at 16:31
-
This question has some pretty broad implications, but fundamentally `GetThreadContext` and `SetThreadContext` work on a single, specific thread, so those by themselves do not buy you multi-threading that you didn't previously have - simulated concurrency, perhaps, but not multi-threading. – 500 - Internal Server Error Feb 04 '19 at 19:10
1 Answers
0
setcontext(), getcontext(), makecontext() and swapcontext() provide the ability to setup multiple lightweight threads of execution. They are more commonly called coroutines. In GLIBC/Linux environment, they are set up inside a process. They are not controlled by the OS scheduler. It is the responsibility of the programmer which implements them to switch from one to the other. As we run in the context of a process, we are not fully parallel but simulating parallelism: this is pseudo-parallelism.
An implementation is shown in the "examples" section of the manual of makecontext().

Rachid K.
- 4,490
- 3
- 11
- 30