简体   繁体   中英

How to do multithreading with getcontext() and setcontext()?

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?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM