Setjmp in C language standard library

Source: Internet
Author: User

Setjmp in C language standard library
Coroutine refers to the co-operative routines, which was first proposed and implemented by Melvin Conway in 1963. Unlike threads in mainstream programming languages, threads are intrusive components. The thread-implemented system is called a preemptible multi-task system, while the multi-task system implemented by coroutine becomes a collaborative multi-task system. Due to the lack of yield semantics, scheduling, sleep suspension, context switching, and other system overhead are inevitable during the running process. You also need to be careful to use the synchronization mechanism to ensure the normal operation of multithreading. The coroutine running command series are fixed and do not require a synchronization mechanism. switching between coroutines only involves the exchange of control. Compared with the thread, it is very lightweight. However, multiple threads can run at the same time, but only one route can run. In fact, the concept of coroutine is earlier than that of the thread. According to Knuth, "Child routines are special cases of coroutine." A child routine is a sub-function call. In fact, coroutine is a program component like a class function, you can easily create hundreds of thousands of coroutines in one thread, just like calling functions for hundreds of thousands of times. However, the child routine has only one call entry start point, and the return ends. The coroutine entry can be both the start point and continue execution from the previous return point, that is to say, the coroutine can transfer the execution right through yield, and call the other party in symmetric or hierarchical mode, rather than the subordinate call relationship like the routine. Of course, the "special case" of Knuth refers to the coordination process which can also simulate the routine to realize the upper-and lower-level call relationship. This is called asypolicric coroutines ). Setjmp. hsetjmp/longjmp is actually the content in the C standard library, which is defined in <setjmp. h> in the header file, a considerable number of people I know have not heard of C/C ++ for many years, after learning about the features and functions of setjmp, they did not agree that I would not use it; however, have you ever wondered why the standard library will implement a syntax support with such a strange feature? The reason is very simple, that is, to implement coroutine. If you locate yourself as a user of the coroutine from the very beginning, you don't care how it is implemented, or even identify yourself as never using the coroutine, you can skip the following content. Let's first look at the definitions of the setjmp/longjmp functions. Int setjmp (jmp_buf _ Buf); void longjmp (jmp_buf _ Buf, int _ Value); Note: 1. When using setjmp with longjmp, they must have a strict execution sequence, that is, call the setjmp function first, and then call the longjmp function to restore to the previously saved "program execution point ". Otherwise, if you execute the longjmp function before setjmp is called, the execution flow of the program is unpredictable, and it is easy to cause the program to crash and Exit 2. longjmp must be called after setjmp, in addition, longjmp must be within the scope of setjmp. Specifically, setjmp is used in a function to initialize a global label. Once this function has not been returned, longjmp can be called anywhere else to jump to the next statement execution of setjmp. In fact, the setjmp function stores the local environment where the call occurs in a jmp_buf structure. As long as the corresponding memory in the main function is not released (the local memory becomes invalid when the function returns ), when longjmp is called, it can be restored to the place where setjmp is executed according to the saved jmp_buf parameter. To put it bluntly, when using setjmp, the most common mistake is to encapsulate it and it should not be encapsulated in a function. For example, copy the code int try (breakpoint bp) {return setjmp (bp-> jb);} void throw (breakpoint bp) {longjmp (bp-> jb, 1 );} writing code in this way will not cause compilation errors, but it is easy to cause runtime errors, because the setjmp stack is in the try function, the next time longjmp is called, The try function may not be cleared in the stack. Here is a simple example: copy the Code # include <stdio. h> # include <setjmp. h> jmp_buf buf; void second () {printf ("second \ n"); longjmp (buf, 1);} void first () {second (); printf ("first \ n");} int coro_main () {if (! (Setjmp (buf) {first () ;}else {printf ("main \ n") ;}return 0 ;}copy the code output result: in addition, secondmain also has ucontext, which is a widely used non-standard C language coroutine library. As far as I know, ucontext is more widely used. The vast majority of C coroutine libraries on the Internet are also implemented based on the ucontext component. I will study it again next time...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.