Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62106 - in sandbox/chrono/libs/thread: . tutorial
From: vicente.botet_at_[hidden]
Date: 2010-05-20 03:52:30


Author: viboes
Date: 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
New Revision: 62106
URL: http://svn.boost.org/trac/boost/changeset/62106

Log:
Boost.Chrono: Add Boost.Threads 1.43: port to Boost.Chrono to conform with C++0x
Added:
   sandbox/chrono/libs/thread/index.html (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/
   sandbox/chrono/libs/thread/tutorial/bounded_buffer.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/counter.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/factorial.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/factorial2.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/factorial3.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/helloworld.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/helloworld2.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/helloworld3.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/helloworld4.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/once.cpp (contents, props changed)
   sandbox/chrono/libs/thread/tutorial/tss.cpp (contents, props changed)

Added: sandbox/chrono/libs/thread/index.html
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/index.html 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,13 @@
+<!-- Copyright (c) 2002-2003 William E. Kempf.
+ Subject to the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
+-->
+
+<html>
+<head>
+<meta http-equiv="refresh" content="0; URL=doc/index.html">
+</head>
+<body>
+Automatic redirection failed, please go to doc/index.html
+</body>
+</html>

Added: sandbox/chrono/libs/thread/tutorial/bounded_buffer.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/bounded_buffer.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,69 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/condition.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <iostream>
+#include <vector>
+
+class bounded_buffer : private boost::noncopyable
+{
+public:
+ typedef boost::mutex::scoped_lock lock;
+ bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { }
+ void send (int m) {
+ lock lk(monitor);
+ while (buffered == circular_buf.size())
+ buffer_not_full.wait(lk);
+ circular_buf[end] = m;
+ end = (end+1) % circular_buf.size();
+ ++buffered;
+ buffer_not_empty.notify_one();
+ }
+ int receive() {
+ lock lk(monitor);
+ while (buffered == 0)
+ buffer_not_empty.wait(lk);
+ int i = circular_buf[begin];
+ begin = (begin+1) % circular_buf.size();
+ --buffered;
+ buffer_not_full.notify_one();
+ return i;
+ }
+private:
+ int begin, end, buffered;
+ std::vector<int> circular_buf;
+ boost::condition buffer_not_full, buffer_not_empty;
+ boost::mutex monitor;
+};
+bounded_buffer buf(2);
+
+void sender() {
+ int n = 0;
+ while (n < 100) {
+ buf.send(n);
+ std::cout << "sent: " << n << std::endl;
+ ++n;
+ }
+ buf.send(-1);
+}
+
+void receiver() {
+ int n;
+ do {
+ n = buf.receive();
+ std::cout << "received: " << n << std::endl;
+ } while (n != -1); // -1 indicates end of buffer
+}
+
+int main()
+{
+ boost::thread thrd1(&sender);
+ boost::thread thrd2(&receiver);
+ thrd1.join();
+ thrd2.join();
+}

Added: sandbox/chrono/libs/thread/tutorial/counter.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/counter.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,28 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <iostream>
+
+boost::mutex mutex;
+int counter=0;
+
+void change_count()
+{
+ boost::mutex::scoped_lock lock(mutex);
+ int i = ++counter;
+ std::cout << "count == " << i << std::endl;
+}
+
+int main()
+{
+ const int num_threads = 4;
+ boost::thread_group thrds;
+ for (int i=0; i < num_threads; ++i)
+ thrds.create_thread(&change_count);
+ thrds.join_all();
+}

Added: sandbox/chrono/libs/thread/tutorial/factorial.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/factorial.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,32 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <iostream>
+
+class factorial
+{
+public:
+ factorial(int x, int& res) : x(x), res(res) { }
+ void operator()() { res = calculate(x); }
+ int result() const { return res; }
+
+private:
+ int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }
+
+private:
+ int x;
+ int& res;
+};
+
+int main()
+{
+ int result;
+ factorial f(10, result);
+ boost::thread thrd(f);
+ thrd.join();
+ std::cout << "10! = " << result << std::endl;
+}

