用於實現協程,它們的實現比setcontext系列更輕量和高效,一般用setcontext做第一次跳轉進入協程棧來避免用匯編寫,後續用setjmp和longjmp做協程間跳轉。可以參考我在github上的示例程式碼,kayaklee/uthread補充一下,正好昨天又拿出來longjmp的程式碼來研究,在某些發行版上(ubuntu8.10以上,centOS貌似還沒這個feature),gcc帶最佳化的情況下,會自動帶上FORTIFY_SOURCE編譯,這樣longjmp在執行時會crash,報錯為“longjmp causes uninitialized stack frame”,修復辦法為為關閉gcc的FORTIFY_SOURCE特性,增加編譯引數“-U_FORTIFY_SOURCE”.gcc手冊原文:NOTE: In Ubuntu 8.10 and later versions, -D_FORTIFY_SOURCE=2 is set by default, and is activated when -O is set to 2 or higher. This enables additional compile-time and run-time checks for several libc functions. To disable, specify either -U_FORTIFY_SOURCE or -D_FORTIFY_SOURCE=0.
用於實現協程,它們的實現比setcontext系列更輕量和高效,一般用setcontext做第一次跳轉進入協程棧來避免用匯編寫,後續用setjmp和longjmp做協程間跳轉。可以參考我在github上的示例程式碼,kayaklee/uthread補充一下,正好昨天又拿出來longjmp的程式碼來研究,在某些發行版上(ubuntu8.10以上,centOS貌似還沒這個feature),gcc帶最佳化的情況下,會自動帶上FORTIFY_SOURCE編譯,這樣longjmp在執行時會crash,報錯為“longjmp causes uninitialized stack frame”,修復辦法為為關閉gcc的FORTIFY_SOURCE特性,增加編譯引數“-U_FORTIFY_SOURCE”.gcc手冊原文:NOTE: In Ubuntu 8.10 and later versions, -D_FORTIFY_SOURCE=2 is set by default, and is activated when -O is set to 2 or higher. This enables additional compile-time and run-time checks for several libc functions. To disable, specify either -U_FORTIFY_SOURCE or -D_FORTIFY_SOURCE=0.