]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/examples/ping_pong.cpp
4ab22bd6c80d4ccef9a64d8994093bbdde3c7d30
5 #include <boost/assert.hpp>
6 #include <boost/intrusive_ptr.hpp>
7 #include <boost/ref.hpp>
8 #include <boost/optional.hpp>
10 #include <boost/fiber/all.hpp>
12 typedef boost::fibers::unbounded_channel
< std::string
> fifo_t
;
15 void ping( fifo_t
& recv_buf
, fifo_t
& send_buf
)
17 boost::fibers::fiber::id
id( boost::this_fiber::get_id() );
19 send_buf
.push( std::string("ping") );
21 std::string value
= recv_buf
.value_pop();
22 std::cout
<< "fiber " << id
<< ": ping received: " << value
<< std::endl
;
25 send_buf
.push( std::string("ping") );
27 value
= recv_buf
.value_pop();
28 std::cout
<< "fiber " << id
<< ": ping received: " << value
<< std::endl
;
31 send_buf
.push( std::string("ping") );
33 value
= recv_buf
.value_pop();
34 std::cout
<< "fiber " << id
<< ": ping received: " << value
<< std::endl
;
40 void pong( fifo_t
& recv_buf
, fifo_t
& send_buf
)
42 boost::fibers::fiber::id
id( boost::this_fiber::get_id() );
44 std::string value
= recv_buf
.value_pop();
45 std::cout
<< "fiber " << id
<< ": pong received: " << value
<< std::endl
;
48 send_buf
.push( std::string("pong") );
50 value
= recv_buf
.value_pop();
51 std::cout
<< "fiber " << id
<< ": pong received: " << value
<< std::endl
;
54 send_buf
.push( std::string("pong") );
56 value
= recv_buf
.value_pop();
57 std::cout
<< "fiber " << id
<< ": pong received: " << value
<< std::endl
;
59 send_buf
.push( std::string("pong") );
71 boost::fibers::fiber
f1( & ping
, boost::ref( buf1
), boost::ref( buf2
) );
72 boost::fibers::fiber
f2( & pong
, boost::ref( buf2
), boost::ref( buf1
) );
78 std::cout
<< "done." << std::endl
;
82 catch ( std::exception
const& e
)
83 { std::cerr
<< "exception: " << e
.what() << std::endl
; }
85 { std::cerr
<< "unhandled exception" << std::endl
; }