Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r49837 - sandbox/interthreads/libs/interthreads/test
From: vicente.botet_at_[hidden]
Date: 2008-11-19 06:05:13


Author: viboes
Date: 2008-11-19 06:05:12 EST (Wed, 19 Nov 2008)
New Revision: 49837
URL: http://svn.boost.org/trac/boost/changeset/49837

Log:
intterthreads version 0.1
Added:
   sandbox/interthreads/libs/interthreads/test/Jamfile.v2 (contents, props changed)
   sandbox/interthreads/libs/interthreads/test/test_thread_decorator.cpp (contents, props changed)
   sandbox/interthreads/libs/interthreads/test/test_thread_shared_ptr.cpp (contents, props changed)

Added: sandbox/interthreads/libs/interthreads/test/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/interthreads/libs/interthreads/test/Jamfile.v2 2008-11-19 06:05:12 EST (Wed, 19 Nov 2008)
@@ -0,0 +1,57 @@
+# (C) Copyright William E. Kempf 2001.
+# (C) Copyright 2007 Anthony Williams.
+# (C) Copyright 2007 Vicente Botet Escriba.
+# 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)
+#
+# Boost.InterThreads test Jamfile
+#
+# Additional configuration variables used:
+# 1. PTW32 may be used on Win32 platforms to specify that the pthreads-win32
+# library should be used instead of "native" threads. This feature is
+# mostly used for testing and it's generally recommended you use the
+# native threading libraries instead. PTW32 should be set to be a list
+# of two strings, the first specifying the installation path of the
+# pthreads-win32 library and the second specifying which library
+# variant to link against (see the pthreads-win32 documentation).
+# Example: jam -sPTW32="c:\pthreads-win32 pthreadVCE.lib"
+
+# bring in rules for testing
+import testing ;
+
+project
+ : requirements <library>/boost/test//boost_unit_test_framework/<link>static
+ <library>/boost/thread//boost_thread/<link>static
+ <include>../../..
+ <threading>multi
+ <target-os>cygwin
+ <threadapi>pthread
+ <variant>debug
+
+ ;
+
+rule interthreads-run ( sources )
+{
+ return
+ [ run $(sources) ../build//boost_interthreads ]
+# [ run $(sources) ../build//boost_interthreads/<link>static
+# : : : : $(sources[1]:B)_lib ]
+ ;
+}
+
+{
+ test-suite "tests"
+ :
+ [ interthreads-run test_thread_decorator.cpp ]
+ [ interthreads-run test_thread_shared_ptr.cpp ]
+ ;
+ test-suite "example"
+ :
+ [ interthreads-run ../example/hello_world.cpp ]
+ [ interthreads-run ../example/mono_thread_id.cpp ]
+ [ interthreads-run ../example/basic_keep_alive.cpp ]
+ [ interthreads-run ../example/multiple_algorithms.cpp ]
+
+
+ ;
+}

Added: sandbox/interthreads/libs/interthreads/test/test_thread_decorator.cpp
==============================================================================
--- (empty file)
+++ sandbox/interthreads/libs/interthreads/test/test_thread_decorator.cpp 2008-11-19 06:05:12 EST (Wed, 19 Nov 2008)
@@ -0,0 +1,124 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Roland Schwarz 2006.
+// (C) Copyright Vicente J. Botet Escriba 2008.
+// 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)
+//
+// See http://www.boost.org/libs/interthreads for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <iostream>
+#include <boost/thread/tss.hpp>
+#include <boost/thread.hpp>
+#include <boost/interthreads/thread_decorator.hpp>
+//#include <boost/interthreads/thread_tuple.hpp>
+
+boost::mutex out_global_mutex;
+
+void cleanup_i(boost::thread::id* data) {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "cleanup_i " << *data <<" " << boost::this_thread::get_id()<<std::endl;
+}
+
+boost::thread_specific_ptr<boost::thread::id> specific_ptr_i(cleanup_i);
+
+boost::once_flag global_init_flag=BOOST_ONCE_INIT;
+void global_init() {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "global_init" << boost::this_thread::get_id()<<std::endl;
+}
+
+void thread_init_1() {
+ specific_ptr_i.reset(new boost::thread::id(boost::this_thread::get_id()));
+ boost::call_once(global_init_flag, global_init);
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "thread_init_1 " << boost::this_thread::get_id()<<std::endl;
+}
+
+void thread_init_2() {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "thread_init_2 " << boost::this_thread::get_id()<<std::endl;
+}
+void sleep(int sec)
+{
+ boost::xtime t;
+ boost::xtime_get(&t,1);
+ t.sec += sec;
+ boost::thread::sleep(t);
+}
+
+void run1() {
+ sleep(3);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "long operation on" << boost::this_thread::get_id()<<std::endl;
+ }
+ sleep(10);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "long operation end" << boost::this_thread::get_id()<<std::endl;
+ }
+}
+
+void run2() {
+ sleep(1);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "long operation on" << boost::this_thread::get_id()<<std::endl;
+ }
+ sleep(20);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "long operation end" << boost::this_thread::get_id()<<std::endl;
+ }
+}
+
+boost::interthreads::thread_decoration p1(thread_init_1);
+boost::interthreads::thread_decoration p2(thread_init_2);
+
+
+int main(int argc, char* argv[])
+{
+ boost::interthreads::decorate();
+
+ //boost::interthreads::thread_tuple<2> gr(boost::interthreads::make_decorator(run1), boost::interthreads::make_decorator(run2));
+ boost::interthreads::thread_decorator run1p(run1);
+ boost::interthreads::thread_decorator run2p(run2);
+ boost::thread pt1(run1p);
+ boost::thread pt2(run2p);
+// boost::thread pt1(boost::interthreads::make_decorator(run1));
+// boost::thread pt2(boost::interthreads::make_decorator(run2));
+ sleep(1);
+ boost::thread pt12(run1p);
+// boost::thread pt12(boost::interthreads::make_decorator(run1));
+ sleep(1);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "waiting pt1" <<std::endl;
+ }
+
+ pt1.join();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "waiting pt12" <<std::endl;
+ }
+ pt12.join();
+ //gr.join_all();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "waiting pt2" <<std::endl;
+ }
+ pt2.join();
+
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "END" <<std::endl;
+ }
+
+ return 0;
+}
+
+
+

