0

我想跨线程恢复增强纤维。不幸的是,我在 boost fiber 文档中找不到任何有用的东西,即使在“在线程之间迁移纤维”段落也是如此。

示例代码:

#include <iostream>
#include <future>
#include <boost/fiber/all.hpp>
#include <chrono>


int main()
{
    boost::fibers::fiber myfiber{[]()
    {
        while(true) { // Long running fiber
            std::cerr << std::this_thread::get_id() << std::endl;
            boost::this_fiber::sleep_for(std::chrono::seconds{1});
        }
    }};
    // myfiber.detach();

    while(true) { // long life app main loop
        std::this_thread::sleep_for(std::chrono::seconds{1});

        auto fut = std::async(std::launch::async, [&myfiber]()
        {
            //***-> myfiber.attach();
            boost::this_fiber::yield(); // resume myfiber from this thread
            // myfiber.detach();
        });
        fut.get();
    }
    myfiber.join();
}

谢谢。

4

0 回答 0