Fixes #2550.
Showing the changes to src/boost-test.cpp (1 of 5 files). Show all
Unified · Split
| @@ -9,9 +9,9 @@ · 8 unmodified lines | ||
| 9 | 9 | |
| 10 | 10 | boost::thread_specific_ptr<int> ptr; |
| 11 | 11 | |
| 12 | −// https://www.boost.org/doc/libs/1_60_0/libs/context/doc/html/context/context.html | |
| 13 | −#include <boost/context/all.hpp> | |
| 14 | −boost::context::fcontext_t fcm,fc1,fc2; | |
| 12 | +// https://www.boost.org/doc/libs/1_76_0/libs/context/doc/html/context/context.html | |
| 13 | +#include <boost/context/fiber_fcontext.hpp> | |
| 14 | +namespace ctx=boost::context; | |
| 15 | 15 | |
| 16 | 16 | void test_thread() |
| 17 | 17 | { |
| @@ -21,23 +21,6 @@ void test_thread() · 3 unmodified lines | ||
| 21 | 21 | std::cout << "Hello, World! from thread" << std::endl; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | −void f1(intptr_t) | |
| 25 | −{ | |
| 26 | − std::cout<<"f1: entered"<<std::endl; | |
| 27 | − std::cout<<"f1: call jump_fcontext( & fc1, fc2, 0)"<< std::endl; | |
| 28 | − boost::context::jump_fcontext(&fc1,fc2,0); | |
| 29 | − std::cout<<"f1: return"<<std::endl; | |
| 30 | − boost::context::jump_fcontext(&fc1,fcm,0); | |
| 31 | −} | |
| 32 | − | |
| 33 | −void f2(intptr_t) | |
| 34 | −{ | |
| 35 | − std::cout<<"f2: entered"<<std::endl; | |
| 36 | − std::cout<<"f2: call jump_fcontext( & fc2, fc1, 0)"<<std::endl; | |
| 37 | − boost::context::jump_fcontext(&fc2,fc1,0); | |
| 38 | − BOOST_ASSERT(false&&!"f2: never returns"); | |
| 39 | −} | |
| 40 | − | |
| 41 | 24 | int main(int argc, char *argv[]) |
| 42 | 25 | { |
| 43 | 26 | (void)argc; |
| @@ -50,15 +33,28 @@ int main(int argc, char *argv[]) · 6 unmodified lines | ||
| 50 | 33 | boost::thread thrd(test_thread); |
| 51 | 34 | thrd.join(); |
| 52 | 35 | |
| 53 | − std::size_t size(8192); | |
| 54 | − void* sp1(std::malloc(size)); | |
| 55 | − void* sp2(std::malloc(size)); | |
| 56 | − | |
| 57 | − fc1=boost::context::make_fcontext(sp1,size,f1); | |
| 58 | − fc2=boost::context::make_fcontext(sp2,size,f2); | |
| 59 | − | |
| 60 | − std::cout<<"main: call jump_fcontext( & fcm, fc1, 0)"<<std::endl; | |
| 61 | − boost::context::jump_fcontext(&fcm,fc1,0); | |
| 62 | − | |
| 36 | + int data=0; | |
| 37 | + ctx::fiber f1{[&data](ctx::fiber&& f2) { | |
| 38 | + std::cout << "f1: entered first time: " << data << std::endl; | |
| 39 | + data+=1; | |
| 40 | + f2=std::move(f2).resume(); | |
| 41 | + std::cout << "f1: entered second time: " << data << std::endl; | |
| 42 | + data+=1; | |
| 43 | + f2=std::move(f2).resume(); | |
| 44 | + std::cout << "f1: entered third time: " << data << std::endl; | |
| 45 | + return std::move(f2); | |
| 46 | + }}; | |
| 47 | + f1=std::move(f1).resume(); | |
| 48 | + std::cout << "f1: returned first time: " << data << std::endl; | |
| 49 | + data+=1; | |
| 50 | + f1=std::move(f1).resume(); | |
| 51 | + std::cout << "f1: returned second time: " << data << std::endl; | |
| 52 | + data+=1; | |
| 53 | + f1=std::move(f1).resume_with([&data](ctx::fiber&& f2){ | |
| 54 | + std::cout << "f2: entered: " << data << std::endl; | |
| 55 | + data=-1; | |
| 56 | + return std::move(f2); | |
| 57 | + }); | |
| 58 | + std::cout << "f1: returned third time" << std::endl; | |
| 63 | 59 | return 0; |
| 64 | 60 | } |