Added: sandbox/interthreads/libs/interthreads/test/test_thread_shared_ptr.cpp
==============================================================================
--- (empty file)
+++ sandbox/interthreads/libs/interthreads/test/test_thread_shared_ptr.cpp 2008-11-19 06:05:12 EST (Wed, 19 Nov 2008)
@@ -0,0 +1,159 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Roland Schwarz 2006.
+// (C) Copyright Vicente J. Botet Escriba 2008.
+// 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)
+//
+// See http://www.boost.org/libs/interthreads for documentation.
+//
+// Based on the shared.cpp example from the threadalert library of Roland Schwarz
+//////////////////////////////////////////////////////////////////////////////
+#include <boost/thread/mutex.hpp>
+boost::mutex out_global_mutex;
+#include <boost/thread/locks.hpp>
+#include <boost/thread.hpp>
+#include <iostream>
+#include <boost/interthreads/thread_specific_shared_ptr.hpp>
+#include <boost/interthreads/thread_decorator.hpp>
+
+void sleep(int sec)
+{
+ boost::xtime t;
+ boost::xtime_get(&t,1); t.sec += sec; boost::thread::sleep(t);
+}
+
+// A state class living in shared memory.
+// The class must have its own sync mechanism.
+class state {
+public:
+ state() : result(0)
+ {}
+ void set_result(int n)
+ {
+ boost::mutex::scoped_lock lock(monitor);
+ result = n;
+ }
+ int get_result()
+ {
+ boost::mutex::scoped_lock lock(monitor);
+ return result;
+ }
+ static void deleter(state * p) { delete p; }
+private:
+ ~state() {}
+ boost::mutex monitor;
+ int result;
+};
+
+// The conventional tss pointer.
+// It can be seen as a memory space that is
+// only privately accessible by the thread,
+// when interpreted in our context.
+boost::thread_specific_ptr<int> private_int;
+
+// The thread member pointer.
+// Its syntax is almost the same when used
+// from "inside" a thread as that of the
+// thread_specific_ptr. I named it public
+// to underpin the fact it is accessible
+// from "outside" too. (Perhaps other names
+// than thread_specific_shared_ptr and thread_specific_ptr
+// would be more appropriate? Any suggestions?
+// E.g. thread_public_ptr and thread_private_ptr?)
+boost::interthreads::thread_specific_shared_ptr<state> public_state;
+
+// It might be convenient to have a function
+// that is called automatically on thread start
+// up to auto initialize the variables.
+void init_state_fn()
+{
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "init_state_fn" << " " << boost::this_thread::get_id()<<std::endl;
+ }
+
+ public_state.reset(new state(), state::deleter);
+ private_int.reset(new int(0));
+}
+
+boost::interthreads::thread_decoration init_state(init_state_fn);
+
+
+void run()
+{
+ init_state_fn();
+ sleep(2);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "run " << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ public_state->set_result(42);
+
+ boost::shared_ptr<state> ths = public_state[boost::this_thread::get_id()];
+ int result;
+ result = ths->get_result();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "ahead " << result << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ sleep(2);
+
+}
+
+int main(int argc, char* argv[])
+{
+ int result;
+
+ boost::thread* th = new boost::thread(boost::interthreads::make_decorator(run));
+
+ const boost::shared_ptr<state> ths = public_state.wait_and_get(th->get_id());
+ if (ths.get()!=0) {
+ result = ths->get_result();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "ahead " << result << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ }
+
+ private_int.reset(new int(0));
+ ths->set_result(1111);
+ if (ths.get()!=0) {
+ result = ths->get_result();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "ahead " << result << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ }
+
+ sleep(2);
+ boost::shared_ptr<state> ths3 = public_state[th->get_id()];
+ if (ths.get()!=0) {
+ result = ths->get_result();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "ahead " << result << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ }
+
+ th->join();
+
+ delete th;
+
+ // We still may access the state object by means of the shared_ptr.
+ if (ths.get()!=0) {
+ result = ths->get_result();
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "ahead " << result << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ }
+ const boost::shared_ptr<state> cths(new state(), state::deleter);
+ cths->set_result(2);
+ {
+ boost::mutex::scoped_lock out_guard(out_global_mutex);
+ std::cout << "xxx" << cths->get_result() << " " << boost::this_thread::get_id()<<std::endl;
+ }
+ 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