summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/fiber/examples/join.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/fiber/examples/join.cpp
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/boost/libs/fiber/examples/join.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/boost/libs/fiber/examples/join.cpp b/src/boost/libs/fiber/examples/join.cpp
new file mode 100644
index 00000000..7ece0500
--- /dev/null
+++ b/src/boost/libs/fiber/examples/join.cpp
@@ -0,0 +1,67 @@
+
+// Copyright Oliver Kowalke 2013.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#include <cstdlib>
+#include <functional>
+#include <stdexcept>
+#include <iostream>
+#include <string>
+
+#include <boost/fiber/all.hpp>
+
+int value1 = 0;
+int value2 = 0;
+
+void fn1()
+{
+ boost::fibers::fiber::id this_id = boost::this_fiber::get_id();
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value1;
+ std::cout << "fiber " << this_id << " fn1: increment value1: " << value1 << std::endl;
+ boost::this_fiber::yield();
+ }
+ std::cout << "fiber " << this_id << " fn1: returns" << std::endl;
+}
+
+void fn2( boost::fibers::fiber & f)
+{
+ boost::fibers::fiber::id this_id = boost::this_fiber::get_id();
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value2;
+ std::cout << "fiber " << this_id << " fn2: increment value2: " << value2 << std::endl;
+ if ( i == 1)
+ {
+ boost::fibers::fiber::id id = f.get_id();
+ std::cout << "fiber " << this_id << " fn2: joins fiber " << id << std::endl;
+ f.join();
+ std::cout << "fiber " << this_id << " fn2: joined fiber " << id << std::endl;
+ }
+ boost::this_fiber::yield();
+ }
+ std::cout << "fiber " << this_id << " fn2: returns" << std::endl;
+}
+
+int main()
+{
+ try
+ {
+ boost::fibers::fiber f1( fn1);
+ boost::fibers::fiber f2( fn2, std::ref( f1) );
+
+ f2.join();
+
+ std::cout << "done." << std::endl;
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch (...)
+ { std::cerr << "unhandled exception" << std::endl; }
+ return EXIT_FAILURE;
+}