2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-01-31 08:12:08 +00:00
Files
fiber/examples/simple.cpp
2012-12-19 19:37:05 +01:00

43 lines
814 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
void fn( std::string const& str, int n)
{
for ( int i = 0; i < n; ++i)
{
std::cout << i << ": " << str << std::endl;
this_stm::yield();
}
}
int main()
{
stm::round_robin ds;
stm::scheduler::replace( & ds);
try
{
stm::fiber s1( boost::bind( fn, "abc", 5) );
stm::fiber s2( boost::bind( fn, "xyz", 7) );
while ( s1 || s2 ) stm::run();
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;
}