As far as I know, ucontext
offers something better then setjmp
. But it's deprecated and now removed from the POSIX spec. So why did it appear and why was it removed?
Asked
Active
Viewed 857 times
1 Answers
4
The signature of makecontext
from ucontext.h
is:
void makecontext(ucontext_t *ucp, void (*func)(),
int argc, ...);
Note that func
uses an empty parenthesis as argument, but this is a deprecated feature in standard C:
C11(ISO/IEC 9899:201x) §6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
This is the reason it's deprecated (and somehow unfortunate).
-
The rationale in the linked description of `makecontext()` from the POSIX 2004 standard explains why the code was problematic. It also describes in general term what the alternatives are — using threads. And it points out that very few programs were using the facility. – Jonathan Leffler Dec 26 '13 at 04:08
-
2@JonathanLeffler I found the rationale on *POSIX 2004 Page 743*. The reason it gives is basically the same as what I said and it provides some more detailed explanation. Adding its [link](http://cs3.ist.unomaha.edu/~stanw/POSIX/01309818.pdf) here for OP to check out. – Yu Hao Dec 26 '13 at 05:03
-
1@JonathanLeffler From the book, *There are very few applications today that use the \*context() routines. Those that do use them are almost always using them to implement co-routines.*. Maybe this is not true anymore. People are struggling to get lightweight coroutines back on system programming (e.g. Go, Rust). And unfortunately, I couldn't find any evidence the POSIX threading can provide lightweight coroutines… – eonil Dec 26 '13 at 07:20
-
The interface has been marked obsolescent for almost a decade. People who've chosen to use it during that decade are not dreadfully sensible (because it is marked obsolescent, so it will go away over time). As to alternatives, I've no opinion to offer. I merely echoed what the POSIX rationale said. It also noted that the facilities using threads were not simple. But they were also left with an interface that could not be implemented in C99, so they chose to mark it obsolescent. My comment was intended to be complimentary to the answer; the right link with information at the far end of it. – Jonathan Leffler Dec 26 '13 at 08:01
-
5If someone want's to use ucontext the most likely reason is because they don't want to use threads for a specific application in the first place. Like having a "thread" for every client connection which there can be 1000s of. Anybody saying to just use threads instead is being ignorant. – Hugo Maxwell Oct 03 '16 at 00:55
-
1@HugoMaxwell Bingo. Contexts allow one to have user-mode scheduling. They also allow for true coroutines in C, which can be very useful with certain APIs. – Demi Aug 22 '18 at 02:35