Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:16:07+00:00 2026-06-12T15:16:07+00:00

I found boost has a class called context which is used for context switching,

  • 0

I found boost has a class called context which is used for context switching, right?

I try to Google it but did not found any document or example. I am just wondering if anyone can provide some information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:16:08+00:00Added an answer on June 12, 2026 at 3:16 pm

    Boost::Context is an official part of Boost in version 1.51.0 and up. See http://www.boost.org/doc/libs/1_51_0/libs/context/doc/html/index.html for information about it. Unfortunately the documentation is slightly different than the implementation, and some things have changed in SVN, so you’ll need to read the header files a little bit.

    Here is an example I wrote the other day showing Boost::Context to make simple coroutines using Boost 1.51.0+latest SVN:

    #include <array>
    #include <functional>
    
    #include <boost/context/all.hpp>
    
    class Coroutine {
        public:
        Coroutine() :
            my_context(boost::context::make_fcontext(
                stack.data() + stack.size(),
                stack.size(),
                Coroutine::dispatch
            ))
        {}
        virtual ~Coroutine() {}
    
        void operator()() {
            boost::context::jump_fcontext(&yield_context, my_context, reinterpret_cast<intptr_t>(this));
        }
    
        protected:
        void yield() {
            boost::context::jump_fcontext(my_context, &yield_context, 0);
        }
    
        virtual void call() = 0;
    
        private:
        static void dispatch(intptr_t coroutine_ptr) {
            Coroutine *coroutine = reinterpret_cast<Coroutine *>(coroutine_ptr);
            coroutine->call();
            while (true) coroutine->yield();
        }
    
        private:
        boost::context::fcontext_t *my_context;
        boost::context::fcontext_t yield_context;
        std::array<intptr_t, 64*1024> stack;
    };
    
    struct A : public Coroutine {
        void call() {
            std::cerr << "A went to the store one day.\n";
            yield();
            std::cerr << "A was looking for groceries.\n";
            yield();
            std::cerr << "A finally found what she was looking for.\n";
        }
    };
    
    struct B : public Coroutine {
        void call() {
            std::cerr << "B went to the store one day.\n";
            yield();
            std::cerr << "B was looking for replacement tires.\n";
            yield();
            std::cerr << "B didn't find anything at all.\n";
            yield();
            std::cerr << "B went to another store.\n";
            yield();
            std::cerr << "B got the tires installed there.\n";
        }
    };
    
    struct C : public Coroutine {
        void call() {
            std::cerr << "C went to the store one day.\n";
            yield();
            std::cerr << "C was looking for USB drives.\n";
            yield();
            std::cerr << "C found several with competitive pricing.\n";
            yield();
            std::cerr << "C couldn't decide which to buy, so gave up.\n";
        }
    };
    
    
    int main() {
        std::cerr << "So, this is what happened.\n";
        A a;
        B b;
        C c;
        for (size_t i=0; i<10; ++i) {
            a();
            b();
            c();
        }
        std::cerr << "Then it all was done.\n";
    }
    

    Then compiling and running looks like this:

    $ g++ -std=c++11 -o coroutines coroutines.c++ -lboost_context
    $ ./coroutines
    So, this is what happened.
    A went to the store one day.
    B went to the store one day.
    C went to the store one day.
    A was looking for groceries.
    B was looking for replacement tires.
    C was looking for USB drives.
    A finally found what she was looking for.
    B didn't find anything at all.
    C found several with competitive pricing.
    B went to another store.
    C couldn't decide which to buy, so gave up.
    B got the tires installed there.
    Then it all was done.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a threadpool which doesn't seem to be in boost yet, but I
I have a class, which has a boost::asio::io_service object. I want this object stored
Found related questions but not the exact variant so I am posting a very
I have a C++ class, which has the following methods: class Bar { ...
I have a class that has two functions, both of which take a different
I'm making a simple boost::any -like class for educational purposes, but I can't figure
I found that boost thread overhead has three order of magnitude timing overhead in
code: boost::filesystem::path config_folder(Config::CONFIG_FOLDER_NAME); if( !(boost::filesystem::exists(config_folder))) { std::cout << Network Config Directory not found...\n; std::cout
I have a TcpDevice class which encapsulates a TCP connection, which has an onRemoteDisconnect
I'm currently working on a cross-platform application (Win/OSX/iOS) which has a C++ (with Boost)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.