Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81103 - trunk/libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2012-10-29 15:09:48


Author: viboes
Date: 2012-10-29 15:09:47 EDT (Mon, 29 Oct 2012)
New Revision: 81103
URL: http://svn.boost.org/trac/boost/changeset/81103

Log:
Thread: Added an example using externally_locked_stream
Added:
   trunk/libs/thread/example/not_interleaved.cpp (contents, props changed)

Added: trunk/libs/thread/example/not_interleaved.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/example/not_interleaved.cpp 2012-10-29 15:09:47 EDT (Mon, 29 Oct 2012)
@@ -0,0 +1,42 @@
+// (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;
+}
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk