- 论坛徽章:
- 11
|
本帖最后由 zylthinking 于 2013-05-21 00:05 编辑
记得之前版上有几个将动态语言中 yield 等语义用 C 来实现的尝试, 今天看文档, 居然发现 posix 已经在N年前支持了这个语义, 这个应该在 pthread 之前出现的, 看样子, 应该是 pthread 的兴起才导致它的没落, 不过, 即便在现在, 貌似在一定的场所, 也有用处:- #include <string.h>
- #include <ucontext.h>
- #include <stdio.h>
- // 64kB stack
- #define FIBER_STACK 1024*64
- ucontext_t child, parent;
- // The child thread will execute this function
- void threadFunction()
- {
- printf( "Child fiber yielding to parent\n" );
- swapcontext( &child, &parent );
- printf( "Child thread exiting\n" );
- swapcontext( &child, &parent );
- }
- int main()
- {
- // Get the current execution context
- getcontext( &child );
- // Modify the context to a new stack
- child.uc_link = 0;
- child.uc_stack.ss_sp = malloc( FIBER_STACK );
- child.uc_stack.ss_size = FIBER_STACK;
- child.uc_stack.ss_flags = 0;
- if ( child.uc_stack.ss_sp == 0 )
- {
- perror( "malloc: Could not allocate stack" );
- exit( 1 );
- }
- // Create the new context
- printf( "Creating child fiber\n" );
- makecontext( &child, &threadFunction, 0 );
- // Execute the child context
- printf( "Switching to child fiber\n" );
- swapcontext( &parent, &child );
- printf( "Switching to child fiber again\n" );
- swapcontext( &parent, &child );
- // Free the stack
- free( child.uc_stack.ss_sp );
- printf( "Child fiber returned and stack freed\n" );
- return 0;
复制代码 |
|