Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50593 - sandbox/interthreads/libs/interthreads/example
From: vicente.botet_at_[hidden]
Date: 2009-01-14 12:58:42


Author: viboes
Date: 2009-01-14 12:58:41 EST (Wed, 14 Jan 2009)
New Revision: 50593
URL: http://svn.boost.org/trac/boost/changeset/50593

Log:
interthreads version 0.2
Adding threader/joiner
Adding Asynchronous Executors fmk
Text files modified:
   sandbox/interthreads/libs/interthreads/example/basic_keep_alive.cpp | 105 +++++++++++-----------
   sandbox/interthreads/libs/interthreads/example/hello_world.cpp | 134 +++++-----------------------
   sandbox/interthreads/libs/interthreads/example/mono_thread_id.cpp | 182 +++++++++++++++++++++++++--------------
   sandbox/interthreads/libs/interthreads/example/multiple_algorithms.cpp | 110 ++++++++++++-----------
   4 files changed, 253 insertions(+), 278 deletions(-)

Modified: sandbox/interthreads/libs/interthreads/example/basic_keep_alive.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/example/basic_keep_alive.cpp (original)
+++ sandbox/interthreads/libs/interthreads/example/basic_keep_alive.cpp 2009-01-14 12:58:41 EST (Wed, 14 Jan 2009)
@@ -26,66 +26,65 @@
 
 // #include <boost/interthreads/threader.hpp>
 // #include <boost/interthreads/joiner_tuple.hpp>
- #include <boost/interthreads/thread_decorator.hpp>
- #include <boost/interthreads/thread_keep_alive.hpp>
-// #include "./async_ostream.cpp"
- #include "./async_ostream.hpp"
- #include <boost/thread/thread.hpp>
- #include <iostream>
- #include <sstream>
+#include <boost/interthreads/thread_decorator.hpp>
+#include <boost/interthreads/thread_keep_alive.hpp>
+
+#include <boost/interthreads/typeof/threader_decorator.hpp>
+#include <boost/interthreads/wait_for_all.hpp>
+//#include <boost/type_traits.hpp>
+
+#include "./async_ostream.hpp"
+#include <boost/thread/thread.hpp>
+#include <boost/static_assert.hpp>
+#include <iostream>
+#include <sstream>
         
- namespace bith = boost::interthreads;
+namespace bith = boost::interthreads;
 
