Exemplo n.º 1
0
inline
void fn2( boost::fibers::barrier & b)
{
    boost::fibers::fiber::id id = boost::this_fiber::get_id();
    std::cout << "fiber " << id << ": fn2 entered" << std::endl;

    ++value2;
    std::cout << "fiber " << id << ": incremented value2: " << value2 << std::endl;
    boost::this_fiber::yield();

    ++value2;
    std::cout << "fiber " << id << ": incremented value2: " << value2 << std::endl;
    boost::this_fiber::yield();

    ++value2;
    std::cout << "fiber " << id << ": incremented value2: " << value2 << std::endl;
    boost::this_fiber::yield();

    std::cout << "fiber " << id << ": waits for barrier" << std::endl;
    b.wait();
    std::cout << "fiber " << id << ": passed barrier" << std::endl;

    ++value2;
    std::cout << "fiber " << id << ": incremented value2: " << value2 << std::endl;
    boost::this_fiber::yield();

    std::cout << "fiber " << id << ": fn2 returns" << std::endl;
}
void fn2( boost::fibers::barrier & b) {
    ++value2;
    boost::this_fiber::yield();
    ++value2;
    boost::this_fiber::yield();
    ++value2;
    boost::this_fiber::yield();

    b.wait();

    ++value2;
    boost::this_fiber::yield();
    ++value2;
}
Exemplo n.º 3
0
/*****************************************************************************
*   fiber function per client
*****************************************************************************/
void client( boost::asio::io_service & io_svc, tcp::acceptor & a,
             boost::fibers::barrier& barrier, unsigned iterations) {
    print( tag(), ": echo-client started");
    for (unsigned count = 0; count < iterations; ++count) {
        tcp::resolver resolver( io_svc);
        tcp::resolver::query query( tcp::v4(), "127.0.0.1", "9999");
        tcp::resolver::iterator iterator = resolver.resolve( query);
        tcp::socket s( io_svc);
        boost::asio::connect( s, iterator);
        for (unsigned msg = 0; msg < 1; ++msg) {
            std::ostringstream msgbuf;
            msgbuf << "from " << fiber_names.lookup() << " " << count << "." << msg;
            std::string message(msgbuf.str());
            print( tag(), ": Sending: ", message);
            boost::system::error_code ec;
            boost::asio::async_write(
                    s,
                    boost::asio::buffer( message),
                    boost::fibers::asio::yield[ec]);
            if ( ec == boost::asio::error::eof) {
                return; //connection closed cleanly by peer
            } else if ( ec) {
                throw boost::system::system_error( ec); //some other error
            }
            char reply[max_length];
            size_t reply_length = s.async_read_some(
                    boost::asio::buffer( reply, max_length),
                    boost::fibers::asio::yield[ec]);
            if ( ec == boost::asio::error::eof) {
                return; //connection closed cleanly by peer
            } else if ( ec) {
                throw boost::system::system_error( ec); //some other error
            }
            print( tag(), ": Reply  : ", std::string( reply, reply_length));
        }
    }
    // done with all iterations, wait for rest of client fibers
    if ( barrier.wait()) {
        // exactly one barrier.wait() call returns true
        // we're the lucky one
        a.close();
        print( tag(), ": acceptor stopped");
    }
    print( tag(), ": echo-client stopped");
}