C setjmp.h and ucontext.h, which is better?

Hi, I need to jump from place to place ...

But I would like to know what is better to use, setjmp or ucontext, for example:

  • Are setjmp and ucontext portable?
  • Is my code thread safe using this library?
  • Why use one instead of the other?
  • What is fast and safe?
  • ... (Can someone please answer a future question that I forgot to put here?)

Please give a little more information about which I am asking, for example, examples or some documents ...

I searched on the Internet, but I only have exception handling in C, as an example of setjmp, and I got nothing about ucontex.h, I realized that it was used for multitasking, what is the difference between it and pthread?

Thank you very much.

+4
source share
3 answers

setjmp is portable (ISO C89 and C99) and ucontext (deprecated in SUSv3 and remote since SUSv4 / POSIX 2008) is not. However, ucontext was significantly more powerful in specs. In practice, if you used nasty hacks with setjmp / longjmp and signal handlers and alternating signal stacks, you could make them about as powerful as ucontext , but they were not "portable".

None of them should be used for multithreading. For this purpose, POSIX threads (pthread functions). I have several reasons to say this:

  • If you write threaded code, you can also run it at the same time. We are faced with speed limits for non-parallel computing, and future machines will be more and more parallel, so take advantage of this.
  • ucontext been removed from the standards and may not be supported in future OSs (or even some existing ones?)
  • Scrolling your own threads may not be transparent to the library code that you might want to use. This can lead to library code breaking, which makes reasonable assumptions about concurrency, locking, etc. As long as your multithreading is collaborative rather than based on an asynchronous signal, there may not be as many problems as this, but once you get this deep into intolerable hacks everything can become very fragile.
  • ... and probably a few more reasons that I can’t think about right now. :-)
+5
source

setjmp / longjmp are intended only to restore the "calling" context, so you can only use it to "quickly exit" the chain of routines. Different applications may or may not work depending on the system, but in general, these functions are not intended to perform such actions. So ucontext is better. Also look at the “fibers” (native to Windows). Here's a link to an article that might be useful:

How to implement a practical fiber planner?

Bye!

0
source

Regarding the importance of portability, setjmp() portable for all hosted C implementations; the <ucontext.h> functions are part of the XSI extensions for POSIX - this greatly simplifies setjmp() .

You can use setjmp() in thread safe mode. It makes no sense to use ucontext functions in a streaming program - you will use multiple threads, not multiple contexts.

Use setjmp() if you want to quickly return from a deeply nested function call (which is why you will find that most examples show its use for exception handling). Use ucontext functions to implement user space threads or coroutines (or do not use them at all).

A “quick and safe” question does not make sense. Implementations are usually as fast as practical to implement them, but they perform different functions, so they cannot be compared directly ( ucontext functions do more work, so they will usually be a bit slower).

Note that ucontext functions ucontext listed as deprecated in the last two releases of POSIX. Instead, use the pthreads streaming functions.

0
source

Source: https://habr.com/ru/post/1346536/


All Articles