Boost logo

Boost :

Subject: [boost] [boost::context] reusing stacks
From: Valentin Milea (valentin.milea_at_[hidden])
Date: 2012-11-26 15:51:02


Hi,

I'm working on a tiny coroutine library based on boost::context. For
performance reasons I'd rather keep a pool of stacks instead
allocating/deallocating every time. Is it legal to reuse a stack for a
different context?

    namespace ctx = boost::context;

    ctx::fcontext_t fcm, *fc;

    void f1(intptr_t)
    {
        std::cout << "f1" << std::endl;
        ctx::jump_fcontext(fc, &fcm, 0);
    }

    void f2(intptr_t)
    {
        std::cout << "f2" << std::endl;
        ctx::jump_fcontext(fc, &fcm, 0);
    }

    int main(int argc, char * argv[])
    {
        ctx::guarded_stack_allocator alloc;

        std::size_t size = ctx::guarded_stack_allocator::default_stacksize();
        void *sp = alloc.allocate(size);

        fc = ctx::make_fcontext(sp, size, f1);
        std::cout << "main: jump 1" << std::endl;
        ctx::jump_fcontext(&fcm, fc, 0);

        fc = ctx::make_fcontext(sp, size, f2); // legal?
        std::cout << "main: jump 2" << std::endl;
        ctx::jump_fcontext(&fcm, fc, 0);

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

        return 0;
    }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk