]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/fiber/examples/join.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / fiber / examples / join.cpp
CommitLineData
b32b8144
FG
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
7c673cae
FG
7#include <cstdlib>
8#include <functional>
9#include <stdexcept>
10#include <iostream>
11#include <string>
12
13#include <boost/fiber/all.hpp>
14
15int value1 = 0;
16int value2 = 0;
17
18void fn1()
19{
92f5a8d4 20 boost::fibers::fiber::id this_id = boost::this_fiber::get_id();
7c673cae
FG
21 for ( int i = 0; i < 5; ++i)
22 {
23 ++value1;
92f5a8d4 24 std::cout << "fiber " << this_id << " fn1: increment value1: " << value1 << std::endl;
7c673cae
FG
25 boost::this_fiber::yield();
26 }
92f5a8d4 27 std::cout << "fiber " << this_id << " fn1: returns" << std::endl;
7c673cae
FG
28}
29
30void fn2( boost::fibers::fiber & f)
31{
92f5a8d4 32 boost::fibers::fiber::id this_id = boost::this_fiber::get_id();
7c673cae
FG
33 for ( int i = 0; i < 5; ++i)
34 {
35 ++value2;
92f5a8d4 36 std::cout << "fiber " << this_id << " fn2: increment value2: " << value2 << std::endl;
7c673cae
FG
37 if ( i == 1)
38 {
39 boost::fibers::fiber::id id = f.get_id();
92f5a8d4 40 std::cout << "fiber " << this_id << " fn2: joins fiber " << id << std::endl;
7c673cae 41 f.join();
92f5a8d4 42 std::cout << "fiber " << this_id << " fn2: joined fiber " << id << std::endl;
7c673cae
FG
43 }
44 boost::this_fiber::yield();
45 }
92f5a8d4 46 std::cout << "fiber " << this_id << " fn2: returns" << std::endl;
7c673cae
FG
47}
48
49int 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}