|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r62104 - sandbox/chrono/libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2010-05-20 03:38:37
Author: viboes
Date: 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
New Revision: 62104
URL: http://svn.boost.org/trac/boost/changeset/62104
Log:
Boost.Chrono: Add Boost.Threads 1.43: port to Boost.Chrono to conform with C++0x
Added:
sandbox/chrono/libs/thread/example/
sandbox/chrono/libs/thread/example/Jamfile.v2 (contents, props changed)
sandbox/chrono/libs/thread/example/condition.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/monitor.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/mutex.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/once.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/recursive_mutex.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/starvephil.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/tennis.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/thread.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/thread_group.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/tss.cpp (contents, props changed)
sandbox/chrono/libs/thread/example/xtime.cpp (contents, props changed)
Added: sandbox/chrono/libs/thread/example/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/Jamfile.v2 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,23 @@
+# 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)
+
+project boost/thread/example
+ : requirements <library>../build//boost_thread <threading>multi
+ ;
+
+
+exe monitor : monitor.cpp ;
+exe starvephil : starvephil.cpp ;
+exe tennis : tennis.cpp ;
+exe condition : condition.cpp ;
+exe mutex : mutex.cpp ;
+exe once : once.cpp ;
+exe recursive_mutex : recursive_mutex.cpp ;
+exe thread : thread.cpp ;
+exe thread_group : thread_group.cpp ;
+exe tss : tss.cpp ;
+exe xtime : xtime.cpp ;
+
Added: sandbox/chrono/libs/thread/example/condition.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/condition.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,89 @@
+// 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 <iostream>
+#include <vector>
+#include <boost/utility.hpp>
+#include <boost/thread/condition.hpp>
+#include <boost/thread/thread.hpp>
+
+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);
+
+boost::mutex io_mutex;
+
+void sender() {
+ int n = 0;
+ while (n < 1000000) {
+ buf.send(n);
+ if(!(n%10000))
+ {
+ boost::mutex::scoped_lock io_lock(io_mutex);
+ std::cout << "sent: " << n << std::endl;
+ }
+ ++n;
+ }
+ buf.send(-1);
+}
+
+void receiver() {
+ int n;
+ do {
+ n = buf.receive();
+ if(!(n%10000))
+ {
+ boost::mutex::scoped_lock io_lock(io_mutex);
+ std::cout << "received: " << n << std::endl;
+ }
+ } while (n != -1); // -1 indicates end of buffer
+ buf.send(-1);
+}
+
+int main(int, char*[])
+{
+ boost::thread thrd1(&sender);
+ boost::thread thrd2(&receiver);
+ boost::thread thrd3(&receiver);
+ boost::thread thrd4(&receiver);
+ thrd1.join();
+ thrd2.join();
+ thrd3.join();
+ thrd4.join();
+ return 0;
+}
Added: sandbox/chrono/libs/thread/example/monitor.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/monitor.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,112 @@
+// 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 <vector>
+#include <iostream>
+#include <boost/thread/condition.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/recursive_mutex.hpp>
+#include <boost/thread/thread.hpp>
+
+namespace {
+const int ITERS = 100;
+boost::mutex io_mutex;
+} // namespace
+
+template <typename M>
+class buffer_t
+{
+public:
+ typedef typename M::scoped_lock scoped_lock;
+
+ buffer_t(int n)
+ : p(0), c(0), full(0), buf(n)
+ {
+ }
+
+ void send(int m)
+ {
+ scoped_lock lk(mutex);
+ while (full == buf.size())
+ cond.wait(lk);
+ buf[p] = m;
+ p = (p+1) % buf.size();
+ ++full;
+ cond.notify_one();
+ }
+ int receive()
+ {
+ scoped_lock lk(mutex);
+ while (full == 0)
+ cond.wait(lk);
+ int i = buf[c];
+ c = (c+1) % buf.size();
+ --full;
+ cond.notify_one();
+ return i;
+ }
+
+ static buffer_t& get_buffer()
+ {
+ static buffer_t buf(2);
+ return buf;
+ }
+
+ static void do_sender_thread()
+ {
+ for (int n = 0; n < ITERS; ++n)
+ {
+ {
+ boost::mutex::scoped_lock lock(io_mutex);
+ std::cout << "sending: " << n << std::endl;
+ }
+ get_buffer().send(n);
+ }
+ }
+
+ static void do_receiver_thread()
+ {
+ for (int x=0; x < (ITERS/2); ++x)
+ {
+ int n = get_buffer().receive();
+ {
+ boost::mutex::scoped_lock lock(io_mutex);
+ std::cout << "received: " << n << std::endl;
+ }
+ }
+ }
+
+private:
+ M mutex;
+ boost::condition cond;
+ unsigned int p, c, full;
+ std::vector<int> buf;
+};
+
+template <typename M>
+void do_test(M* dummy=0)
+{
+ typedef buffer_t<M> buffer_type;
+ buffer_type::get_buffer();
+ boost::thread thrd1(&buffer_type::do_receiver_thread);
+ boost::thread thrd2(&buffer_type::do_receiver_thread);
+ boost::thread thrd3(&buffer_type::do_sender_thread);
+ thrd1.join();
+ thrd2.join();
+ thrd3.join();
+}
+
+void test_buffer()
+{
+ do_test<boost::mutex>();
+ do_test<boost::recursive_mutex>();
+}
+
+int main()
+{
+ test_buffer();
+ return 0;
+}
Added: sandbox/chrono/libs/thread/example/mutex.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/mutex.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,47 @@
+// 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 io_mutex; // The iostreams are not guaranteed to be thread-safe!
+
+class counter
+{
+public:
+ counter() : count(0) { }
+
+ int increment() {
+ boost::mutex::scoped_lock scoped_lock(mutex);
+ return ++count;
+ }
+
+private:
+ boost::mutex mutex;
+ int count;
+};
+
+counter c;
+
+void change_count()
+{
+ int i = c.increment();
+ boost::mutex::scoped_lock scoped_lock(io_mutex);
+ std::cout << "count == " << i << std::endl;
+}
+
+int main(int, char*[])
+{
+ 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();
+
+ return 0;
+}
Added: sandbox/chrono/libs/thread/example/once.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/once.cpp 2010-05-20 03:38:35 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/example/recursive_mutex.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/recursive_mutex.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,49 @@
+// 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/recursive_mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <iostream>
+
+class counter
+{
+public:
+ counter() : count(0) { }
+
+ int add(int val) {
+ boost::recursive_mutex::scoped_lock scoped_lock(mutex);
+ count += val;
+ return count;
+ }
+ int increment() {
+ boost::recursive_mutex::scoped_lock scoped_lock(mutex);
+ return add(1);
+ }
+
+private:
+ boost::recursive_mutex mutex;
+ int count;
+};
+
+counter c;
+
+void change_count()
+{
+ std::cout << "count == " << c.increment() << std::endl;
+}
+
+int main(int, char*[])
+{
+ const int num_threads=4;
+
+ boost::thread_group threads;
+ for (int i=0; i < num_threads; ++i)
+ threads.create_thread(&change_count);
+
+ threads.join_all();
+
+ return 0;
+}
Added: sandbox/chrono/libs/thread/example/starvephil.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/starvephil.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,185 @@
+// 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/condition.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/xtime.hpp>
+#include <iostream>
+#include <time.h>
+
+namespace
+{
+boost::mutex iomx;
+} // namespace
+
+class canteen
+{
+public:
+ canteen() : m_chickens(0) { }
+
+ void get(int id)
+ {
+ boost::mutex::scoped_lock lock(m_mutex);
+ while (m_chickens == 0)
+ {
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Phil" << id <<
+ ": wot, no chickens? I'll WAIT ..." << std::endl;
+ }
+ m_condition.wait(lock);
+ }
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Phil" << id <<
+ ": those chickens look good ... one please ..." << std::endl;
+ }
+ m_chickens--;
+ }
+ void put(int value)
+ {
+ boost::mutex::scoped_lock lock(m_mutex);
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock()
+ << ") Chef: ouch ... make room ... this dish is "
+ << "very hot ..." << std::endl;
+ }
+ boost::xtime xt;
+ boost::xtime_get(&xt, boost::TIME_UTC);
+ xt.sec += 3;
+ boost::thread::sleep(xt);
+ m_chickens += value;
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() <<
+ ") Chef: more chickens ... " << m_chickens <<
+ " now available ... NOTIFYING ..." << std::endl;
+ }
+ m_condition.notify_all();
+ }
+
+private:
+ boost::mutex m_mutex;
+ boost::condition m_condition;
+ int m_chickens;
+};
+
+canteen g_canteen;
+
+void chef()
+{
+ const int chickens = 4;
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Chef: starting ..." << std::endl;
+ }
+ for (;;)
+ {
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Chef: cooking ..." << std::endl;
+ }
+ boost::xtime xt;
+ boost::xtime_get(&xt, boost::TIME_UTC);
+ xt.sec += 2;
+ boost::thread::sleep(xt);
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Chef: " << chickens
+ << " chickens, ready-to-go ..." << std::endl;
+ }
+ g_canteen.put(chickens);
+ }
+}
+
+struct phil
+{
+ phil(int id) : m_id(id) { }
+ void run() {
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Phil" << m_id
+ << ": starting ..." << std::endl;
+ }
+ for (;;)
+ {
+ if (m_id > 0)
+ {
+ boost::xtime xt;
+ boost::xtime_get(&xt, boost::TIME_UTC);
+ xt.sec += 3;
+ boost::thread::sleep(xt);
+ }
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Phil" << m_id
+ << ": gotta eat ..." << std::endl;
+ }
+ g_canteen.get(m_id);
+ {
+ boost::mutex::scoped_lock lock(iomx);
+ std::cout << "(" << clock() << ") Phil" << m_id
+ << ": mmm ... that's good ..." << std::endl;
+ }
+ }
+ }
+ static void do_thread(void* param) {
+ static_cast<phil*>(param)->run();
+ }
+
+ int m_id;
+};
+
+struct thread_adapt
+{
+ thread_adapt(void (*func)(void*), void* param)
+ : _func(func), _param(param)
+ {
+ }
+ int operator()() const
+ {
+ _func(_param);
+ return 0;
+ }
+
+ void (*_func)(void*);
+ void* _param;
+};
+
+class thread_adapter
+{
+public:
+ thread_adapter(void (*func)(void*), void* param)
+ : _func(func), _param(param)
+ {
+ }
+ void operator()() const { _func(_param); }
+private:
+ void (*_func)(void*);
+ void* _param;
+};
+
+int main(int argc, char* argv[])
+{
+ boost::thread thrd_chef(&chef);
+ phil p[] = { phil(0), phil(1), phil(2), phil(3), phil(4) };
+ boost::thread thrd_phil0(thread_adapter(&phil::do_thread, &p[0]));
+ boost::thread thrd_phil1(thread_adapter(&phil::do_thread, &p[1]));
+ boost::thread thrd_phil2(thread_adapter(&phil::do_thread, &p[2]));
+ boost::thread thrd_phil3(thread_adapter(&phil::do_thread, &p[3]));
+ boost::thread thrd_phil4(thread_adapter(&phil::do_thread, &p[4]));
+
+ thrd_chef.join();
+ thrd_phil0.join();
+ thrd_phil1.join();
+ thrd_phil2.join();
+ thrd_phil3.join();
+ thrd_phil4.join();
+
+ return 0;
+}
Added: sandbox/chrono/libs/thread/example/tennis.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/tennis.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,135 @@
+// 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/condition.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/xtime.hpp>
+#include <iostream>
+
+#if defined(BOOST_HAS_WINTHREADS)
+# include <windows.h>
+# include <process.h>
+#endif
+
+enum game_state
+{
+ START,
+ PLAYER_A,
+ PLAYER_B,
+ GAME_OVER,
+ ONE_PLAYER_GONE,
+ BOTH_PLAYERS_GONE
+};
+
+int state;
+boost::mutex mutex;
+boost::condition cond;
+
+char* player_name(int state)
+{
+ if (state == PLAYER_A)
+ return "PLAYER-A";
+ if (state == PLAYER_B)
+ return "PLAYER-B";
+ throw "bad player";
+ return 0;
+}
+
+void player(void* param)
+{
+ boost::mutex::scoped_lock lock(mutex);
+
+ int active = (int)param;
+ int other = active == PLAYER_A ? PLAYER_B : PLAYER_A;
+
+ while (state < GAME_OVER)
+ {
+ std::cout << player_name(active) << ": Play." << std::endl;
+ state = other;
+ cond.notify_all();
+ do
+ {
+ cond.wait(lock);
+ if (state == other)
+ {
+ std::cout << "---" << player_name(active)
+ << ": Spurious wakeup!" << std::endl;
+ }
+ } while (state == other);
+ }
+
+ ++state;
+ std::cout << player_name(active) << ": Gone." << std::endl;
+ cond.notify_all();
+}
+
+struct thread_adapt
+{
+ thread_adapt(void (*func)(void*), void* param)
+ : _func(func), _param(param)
+ {
+ }
+ int operator()() const
+ {
+ _func(_param);
+ return 0;
+ }
+
+ void (*_func)(void*);
+ void* _param;
+};
+
+class thread_adapter
+{
+public:
+ thread_adapter(void (*func)(void*), void* param)
+ : _func(func), _param(param)
+ {
+ }
+ void operator()() const { _func(_param); }
+private:
+ void (*_func)(void*);
+ void* _param;
+};
+
+int main(int argc, char* argv[])
+{
+ state = START;
+
+ boost::thread thrda(thread_adapter(&player, (void*)PLAYER_A));
+ boost::thread thrdb(thread_adapter(&player, (void*)PLAYER_B));
+
+ boost::xtime xt;
+ boost::xtime_get(&xt, boost::TIME_UTC);
+ xt.sec += 1;
+ boost::thread::sleep(xt);
+ {
+ boost::mutex::scoped_lock lock(mutex);
+ std::cout << "---Noise ON..." << std::endl;
+ }
+
+ for (int i = 0; i < 1000000000; ++i)
+ cond.notify_all();
+
+ {
+ boost::mutex::scoped_lock lock(mutex);
+ std::cout << "---Noise OFF..." << std::endl;
+ state = GAME_OVER;
+ cond.notify_all();
+ do
+ {
+ cond.wait(lock);
+ } while (state != BOTH_PLAYERS_GONE);
+ }
+
+ std::cout << "GAME OVER" << std::endl;
+
+ thrda.join();
+ thrdb.join();
+
+ return 0;
+}
Added: sandbox/chrono/libs/thread/example/thread.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/thread.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,35 @@
+// 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/xtime.hpp>
+#include <iostream>
+
+struct thread_alarm
+{
+ thread_alarm(int secs) : m_secs(secs) { }
+ void operator()()
+ {
+ boost::xtime xt;
+ boost::xtime_get(&xt, boost::TIME_UTC);
+ xt.sec += m_secs;
+
+ boost::thread::sleep(xt);
+
+ std::cout << "alarm sounded..." << std::endl;
+ }
+
+ int m_secs;
+};
+
+int main(int argc, char* argv[])
+{
+ int secs = 5;
+ std::cout << "setting alarm for 5 seconds..." << std::endl;
+ thread_alarm alarm(secs);
+ boost::thread thrd(alarm);
+ thrd.join();
+}
Added: sandbox/chrono/libs/thread/example/thread_group.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/thread_group.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,25 @@
+// 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>
+
+int count = 0;
+boost::mutex mutex;
+
+void increment_count()
+{
+ boost::mutex::scoped_lock lock(mutex);
+ std::cout << "count = " << ++count << std::endl;
+}
+
+int main(int argc, char* argv[])
+{
+ boost::thread_group threads;
+ for (int i = 0; i < 10; ++i)
+ threads.create_thread(&increment_count);
+ threads.join_all();
+}
Added: sandbox/chrono/libs/thread/example/tss.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/tss.cpp 2010-05-20 03:38:35 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();
+}
Added: sandbox/chrono/libs/thread/example/xtime.cpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/libs/thread/example/xtime.cpp 2010-05-20 03:38:35 EDT (Thu, 20 May 2010)
@@ -0,0 +1,16 @@
+// 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/xtime.hpp>
+
+int main(int argc, char* argv[])
+{
+ boost::xtime xt;
+ boost::xtime_get(&xt, boost::TIME_UTC);
+ xt.sec += 1;
+ boost::thread::sleep(xt); // Sleep for 1 second
+}
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