]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/examples/ping_pong.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / fiber / examples / ping_pong.cpp
1 #include <cstdlib>
2 #include <iostream>
3 #include <string>
4
5 #include <boost/assert.hpp>
6 #include <boost/intrusive_ptr.hpp>
7 #include <boost/ref.hpp>
8 #include <boost/optional.hpp>
9
10 #include <boost/fiber/all.hpp>
11
12 typedef boost::fibers::unbounded_channel< std::string > fifo_t;
13
14 inline
15 void ping( fifo_t & recv_buf, fifo_t & send_buf)
16 {
17 boost::fibers::fiber::id id( boost::this_fiber::get_id() );
18
19 send_buf.push( std::string("ping") );
20
21 std::string value = recv_buf.value_pop();
22 std::cout << "fiber " << id << ": ping received: " << value << std::endl;
23 value.clear();
24
25 send_buf.push( std::string("ping") );
26
27 value = recv_buf.value_pop();
28 std::cout << "fiber " << id << ": ping received: " << value << std::endl;
29 value.clear();
30
31 send_buf.push( std::string("ping") );
32
33 value = recv_buf.value_pop();
34 std::cout << "fiber " << id << ": ping received: " << value << std::endl;
35
36 send_buf.close();
37 }
38
39 inline
40 void pong( fifo_t & recv_buf, fifo_t & send_buf)
41 {
42 boost::fibers::fiber::id id( boost::this_fiber::get_id() );
43
44 std::string value = recv_buf.value_pop();
45 std::cout << "fiber " << id << ": pong received: " << value << std::endl;
46 value.clear();
47
48 send_buf.push( std::string("pong") );
49
50 value = recv_buf.value_pop();
51 std::cout << "fiber " << id << ": pong received: " << value << std::endl;
52 value.clear();
53
54 send_buf.push( std::string("pong") );
55
56 value = recv_buf.value_pop();
57 std::cout << "fiber " << id << ": pong received: " << value << std::endl;
58
59 send_buf.push( std::string("pong") );
60
61 send_buf.close();
62 }
63
64 int main()
65 {
66 try
67 {
68 {
69 fifo_t buf1, buf2;
70
71 boost::fibers::fiber f1( & ping, boost::ref( buf1), boost::ref( buf2) );
72 boost::fibers::fiber f2( & pong, boost::ref( buf2), boost::ref( buf1) );
73
74 f1.join();
75 f2.join();
76 }
77
78 std::cout << "done." << std::endl;
79
80 return EXIT_SUCCESS;
81 }
82 catch ( std::exception const& e)
83 { std::cerr << "exception: " << e.what() << std::endl; }
84 catch (...)
85 { std::cerr << "unhandled exception" << std::endl; }
86 return EXIT_FAILURE;
87 }