C setjmp.h和ucontext.h,哪个更好?
嗨,我需要从一个地方跳到另一个地方...
但我想知道 setjmp 或 ucontext 哪个更好用,例如:
- setjmp 和 ucontext 是否可移植?
- 使用这些库我的代码是线程安全的吗?
- 为什么要使用一种而不是另一种?
- 哪个又快又安全?
- ...(有人可以回答我忘记放在这里的未来问题吗?)
请提供我要求的更多信息,例如示例或一些文档...
我在网上进行了搜索,但我只在 C 中得到异常处理,如 setjmp 的示例,但我对 ucontex.h 一无所知,我知道它用于多任务,它和 pthread 有什么区别?
多谢。
Hi I'm need to jump from a place to another...
But I would like to know which is better to use, setjmp or ucontext, things like:
- Are setjmp and ucontext portable?
- My code is thread safe using these library?
- Why use one instead another?
- Which is fast and secure?
- ...(Someone please, can answer future question that I forgot to put here?)
Please give a little more information that I'm asking for, like examples or some docs...
I had searching on the web, but I only got exception handling in C like example of setjmp, and I got nothing about ucontex.h, I got that it was used for multitask, what's the difference of it and pthread?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
setjmp
是可移植的(ISO C89 和 C99),而ucontext
(在 SUSv3 中已过时,并从 SUSv4/POSIX 2008 中删除)则不是。然而,ucontext
在规范上要强大得多。实际上,如果您对setjmp
/longjmp
以及信号处理程序和备用信号处理堆栈使用了令人讨厌的 hack,那么您可以使它们与ucontext
一样强大code>,但它们不是“可移植的”。两者都不应该用于多线程。为此,POSIX 线程(pthread 函数)。我这样说有几个原因:
ucontext
已从标准中删除,并且可能在未来的操作系统(甚至某些现有的操作系统)中不受支持?setjmp
is portable (ISO C89 and C99) anducontext
(obsolescent in SUSv3 and removed from SUSv4/POSIX 2008) is not. Howeverucontext
was considerably more powerful in specification. In practice, if you used nasty hacks withsetjmp
/longjmp
and signal handlers and alternate signal handling stacks, you could make these just about as powerful asucontext
, but they were not "portable".Neither should be used for multithreading. For that purpose POSIX threads (pthread functions). I have several reasons for saying this:
ucontext
was removed from the standards and might not be supported in future OS's (or even some present ones?)在可移植性方面,
setjmp()
可移植到所有托管 C 实现;
函数是 POSIX XSI 扩展的一部分 - 这使得setjmp()
显着提高了可移植性。可以以线程安全的方式使用
setjmp()
。在线程程序中使用 ucontext 函数没有多大意义 - 您将使用多个线程而不是多个上下文。如果您想从深度嵌套的函数调用中快速返回,请使用 setjmp()(这就是为什么您发现大多数示例都显示其用于异常处理的原因)。使用 ucontext 函数来实现用户空间线程或协程(或者根本不使用它们)。
“快速和安全”的问题毫无意义。这些实现通常与实际实现的速度一样快,但它们执行不同的功能,因此无法直接比较(
ucontext
函数执行更多工作,因此通常会稍微慢一些)。请注意,
ucontext
函数在 POSIX 的两个最新版本中被列为已过时的函数。通常应该使用 pthreads 线程函数来代替。On the portability matter,
setjmp()
is portable to all hosted C implementations; the<ucontext.h>
functions are part of the XSI extensions to POSIX - this makessetjmp()
significantly more portable.It is possible to use
setjmp()
in a thread-safe manner. It doesn't make much sense to use theucontext
functions in a threaded program - you would use multiple threads rather than multiple contexts.Use
setjmp()
if you want to quickly return from a deeply-nested function call (this is why you find that most examples show its use for exception handling). Use theucontext
functions for implementing user-space threads or coroutines (or don't use them at all).The "fast and secure" question makes no sense. The implementations are typically as fast as it is practical to make them, but they perform different functions so cannot be directly compared (the
ucontext
functions do more work, so will typically be slightly slower).Note that the
ucontext
functions are listed as obsolescent in the two most recent editions of POSIX. The pthreads threading functions should generally be used instead.setjmp/longjmp 仅用于恢复“调用”上下文,因此您只能使用它从子例程链中“快速退出”。不同的用途可能会或可能不会工作,具体取决于系统,但一般来说,这些功能并不是为了做这种事情。所以“ucontext”更好。还可以查看“纤维”(Windows 上的本机)。这里是一篇可能有用的文章的链接:
如何实现实用的光纤调度程序?
再见!
setjmp/longjmp are only intended to restore a "calling" context, so you can use it only to do a "fast exit" from a chain of subroutines. Different uses may or may not work depending on the system, but in general these functions are not intended to do this kind of things. So "ucontext" is better. Also have a look to "fibers" (native on Windows). Here a link to an article that may be helpful:
How to implement a practical fiber scheduler?
Bye!