是否有东西可以替换功能?
中的用户线程函数已被弃用,因为它们使用了已弃用的 C 功能(它们 使用带空括号的函数声明作为参数)。
有标准的替代品吗?我不认为成熟的线程擅长实现协作线程。
The user thread functions in <ucontext.h>
are deprecated because they use a deprecated C feature (they use a function declaration with empty parentheses for an argument).
Is there a standard replacement for them? I don't feel full-fledged threads are good at implementing cooperative threading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确实想做 ucontext.h 函数允许的事情,我会继续使用它们。其他任何东西都会不太便携。在 POSIX 中将它们标记为过时似乎是委员会中某人犯下的一个可怕的迂腐错误。 POSIX 本身要求函数指针和数据指针具有相同的大小,并且函数指针可表示为
void *
,而 C 本身则需要在函数指针类型之间进行强制转换以实现往返安全,所以有很多方法可以解决这个问题。有一个真正的问题,如果没有编译器的大力帮助,则无法将传递到 makecontext 的
int argc, ...
转换为传递给函数的形式,除非可变参数和非可变参数函数的调用约定恰好是相同的(即使如此,是否可以稳健地完成还是相当值得怀疑的)。不过,这个问题本来可以通过不赞成以makecontext(ucp, func, 1, (void *)arg);
以外的任何形式使用makecontext
来解决。也许更好的问题是为什么您认为 ucontext.h 函数是处理线程的最佳方法。如果您确实想使用它们,我可能建议编写一个包装器接口,您可以使用
ucontext.h
实现 或使用 pthreads 实现,然后比较性能和膨胀。这还有一个优点,如果未来的系统放弃对 ucontext.h 的支持,您可以简单地切换到使用基于 pthread 的实现进行编译,一切都会简单地工作。 (到那时,膨胀可能不再那么重要,多核/SMP 的好处可能会很大,并且希望 pthread 实现不会那么臃肿。)编辑(根据 OP 的请求):使用 pthread 实现“协作线程”,需要条件变量。这是一个不错的 pthreads 教程,其中包含有关使用它们的信息:
https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables
你的协作多任务处理原语“将执行移交给线程 X”会像这样:
希望我没问题;至少总体思路是正确的。如果有人看到错误请评论,以便我修复。对于这种用法,一半的锁定(
other_thread
的互斥体)可能是完全不必要的,因此您也许可以将互斥体设置为task_switch
函数中的局部变量。您真正要做的就是使用 pthread_cond_wait 和 pthread_cond_signal 作为“进入睡眠”和“唤醒其他线程”原语。If you really want to do something like what the
ucontext.h
functions allow, I would keep using them. Anything else will be less portable. Marking them obsolescent in POSIX seems to have been a horrible mistake of pedantry by someone on the committee. POSIX itself requires function pointers and data pointers to be the same size and for function pointers to be representable cast tovoid *
, and C itself requires a cast between function pointer types and back to be round-trip safe, so there are many ways this issue could have been solved.There is one real problem, that converting the
int argc, ...
passed intomakecontext
into a form to pass to the function cannot be done without major assistance from the compiler unless the calling convention for variadic and non-variadic functions happens to be the same (and even then it's rather questionable whether it can be done robustly). This problem however could have been solved simply by deprecating the use ofmakecontext
in any form other thanmakecontext(ucp, func, 1, (void *)arg);
.Perhaps a better question though is why you think
ucontext.h
functions are the best way to handle threading. If you do want to go with them, I might suggest writing a wrapper interface that you can implement either withucontext.h
or with pthreads, then comparing the performance and bloat. This will also have the advantage that, should future systems drop support forucontext.h
, you can simply switch to compiling with the pthread-based implementation and everything will simply work. (By then, the bloat might be less important, the benefit of multi-core/SMP will probably be huge, and hopefully pthread implementations will be less bloated.)Edit (based on OP's request): To implement "cooperative threading" with pthreads, you need condition variables. Here's a decent pthreads tutorial with information on using them:
https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables
Your cooperative multitasking primitive of "hand off execution to thread X" would go something like:
Hope I got that all right; at least the general idea is correct. If anyone sees mistakes please comment so I can fix it. Half of the locking (
other_thread
's mutex) is probably entirely unnecessary with this sort of usage, so you could perhaps make the mutex a local variable in thetask_switch
function. All you'd really be doing is usingpthread_cond_wait
andpthread_cond_signal
as "go to sleep" and "wake up other thread" primitives.就其价值而言,有一个
Boost.Context
库最近被接受 并且只需要合并到官方 Boost 版本中。Boost.Context
解决与 POSIXucontext
系列相同的用例:低开销协作上下文切换。作者在性能问题上煞费苦心。For what it's worth, there's a
Boost.Context
library that was recently accepted and needs only to be merged into an official Boost release.Boost.Context
addresses the same use cases as the POSIXucontext
family: low-overhead cooperative context switching. The author has taken pains with performance issues.不,没有标准的替代品。
您可以选择
,即使它们包含过时的 C。No, there is no standard replacement for them.
You options are
<ucontext.h>
even though they contain obsolete C.The Open Group 基本规范第 6 期
IEEE Std 1003.1,2004 版
仍然列出了具有相同已弃用语法的 makecontext() 和 swapcontext()。我没有看到任何更新的东西。
The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition
Still lists makecontext() and swapcontext() with the same deprecated syntax. I have not seen anything more recent.