Regarding the importance of portability, setjmp() portable for all hosted C implementations; the <ucontext.h> functions are part of the XSI extensions for POSIX - this greatly simplifies setjmp() .
You can use setjmp() in thread safe mode. It makes no sense to use ucontext functions in a streaming program - you will use multiple threads, not multiple contexts.
Use setjmp() if you want to quickly return from a deeply nested function call (which is why you will find that most examples show its use for exception handling). Use ucontext functions to implement user space threads or coroutines (or do not use them at all).
A “quick and safe” question does not make sense. Implementations are usually as fast as practical to implement them, but they perform different functions, so they cannot be compared directly ( ucontext functions do more work, so they will usually be a bit slower).
Note that ucontext functions ucontext listed as deprecated in the last two releases of POSIX. Instead, use the pthreads streaming functions.
source share