]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/example/not_interleaved.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / thread / example / not_interleaved.cpp
1 // (C) Copyright 2012 Howard Hinnant
2 // (C) Copyright 2012 Vicente Botet
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 // adapted from the example given by Howard Hinnant in
8
9 #define BOOST_THREAD_VERSION 4
10
11 #include <iostream>
12 #include <boost/thread/scoped_thread.hpp>
13 #include <boost/thread/externally_locked_stream.hpp>
14
15 void use_cerr(boost::externally_locked_stream<std::ostream> &mcerr)
16 {
17 using namespace boost;
18 chrono::steady_clock::time_point tf = chrono::steady_clock::now() + chrono::seconds(10);
19 while (chrono::steady_clock::now() < tf)
20 {
21 mcerr << "logging data to cerr\n";
22 this_thread::sleep_for(chrono::milliseconds(500));
23 }
24 }
25
26 void use_cout(boost::externally_locked_stream<std::ostream> &mcout)
27 {
28 using namespace boost;
29 chrono::steady_clock::time_point tf = chrono::steady_clock::now() + chrono::seconds(5);
30 while (chrono::steady_clock::now() < tf)
31 {
32 mcout << "logging data to cout\n";
33 this_thread::sleep_for(chrono::milliseconds(250));
34 }
35 }
36
37 int main()
38 {
39 using namespace boost;
40
41 recursive_mutex terminal_mutex;
42
43 externally_locked_stream<std::ostream> mcerr(std::cerr, terminal_mutex);
44 externally_locked_stream<std::ostream> mcout(std::cout, terminal_mutex);
45 externally_locked_stream<std::istream> mcin(std::cin, terminal_mutex);
46
47 scoped_thread<> t1(boost::thread(use_cerr, boost::ref(mcerr)));
48 scoped_thread<> t2(boost::thread(use_cout, boost::ref(mcout)));
49 this_thread::sleep_for(chrono::seconds(2));
50 std::string nm;
51 {
52 strict_lock<recursive_mutex> lk(terminal_mutex);
53 std::ostream & gcout = mcout.get(lk);
54 //std::istream & gcin = mcin.get(lk);
55 gcout << "Enter name: ";
56 //gcin >> nm;
57 }
58 t1.join();
59 t2.join();
60 mcout << nm << '\n';
61 return 0;
62 }
63