- int my_thread1() {
- sleep(2);
- return 1;
- }
- void my_thread() {
- bith::this_thread::enable_keep_alive enabler;
- for (int i=0; i<10; i++) {
- bith::this_thread::keep_alive_point();
+int my_thread1() {
+ sleep(2);
+ return 0;
+}
+int my_thread() {
+ bith::this_thread::enable_keep_alive enabler;
+ for (int i=0; i<10; i++) {
+ bith::this_thread::keep_alive_point();
 
- boost::thread::id id= boost::this_thread::get_id();
- std::stringstream sss;
+ boost::thread::id id= boost::this_thread::get_id();
+ std::stringstream sss;
 #define COUT_
 #ifdef COUT_
- bith::cout_ << "TID=" << i << " " << id << std::endl;
- bith::cout_.flush();
+ bith::cout_ << "TID=" << i << " " << id << std::endl;
+ bith::cout_.flush();
 #else
- {
- boost::lock_guard<boost::mutex> lock(out_global_mutex2);
- std::cout << "TID=" << i << " " << id << std::endl;
- }
-#endif
- boost::this_thread::sleep(boost::posix_time::milliseconds(10));
+ {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex2);
+ std::cout << "TID=" << i << " " << id << std::endl;
         }
+#endif
+ boost::this_thread::sleep(boost::posix_time::milliseconds(10));
     }
+ return 0;
+}
     
- int main() {
- #if 0
- bith::threader MyThreader;
- {
- bith::joiner<void> MyJoiner(MyThreader.fork(my_thread));
- MyJoiner.join();
- std::cout << "end" << std::endl;
- }
- {
- bith::joiner<int> MyJoiner(MyThreader.fork(my_thread1));
- int i = MyJoiner.get();
- std::cout << "i=" << i << std::endl;
- }
- #endif
- boost::thread th1(bith::make_decorator(my_thread));
- boost::thread th2(bith::make_decorator(my_thread));
- boost::thread th3(bith::make_decorator(my_thread));
- boost::thread th4(bith::make_decorator(my_thread));
- boost::thread th5(bith::make_decorator(my_thread));
-
- th1.join();
- th2.join();
- th3.join();
- th4.join();
- th5.join();
- sleep(2);
- return 0;
+int main() {
+ {
+ bith::shared_threader_decorator ae;
+ bith::wait_for_all(ae, my_thread, my_thread);
     }
+ #if 0
+ boost::thread th1(bith::make_decorator(my_thread));
+ boost::thread th2(bith::make_decorator(my_thread));
+ boost::thread th3(bith::make_decorator(my_thread));
+ boost::thread th4(bith::make_decorator(my_thread));
+ boost::thread th5(bith::make_decorator(my_thread));
+ th1.join();
+ th2.join();
+ th3.join();
+ th4.join();
+ th5.join();
+ #endif
+
+
+ return 0;
+}

Modified: sandbox/interthreads/libs/interthreads/example/hello_world.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/example/hello_world.cpp (original)
+++ sandbox/interthreads/libs/interthreads/example/hello_world.cpp 2009-01-14 12:58:41 EST (Wed, 14 Jan 2009)
@@ -11,118 +11,34 @@
 //////////////////////////////////////////////////////////////////////////////
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/thread.hpp>
+#include <boost/interthreads/typeof/threader_decorator.hpp>
+
 boost::mutex out_global_mutex;
 
- #include <iostream>
- #include <boost/interthreads/thread_decorator.hpp>
- #include <boost/thread.hpp>
- #include <boost/function.hpp>
+#include <iostream>
+#include <boost/interthreads/thread_decorator.hpp>
+#include <boost/thread.hpp>
+#include <boost/function.hpp>
         
- namespace bith = boost::interthreads;
+namespace bith = boost::interthreads;
 
- void my_setup() {
- std::cout << "Hello World!" << std::endl;
- }
-
- void my_cleanup() {
- std::cout << "Bye, Bye!" << std::endl;
- }
-
- bith::thread_decoration my_decoration(my_setup, my_cleanup);
-
- struct mycallable1
- {
- void operator()() const {
- std::cout << "mycallable1" << std::endl;
- };
- };
-
- struct mycallable2
- {
- void operator()() const {
- std::cout << "mycallable2" << std::endl;
- };
- };
- void my_thread() {
- std::cout << "..." << std::endl;
-// mycallable1 x;
-// boost::this_thread::at_thread_exit(x);
- }
-
- template <typename F>
- void my_test(F f) {
- F cpy = f;
- cpy();
- }
-
- struct X {
- template <typename F>
- X(F f) {
- F cpy = f;
- cpy();
- }
- };
-
- int main() {
-#if 0
- mycallable2 x;
- boost::this_thread::at_thread_exit(x);
-// boost::thread th1(x);
-
- bith::thread_decorator my_thread_d1(my_thread);
- my_test(my_thread_d1);
- X x1(my_thread_d1);
-
- bith::thread_decorator my_thread_d2(my_thread_d1);
- my_test(my_thread_d1);
- X x2(my_thread_d2);
-
- bith::thread_decorator my_thread_d3;
- my_thread_d3 = my_thread_d2;
- my_test(my_thread_d2);
- X x3(my_thread_d3);
-#endif
-#if 1
- bith::thread_decorator my_thread_d(my_thread);
-// boost::thread th(my_thread_d);
-
-// boost::detail::thread_move_t<bith::thread_decorator> mt(my_thread_d);
-// boost::thread th(mt);
- boost::thread th(bith::make_decorator(my_thread));
- boost::thread th2(bith::make_decorator(my_thread));
-
-// boost::function<void ()> fct(my_thread_d);
-// fct();
-// boost::thread th(boost::ref(my_thread_d));
-// boost::thread th(boost::detail::thread_move_t<bith::thread_decorator>(my_thread_d));
-// boost::thread th(boost::move(my_thread_d));
-// boost::thread th(fct);
-
- {
- boost::mutex::scoped_lock out_guard(out_global_mutex);
- std::cout << "waiting1 " <<std::endl;
- }
- th.join();
- {
- boost::mutex::scoped_lock out_guard(out_global_mutex);
- std::cout << "waiting2" <<std::endl;
- }
- th2.join();
- {
- boost::mutex::scoped_lock out_guard(out_global_mutex);
- std::cout << "end" <<std::endl;
- }
-#endif
-
-#if 0
- mycallable2 c2;
- boost::function<void ()> fct(c2);
- boost::thread th2(fct);
- th2.join();
-#endif
+void my_setup() {
+ std::cout << "Hello World!" << std::endl;
+}
+
+void my_cleanup() {
+ std::cout << "Bye, Bye!" << std::endl;
+}
 
-// boost::thread th(my_thread_d);
-// boost::thread th(my_thread);
+bith::thread_decoration my_decoration(my_setup, my_cleanup);
 
- return 0;
- }
+void my_thread() {
+ std::cout << "..." << std::endl;
+}
+
+
+int main() {
+ boost::thread th(bith::make_decorator(my_thread));
+ th.join();
+ return 0;
+}

Modified: sandbox/interthreads/libs/interthreads/example/mono_thread_id.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/example/mono_thread_id.cpp (original)
+++ sandbox/interthreads/libs/interthreads/example/mono_thread_id.cpp 2009-01-14 12:58:41 EST (Wed, 14 Jan 2009)
@@ -22,89 +22,141 @@
     boost::thread::sleep(t);
 }
 
- #include <boost/interthreads/thread_decorator.hpp>
- #include <boost/interthreads/thread_specific_shared_ptr.hpp>
- #include <boost/thread/thread.hpp>
- #include <iostream>
+#include <boost/fusion/include/for_each.hpp>
+
+#include <boost/interthreads/thread_decorator.hpp>
+#include <boost/interthreads/thread_specific_shared_ptr.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/interthreads/fork_all.hpp>
+#include <boost/interthreads/algorithm.hpp>
+#include <boost/interthreads/typeof/threader_decorator.hpp>
+
+#include <iostream>
         
- namespace bith = boost::interthreads;
+namespace bith = boost::interthreads;
 
- struct mono_thread_id {
- static bith::thread_decoration decoration_;
- typedef bith::thread_specific_shared_ptr<unsigned> tssp_type;
- static tssp_type current_;
- static unsigned counter_;
- static boost::mutex sync_;
+struct mono_thread_id {
+ static bith::thread_decoration decoration_;
+ typedef bith::thread_specific_shared_ptr<unsigned> tssp_type;
+ static tssp_type current_;
+ static unsigned counter_;
+ static boost::mutex sync_;
         
- static unsigned create() {
- boost::lock_guard<boost::mutex> lock(sync_);
- unsigned res = counter_;
- ++counter_;
- return res;
- }
- static void setup() {
- std::cout << "setup" << std::endl;
- current_.reset(new unsigned(create()));
- }
- public:
- static unsigned id() {
- return *current_;
- }
- static unsigned id(boost::thread::id id) {
+ static unsigned create() {
+ boost::lock_guard<boost::mutex> lock(sync_);
+ unsigned res = counter_;
+ ++counter_;
+ return res;
+ }
+ static void setup() {
+ std::cout << "setup" << std::endl;
+ current_.reset(new unsigned(create()));
+ }
+public:
+ static unsigned id() {
+ return *current_;
+ }
+ static unsigned id(boost::thread::id id) {
 
- boost::shared_ptr<unsigned> shp = current_[id];
- if (shp.get()!=0) {
- return *shp;
- } else {
- return 0xffffffff;
- }
+ boost::shared_ptr<unsigned> shp = current_[id];
+ if (shp.get()!=0) {
+ return *shp;
+ } else {
+ return 0xffffffff;
         }
+ }
 
- };
+};
 
- bith::thread_decoration mono_thread_id::decoration_(mono_thread_id::setup);
- mono_thread_id::tssp_type mono_thread_id::current_;
- unsigned mono_thread_id::counter_=0;
- boost::mutex mono_thread_id::sync_;
-
- void my_thread() {
- {
- boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << ">> thread::id=" << boost::this_thread::get_id() << " mono_thread_id=" << mono_thread_id::id() << std::endl;
- }
- sleep(5);
- {
+struct mono_thread_id_out {
+ template<typename T>
+ void operator()(T& t) const {
+ std::cout << "thread::id=" << t.get_id()
+ << " mono_thread_id=" << mono_thread_id::id(t.get_id())
+ << std::endl;
+ }
+};
+
+struct mono_thread_id_wait_and_get {
+ template<typename T>
+ void operator()(T& t) const {
+ const boost::shared_ptr<unsigned> shp1 = mono_thread_id::current_.wait_and_get(t.get_id());
+ if (shp1.get()==0) {
             boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << "<< thread::id=" << boost::this_thread::get_id() << " mono_thread_id=" << mono_thread_id::id() << std::endl;
+ std::cout << "ERROR 1" << std::endl;
         }
     }
+};
+
+bith::thread_decoration mono_thread_id::decoration_(mono_thread_id::setup);
+mono_thread_id::tssp_type mono_thread_id::current_;
+unsigned mono_thread_id::counter_=0;
+boost::mutex mono_thread_id::sync_;
     
- int main() {
- boost::thread th1(bith::make_decorator(my_thread));
- boost::thread th2(bith::make_decorator(my_thread));
-
- const boost::shared_ptr<unsigned> shp1 = mono_thread_id::current_.wait_and_get(th1.get_id());
+void my_thread() {
+ {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << ">> thread::id=" << boost::this_thread::get_id() << " mono_thread_id=" << mono_thread_id::id() << std::endl;
+ }
+ sleep(5);
+ {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "<< thread::id=" << boost::this_thread::get_id() << " mono_thread_id=" << mono_thread_id::id() << std::endl;
+ }
+}
+
+void doit() {
+ bith::shared_threader_decorator ae;
+ bith::result_of::fork_all<bith::shared_threader_decorator, boost::fusion::tuple<void(*)(), void(*)()> >::type handles =
+ bith::fork_all(ae, my_thread, my_thread);
+ {
+ const boost::shared_ptr<unsigned> shp1 = mono_thread_id::current_.wait_and_get(boost::fusion::at_c<0>(handles).get_id());
         if (shp1.get()==0) {
             boost::lock_guard<boost::mutex> lock(out_global_mutex);
             std::cout << "ERROR 1" << std::endl;
         }
-
- const boost::shared_ptr<unsigned> shp2 = mono_thread_id::current_.wait_and_get(th2.get_id());
- if (shp2.get()==0) {
+ }
+ {
+ const boost::shared_ptr<unsigned> shp1 = mono_thread_id::current_.wait_and_get(boost::fusion::at_c<1>(handles).get_id());
+ if (shp1.get()==0) {
             boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << "ERROR 2" << std::endl;
+ std::cout << "ERROR 1" << std::endl;
         }
+ }
+ //sleep(1);
+ boost::fusion::for_each(handles, mono_thread_id_wait_and_get());
+ boost::fusion::for_each(handles, mono_thread_id_out());
+ bith::join_all(handles);
+}
 
- {
- unsigned u = mono_thread_id::id(th1.get_id());
- boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << "thread::id=" << th1.get_id() << " mono_thread_id="
- << u << std::endl;
- }
+void doit_clasic() {
+ boost::thread th1(bith::make_decorator(my_thread));
+ boost::thread th2(bith::make_decorator(my_thread));
         
- th1.join();
- th2.join();
-
- return 0;
+ const boost::shared_ptr<unsigned> shp1 = mono_thread_id::current_.wait_and_get(th1.get_id());
+ if (shp1.get()==0) {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "ERROR 1" << std::endl;
+ }
+
+ const boost::shared_ptr<unsigned> shp2 = mono_thread_id::current_.wait_and_get(th2.get_id());
+ if (shp2.get()==0) {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "ERROR 2" << std::endl;
     }
+
+ {
+ unsigned u = mono_thread_id::id(th1.get_id());
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "thread::id=" << th1.get_id() << " mono_thread_id="
+ << u << std::endl;
+ }
+
+ th1.join();
+ th2.join();
+}
+int main() {
+ doit();
+ return 0;
+}
     

Modified: sandbox/interthreads/libs/interthreads/example/multiple_algorithms.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/example/multiple_algorithms.cpp (original)
+++ sandbox/interthreads/libs/interthreads/example/multiple_algorithms.cpp 2009-01-14 12:58:41 EST (Wed, 14 Jan 2009)
@@ -23,72 +23,80 @@
 }
 
 
-// #include <boost/interthreads/thread_decorator.hpp>
- #include <boost/interthreads/thread_tuple.hpp>
- #include <boost/interthreads/thread_tuple_once.hpp>
- #include <boost/interthreads/thread_group_once.hpp>
- #include <boost/interthreads/thread_and_join.hpp>
- #include <boost/thread.hpp>
- #include <iostream>
+#include <boost/interthreads/thread_tuple.hpp>
+#include <boost/interthreads/thread_tuple_once.hpp>
+#include <boost/interthreads/thread_group_once.hpp>
+#include <boost/interthreads/thread_and_join.hpp>
+#include <boost/interthreads/typeof/threader.hpp>
+#include <boost/interthreads/wait_for_any.hpp>
+#include <boost/thread.hpp>
+#include <iostream>
         
- namespace bith = boost::interthreads;
+namespace bith = boost::interthreads;
 
- void my_thread1() {
- {
- boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << "1 thread_id=" << boost::this_thread::get_id() << std::endl;
- }
- sleep(10);
+int my_thread1() {
+ {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "1 thread_id=" << boost::this_thread::get_id() << std::endl;
     }
+ sleep(3);
+ return 0;
+}
     
- void my_thread2() {
- {
- boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << "2 thread_id=" << boost::this_thread::get_id() << std::endl;
- }
- sleep(5);
+int my_thread2() {
+ {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "2 thread_id=" << boost::this_thread::get_id() << std::endl;
     }
+ sleep(1);
+ return 0;
+}
 
- void my_thread3() {
- {
- boost::lock_guard<boost::mutex> lock(out_global_mutex);
- std::cout << "3 thread_id=" << boost::this_thread::get_id() << std::endl;
- }
- sleep(7);
+int my_thread3() {
+ {
+ boost::lock_guard<boost::mutex> lock(out_global_mutex);
+ std::cout << "3 thread_id=" << boost::this_thread::get_id() << std::endl;
     }
+ sleep(2);
+ return 0;
+}
 
- int main() {
 
- bith::thread_tuple<3> tt_0(my_thread1, my_thread2, my_thread3);
- bith::thread_tuple<3> tt_1;
- tt_1= tt_0.move();
- bith::thread_tuple<3> tt_2(tt_1.move());
- bith::thread_tuple<3> tt(boost::move(tt_2));
+int main() {
+ bith::shared_threader ae;
+ BOOST_AUTO(handles,bith::fork_all(ae, my_thread1, my_thread2, my_thread3));
+ BOOST_AUTO(result,bith::wait_for_any(ae, my_thread1, my_thread2, my_thread3));
+ std::cout << "Algotithm " << result.first+1 << " finished the first with wait_for_any" << std::endl;
+
+ bith::thread_tuple<3> tt_0(my_thread1, my_thread2, my_thread3);
+ bith::thread_tuple<3> tt_1;
+ tt_1= tt_0.move();
+ bith::thread_tuple<3> tt_2(tt_1.move());
+ bith::thread_tuple<3> tt(boost::move(tt_2));
 
- tt.join_all();
- std::cout << "All finished join_all" << std::endl;
+ tt.join_all();
+ std::cout << "All finished join_all" << std::endl;
         
- bith::thread_tuple<3> kk_0= bith::make_thread_tuple(my_thread1, my_thread2, my_thread3);
- kk_0.join_all();
- std::cout << "All finished join_all" << std::endl;
+ bith::thread_tuple<3> kk_0= bith::make_thread_tuple(my_thread1, my_thread2, my_thread3);
+ kk_0.join_all();
+ std::cout << "All finished join_all" << std::endl;
 
-#if 0
+#if 0
         
- bith::thread_group_once tgo;
- boost::thread* th1 = tgo.create_thread(my_thread1);
- boost::thread* th2 = tgo.create_thread(my_thread2);
- boost::thread* th3 = tgo.create_thread(my_thread3);
- boost::thread* res_tgo= tgo.join_any();
- std::cout << "Algotithm " << res_tgo << " " << th2 << " finished the first with thread_group_once::join_any" << std::endl;
+ bith::thread_group_once tgo;
+ boost::thread* th1 = tgo.create_thread(my_thread1);
+ boost::thread* th2 = tgo.create_thread(my_thread2);
+ boost::thread* th3 = tgo.create_thread(my_thread3);
+ boost::thread* res_tgo= tgo.join_any();
+ std::cout << "Algotithm " << res_tgo << " " << th2 << " finished the first with thread_group_once::join_any" << std::endl;
         
        
- bith::conc_join_all(my_thread1, my_thread2, my_thread3);
- std::cout << "All finished conc_join_all" << std::endl;
+ bith::conc_join_all(my_thread1, my_thread2, my_thread3);
+ std::cout << "All finished conc_join_all" << std::endl;
 
- unsigned res = bith::conc_join_any(my_thread1, my_thread2, my_thread3);
- std::cout << "Algotithm " << res+1 << " finished the first with conc_join_any" << std::endl;
+ unsigned res = bith::conc_join_any(my_thread1, my_thread2, my_thread3);
+ std::cout << "Algotithm " << res+1 << " finished the first with conc_join_any" << std::endl;
 #endif
         
- return 0;
- }
-
+ 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