2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-01-19 16:22:11 +00:00
Files
fiber/examples/future.cpp

49 lines
908 B
C++

#include <cstdlib>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/fiber/all.hpp>
namespace stm = boost::fibers;
namespace this_stm = boost::this_fiber;
inline
int fn( std::string const& str, int n)
{
for ( int i = 0; i < n; ++i)
{
std::cout << i << ": " << str << std::endl;
this_stm::yield();
}
return n;
}
void start()
{
stm::packaged_task<int> pt(
boost::bind( fn, "abc", 5) );
stm::unique_future<int> fi=pt.get_future();
stm::fiber( boost::move( pt) );
fi.wait();
std::cout << "fn() returned " << fi.get() << std::endl;
}
int main()
{
try
{
stm::fiber( start).join();
std::cout << "done." << std::endl;
return EXIT_SUCCESS;
}
catch ( std::exception const& e)
{ std::cerr << "exception: " << e.what() << std::endl; }
catch (...)
{ std::cerr << "unhandled exception" << std::endl; }
return EXIT_FAILURE;
}