I have two functions A and B, which I want to run concurrently from a program. Can anybody tell me how this program can be implemented using the UNIX signal, alarm, setjmp, and longjmp functions (and not using POSIX thread library).
Roughly speaking, your alarm handler does
- setjmp() to preserve the context of the 'thread' you left
- longjmp() to restore the context of the 'thread' you want to resume
And you just keep alternating back and forth between them
Hi,
Please remember that what you are implementing with SETJMP, LONGJMP are called FIBERS. They are USERLAND threads. The kernel still only sees one thread and process.
If you have a MULTI CPU system ( it will only use 1 CPU to run your prcess ).
If one FIBER stalls like waiting for a resource, the whole process will stall. Now maybe your ALARM() mechanism might over come this.
There are PUBLIC DOMAIN FIBER packages you may want to look at. Why reinvent the wheel.
Using POSIX threads you can actually have 2 threads executing independent of each other within a process and can execute on more than 1 CPU of the system on multiple CPU systems at the same time.
If one stalls waiting for a resource the other thread isn't affected.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.