Is there anything to replace the <ucontext.h> functions?
Custom stream functions in <ucontext.h> are deprecated because they use the deprecated C function (they use the function declaration with empty parentheses for the argument ).
Do they have a standard replacement? I don’t feel that full threads are good at implementing joint threads.
If you really want to do something like ucontext.h functions, I would continue to use them. Everything else will be less portable. Marking their obsolescence in POSIX seems to have been a terrible mistake of pedantry by someone on the committee. POSIX itself requires that pointers to pointers and data pointers be the same size, and function pointers appear cast in void * , and C itself requires dropping between types of function pointers and vice versa to be safe in both directions, so there are many ways this question could be solved.
There is one real problem, that the conversion of int argc, ... , passed to makecontext to the form for the transition to the function, cannot be performed without significant help from the compiler, unless the calling convention for variative and non-invariant functions occurs the same (and even then it is rather doubtful whether this can be done reliably). However, this problem could be solved simply by reducing the use of makecontext in any form other than makecontext(ucp, func, 1, (void *)arg); .
Perhaps the best question is why you think ucontext.h functions are the best way to handle streams. If you want to go with them, I could suggest writing a shell interface that can be implemented either using ucontext.h or using pthreads, and then compare performance and bloat. It will also have the advantage that if future systems refuse to support ucontext.h , you can just switch to compiling with the pthread-based implementation and everything will just work. (By that time, bloating might be less important, the advantage of multi-core / SMP is likely to be huge, and hopefully the implementation of pthread will be less bloated.)
Edit (based on OP request): To implement “collaborative threading” with pthreads, you need condition variables. Here you will find a decent pthreads tutorial with information on their use:
https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables
Your cooperative multitasking primitive "hand off execution to thread X" will look something like this:
self->flag = 0; other_thread->flag = 1; pthread_mutex_lock(other_thread->mutex); pthread_cond_signal(other_thread->cond); pthread_mutex_unlock(other_thread->mutex); pthread_mutex_lock(self->mutex); while (!self->flag) pthread_cond_wait(self->cond, self->mutex); pthread_mutex_unlock(self->mutex); I hope I understand everything; at least the general idea is true. If someone sees errors, please comment so that I can fix it. The half lock ( other_thread mutex) is probably completely unnecessary with this use, so you could make the mutex a local variable in the task_switch function. All you really do is use pthread_cond_wait and pthread_cond_signal as the "go to sleep" and "wake up other threads" primitives.
What is the Boost.Context library Boost.Context , which was recently adopted and needs only to be combined into the official version of Boost. Boost.Context addresses the same use cases as the POSIX ucontext family: with a low level of service interactions. The author tried to solve performance problems.
No, there is no standard replacement for them.
You have options
- continue to use
<ucontext.h>, although they contain deprecated C. - switching to pthreads
- write your own co-stream library
- use an existing (and possibly not so portable) co-thread library like http://swtch.com/libtask/ , although many of these libraries are implemented on top of ucontext.h
Open Group Core Specifications, Issue 6 IEEE Std 1003.1, 2004 Edition
Shows makecontext () and swapcontext () with the same deprecated syntax. I have not seen anything more recent.