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/ostream_buffer.hpp> 14 use_cerr()15void use_cerr() 16 { 17 using namespace boost; 18 chrono::steady_clock::time_point tf = chrono::steady_clock::now() + chrono::seconds(5); 19 int i = 0; 20 while (chrono::steady_clock::now() < tf) 21 { 22 ostream_buffer<std::ostream> mcerr(std::cerr); 23 mcerr.stream() << "logging data to cerr " << i++ << "\n"; 24 this_thread::sleep_for(chrono::milliseconds(250)); 25 } 26 } 27 use_cout()28void use_cout() 29 { 30 using namespace boost; 31 chrono::steady_clock::time_point tf = chrono::steady_clock::now() + chrono::seconds(5); 32 int i = 0; 33 while (chrono::steady_clock::now() < tf) 34 { 35 ostream_buffer<std::ostream> mcout(std::cout); 36 mcout.stream() << "logging data to cout " << i++ << "\n"; 37 this_thread::sleep_for(chrono::milliseconds(500)); 38 } 39 } 40 main()41int main() 42 { 43 using namespace boost; 44 45 scoped_thread<> t1(&use_cerr); 46 scoped_thread<> t2(&use_cout); 47 this_thread::sleep_for(chrono::seconds(2)); 48 std::string nm = "he, he\n"; 49 { 50 ostream_buffer<std::ostream> mcout(std::cout); 51 mcout.stream() << "Enter name: \n"; 52 } 53 t1.join(); 54 t2.join(); 55 { 56 ostream_buffer<std::ostream> mcout(std::cout); 57 mcout.stream() << nm; 58 } 59 return 0; 60 } 61 62