]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/examples/join.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / fiber / examples / join.cpp
1
2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <cstdlib>
8 #include <functional>
9 #include <stdexcept>
10 #include <iostream>
11 #include <string>
12
13 #include <boost/fiber/all.hpp>
14
15 int value1 = 0;
16 int value2 = 0;
17
18 void fn1()
19 {
20 boost::fibers::fiber::id id = boost::this_fiber::get_id();
21 for ( int i = 0; i < 5; ++i)
22 {
23 ++value1;
24 std::cout << "fiber " << id << " fn1: increment value1: " << value1 << std::endl;
25 boost::this_fiber::yield();
26 }
27 std::cout << "fiber " << id << " fn1: returns" << std::endl;
28 }
29
30 void fn2( boost::fibers::fiber & f)
31 {
32 boost::fibers::fiber::id id = boost::this_fiber::get_id();
33 for ( int i = 0; i < 5; ++i)
34 {
35 ++value2;
36 std::cout << "fiber " << id << " fn2: increment value2: " << value2 << std::endl;
37 if ( i == 1)
38 {
39 boost::fibers::fiber::id id = f.get_id();
40 std::cout << "fiber " << id << " fn2: joins fiber " << id << std::endl;
41 f.join();
42 std::cout << "fiber " << id << " fn2: joined fiber " << id << std::endl;
43 }
44 boost::this_fiber::yield();
45 }
46 std::cout << "fiber " << id << " fn2: returns" << std::endl;
47 }
48
49 int main()
50 {
51 try
52 {
53 boost::fibers::fiber f1( fn1);
54 boost::fibers::fiber f2( fn2, std::ref( f1) );
55
56 f2.join();
57
58 std::cout << "done." << std::endl;
59
60 return EXIT_SUCCESS;
61 }
62 catch ( std::exception const& e)
63 { std::cerr << "exception: " << e.what() << std::endl; }
64 catch (...)
65 { std::cerr << "unhandled exception" << std::endl; }
66 return EXIT_FAILURE;
67 }