C setjmp.h和ucontext.h,哪个更好?

发布于 2024-10-30 08:57:35 字数 360 浏览 0 评论 0原文

嗨,我需要从一个地方跳到另一个地方...

但我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-11-06 08:57:35

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) and ucontext (obsolescent in SUSv3 and removed from SUSv4/POSIX 2008) is not. However ucontext was considerably more powerful in specification. In practice, if you used nasty hacks with setjmp/longjmp and signal handlers and alternate signal handling stacks, you could make these just about as powerful as ucontext, 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:

  • If you're writing threaded code, you might as well make it actually run concurrently. We're hitting the speed limits of non-parallel computing and future machines will be more and more parallel, so take advantage of that.
  • ucontext was removed from the standards and might not be supported in future OS's (or even some present ones?)
  • Rolling your own threads cannot be made transparent to library code you might want to use. It might break library code that makes reasonable assumptions about concurrency, locking, etc. As long as your multithreading is cooperative rather than async-signal-based there are probably not too many issues like this, but once you've gotten this deep into nonportable hacks things can get very fragile.
  • ...and probably some more reasons I can't think of right now. :-)
风为裳 2024-11-06 08:57:35

在可移植性方面,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 makes setjmp() significantly more portable.

It is possible to use setjmp() in a thread-safe manner. It doesn't make much sense to use the ucontext 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 the ucontext 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.

月亮坠入山谷 2024-11-06 08:57:35

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文