Added: sandbox/chrono/libs/thread/tutorial/factorial2.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/factorial2.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,32 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <boost/ref.hpp>
+#include <iostream>
+
+class factorial
+{
+public:
+ factorial(int x) : x(x), res(0) { }
+ void operator()() { res = calculate(x); }
+ int result() const { return res; }
+
+private:
+ int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }
+
+private:
+ int x;
+ int res;
+};
+
+int main()
+{
+ factorial f(10);
+ boost::thread thrd(boost::ref(f));
+ thrd.join();
+ std::cout << "10! = " << f.result() << std::endl;
+}

Added: sandbox/chrono/libs/thread/tutorial/factorial3.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/factorial3.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,36 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <iostream>
+
+const int NUM_CALCS=5;
+
+class factorial
+{
+public:
+ factorial(int x, int& res) : x(x), res(res) { }
+ void operator()() { res = calculate(x); }
+ int result() const { return res; }
+
+private:
+ int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }
+
+private:
+ int x;
+ int& res;
+};
+
+int main()
+{
+ int results[NUM_CALCS];
+ boost::thread_group thrds;
+ for (int i=0; i < NUM_CALCS; ++i)
+ thrds.create_thread(factorial(i*10, results[i]));
+ thrds.join_all();
+ for (int j=0; j < NUM_CALCS; ++j)
+ std::cout << j*10 << "! = " << results[j] << std::endl;
+}

Added: sandbox/chrono/libs/thread/tutorial/helloworld.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/helloworld.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,19 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <iostream>
+
+void helloworld()
+{
+ std::cout << "Hello World!" << std::endl;
+}
+
+int main()
+{
+ boost::thread thrd(&helloworld);
+ thrd.join();
+}

Added: sandbox/chrono/libs/thread/tutorial/helloworld2.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/helloworld2.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,24 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <iostream>
+
+struct helloworld
+{
+ helloworld(const char* who) : m_who(who) { }
+ void operator()()
+ {
+ std::cout << m_who << "says, \"Hello World.\"" << std::endl;
+ }
+ const char* m_who;
+};
+
+int main()
+{
+ boost::thread thrd(helloworld("Bob"));
+ thrd.join();
+}

Added: sandbox/chrono/libs/thread/tutorial/helloworld3.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/helloworld3.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,20 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <boost/bind.hpp>
+#include <iostream>
+
+void helloworld(const char* who)
+{
+ std::cout << who << "says, \"Hello World.\"" << std::endl;
+}
+
+int main()
+{
+ boost::thread thrd(boost::bind(&helloworld, "Bob"));
+ thrd.join();
+}

Added: sandbox/chrono/libs/thread/tutorial/helloworld4.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/helloworld4.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,20 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <boost/bind.hpp>
+#include <iostream>
+
+void helloworld(const char* who)
+{
+ std::cout << who << "says, \"Hello World.\"" << std::endl;
+}
+
+int main()
+{
+ boost::thread thrd(boost::bind(&helloworld, "Bob"));
+ thrd.join();
+}

Added: sandbox/chrono/libs/thread/tutorial/once.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/once.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,31 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <boost/thread/once.hpp>
+#include <cassert>
+
+int value=0;
+boost::once_flag once = BOOST_ONCE_INIT;
+
+void init()
+{
+ ++value;
+}
+
+void thread_proc()
+{
+ boost::call_once(&init, once);
+}
+
+int main(int argc, char* argv[])
+{
+ boost::thread_group threads;
+ for (int i=0; i<5; ++i)
+ threads.create_thread(&thread_proc);
+ threads.join_all();
+ assert(value == 1);
+}

Added: sandbox/chrono/libs/thread/tutorial/tss.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/tutorial/tss.cpp 2010-05-20 03:52:22 EDT (Thu, 20 May 2010)
@@ -0,0 +1,36 @@
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// 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 <boost/thread/thread.hpp>
+#include <boost/thread/tss.hpp>
+#include <cassert>
+
+boost::thread_specific_ptr<int> value;
+
+void increment()
+{
+ int* p = value.get();
+ ++*p;
+}
+
+void thread_proc()
+{
+ value.reset(new int(0)); // initialize the thread's storage
+ for (int i=0; i<10; ++i)
+ {
+ increment();
+ int* p = value.get();
+ assert(*p == i+1);
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ boost::thread_group threads;
+ for (int i=0; i<5; ++i)
+ threads.create_thread(&thread_proc);
+ threads.join_all();
+}


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