Esempio n. 1
0
int main() {
    int count = 384;

#if defined(BOOST_USE_SEGMENTED_STACKS)
    std::cout << "using segmented_stack stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
    std::cout << "initial stack size = " << boost::context::segmented_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
    std::cout << "application should not fail" << std::endl;
#else
    std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
    std::cout << "initial stack size = " << boost::context::fixedsize_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
    std::cout << "application might fail" << std::endl;
#endif

    boost::context::execution_context main_ctx(
        boost::context::execution_context::current() );

    boost::context::execution_context bar_ctx(
#if defined(BOOST_USE_SEGMENTED_STACKS)
        boost::context::segmented_stack(),
#else
        boost::context::fixedsize_stack(),
#endif
        [& main_ctx, count](){
            bar( count);
            main_ctx();   
        });
    
    bar_ctx();

    std::cout << "main: done" << std::endl;

    return 0;
}
Esempio n. 2
0
int main() {
    try {
        std::istringstream is("1+1");
        char c;
        bool done=false;
        std::exception_ptr except;

        // create handle to main execution context
        auto main_ctx( boost::context::execution_context::current() );

        // execute parser in new execution context
        boost::context::execution_context parser_ctx(
                boost::context::fixedsize_stack(),
                [&main_ctx,&is,&c,&done,&except](){
                // create parser with callback function
                Parser p( is,
                          [&main_ctx,&c](char ch){
                                c=ch;
                                // resume main execution context
                                main_ctx();
                        });
                    try {
                        // start recursive parsing
                        p.run();
                    } catch ( ... ) {
                        // store other exceptions in exception-pointer
                        except = std::current_exception();
                    }
                    // set termination flag
                    done=true;
                    // resume main execution context
                    main_ctx();
                });

        // user-code pulls parsed data from parser
        // invert control flow
        parser_ctx();
        if ( except) {
            std::rethrow_exception( except);
        }
        while( ! done) {
            printf("Parsed: %c\n",c);
            parser_ctx();
            if ( except) {
                std::rethrow_exception( except);
            }
        }

        std::cout << "main: done" << std::endl;

        return EXIT_SUCCESS;
    } catch ( std::exception const& e) {
        std::cerr << "exception: " << e.what() << std::endl;
    }
    return EXIT_FAILURE;
}
Esempio n. 3
0
int main() {
    std::istringstream is("1+1");
    bool done=false;
    char c;

    // create handle to main execution context
    boost::context::execution_context main_ctx(
        boost::context::execution_context::current() );

    // executes parser in new execution context
    boost::context::execution_context parser_ctx(
        boost::context::fixedsize_stack(),
        [&main_ctx,&is,&c,&done](){
            // create parser with callback function
            Parser p( is,
                      [&main_ctx,&c](char ch){
                          c=ch;
                          // resume main execution context
                          main_ctx.resume();
                      });
            // start recursive parsing
            p.run();
            done=true;
            main_ctx.resume();
        });

    // user-code pulls parsed data from parser
    // invert control flow
    parser_ctx.resume();
    do {
        printf("Parsed: %c\n",c);
        parser_ctx.resume();
    } while( ! done);

    std::cout << "main: done" << std::endl;

    return EXIT_SUCCESS;
}