2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-23 18:12:12 +00:00
Files
thread/example/not_interleaved.cpp
Vicente J. Botet Escriba 11e15ff0f2 Thread: Added an example using externally_locked_stream
[SVN r81103]
2012-10-29 19:09:47 +00:00

43 lines
1.1 KiB
C++

// (C) Copyright 2012 Howard Hinnant
// (C) Copyright 2012 Vicente Botet
//
// 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)
// adapted from the example given by Howard Hinnant in
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/externally_locked_stream.hpp>
void use_cerr(boost::externally_locked_stream<std::ostream> &mcerr)
{
using namespace boost;
auto tf = chrono::steady_clock::now() + chrono::seconds(10);
while (chrono::steady_clock::now() < tf)
{
mcerr << "logging data to cerr\n";
this_thread::sleep_for(milliseconds(500));
}
}
int main()
{
using namespace boost;
externally_locked_stream<std::ostream> mcerr(std::cerr, terminal_mutex());
externally_locked_stream<std::ostream> mcout(std::cerr, terminal_mutex());
externally_locked_stream<std::istream> mcin(std::cerr, terminal_mutex());
thread t1(use_cerr, mcerr);
this_thread::sleep_for(boost::chrono::seconds(2));
std::string nm;
mcout << "Enter name: ";
mcin >> nm;
t1.join();
mcout << nm << '\n';
return 0;
}