Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r55610 - in sandbox/task: boost/task libs/task/examples
From: oliver.kowalke_at_[hidden]
Date: 2009-08-16 05:21:08


Author: olli
Date: 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
New Revision: 55610
URL: http://svn.boost.org/trac/boost/changeset/55610

Log:
- unbounded_buffer + examples

Added:
   sandbox/task/boost/task/unbounded_buffer.hpp (contents, props changed)
   sandbox/task/libs/task/examples/buffer_multi.cpp (contents, props changed)
   sandbox/task/libs/task/examples/buffer_multi2.cpp (contents, props changed)
   sandbox/task/libs/task/examples/buffer_pool.cpp (contents, props changed)
   sandbox/task/libs/task/examples/buffer_pool_thread.cpp (contents, props changed)
   sandbox/task/libs/task/examples/buffer_thread.cpp (contents, props changed)
   sandbox/task/libs/task/examples/no_deadlock_pool.cpp (contents, props changed)
   sandbox/task/libs/task/examples/no_deadlock_pool2.cpp (contents, props changed)
   sandbox/task/libs/task/examples/no_deadlock_pool3.cpp (contents, props changed)
   sandbox/task/libs/task/examples/semaphore_pool.cpp (contents, props changed)
   sandbox/task/libs/task/examples/semaphore_pool_thread.cpp (contents, props changed)
   sandbox/task/libs/task/examples/semaphore_thread.cpp (contents, props changed)

Added: sandbox/task/boost/task/unbounded_buffer.hpp
==============================================================================
--- (empty file)
+++ sandbox/task/boost/task/unbounded_buffer.hpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,104 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#ifndef BOOST_TASK_UNBOUNDED_BUFFER_H
+#define BOOST_TASK_UNBOUNDED_BUFFER_H
+
+#include <deque>
+
+#include <boost/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/optional.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/task/semaphore.hpp>
+
+#include <boost/config/abi_prefix.hpp>
+
+namespace boost { namespace task
+{
+template< typename T >
+class unbounded_buffer
+{
+private:
+ class base
+ {
+ private:
+ class guard : private noncopyable
+ {
+ private:
+ semaphore & sem_;
+
+ public:
+ guard( semaphore & sem)
+ : sem_( sem)
+ { sem_.wait(); }
+
+ ~guard()
+ { sem_.post(); }
+ };
+
+ semaphore protection_sem_;
+ semaphore consumer_sem_;
+ std::deque< T > queue_;
+
+ base( base &);
+ base & operator=( base const&);
+
+ bool empty_() const
+ { return queue_.empty(); }
+
+ bool consumers_activate_() const
+ { return ! empty_(); }
+
+ public:
+ base()
+ :
+ protection_sem_( 1),
+ consumer_sem_( 0),
+ queue_()
+ {}
+
+ void put( T const& t)
+ {
+ {
+ guard lk( protection_sem_);
+ queue_.push_back( t);
+ }
+ consumer_sem_.post();
+ }
+
+ bool take( optional< T > & t)
+ {
+ consumer_sem_.wait();
+ {
+ guard lk( protection_sem_);
+ t = queue_.front();
+ queue_.pop_front();
+ }
+ return true;
+ }
+ };
+
+ shared_ptr< base > impl_;
+
+public:
+ unbounded_buffer()
+ : impl_( new base)
+ {}
+
+ void put( T const& t)
+ { impl_->put( t); }
+
+ void take( optional< T > & t)
+ { impl_->take( t); }
+};
+}}
+
+#include <boost/config/abi_suffix.hpp>
+
+#endif // BOOST_TASK_UNBOUNDED_BUFFER_H

Added: sandbox/task/libs/task/examples/buffer_multi.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/buffer_multi.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,127 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+#include <utility>
+
+#include <boost/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/optional.hpp>
+#include <boost/thread.hpp>
+
+#include "boost/task.hpp"
+
+namespace tsk = boost::task;
+
+typedef tsk::static_pool< tsk::unbounded_channel< tsk::fifo > > pool_type;
+
+int serial_fib( int n)
+{
+ if( n < 2)
+ return n;
+ else
+ return serial_fib( n - 1) + serial_fib( n - 2);
+}
+
+int parallel_fib( int n, int cutof)
+{
+ if ( n < cutof) return serial_fib( n);
+ else
+ {
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+ tsk::task< int > t1(
+ parallel_fib,
+ n - 1,
+ cutof);
+ tsk::task< int > t2(
+ parallel_fib,
+ n - 2,
+ cutof);
+ tsk::handle< int > h1(
+ tsk::async(
+ boost::move( t1),
+ tsk::as_sub_task() ) ) ;
+ tsk::handle< int > h2(
+ tsk::async(
+ boost::move( t2),
+ tsk::as_sub_task() ) );
+ return h1.get() + h2.get();
+ }
+}
+
+inline
+void submit(
+ tsk::unbounded_buffer< int > & send,
+ tsk::unbounded_buffer< std::pair< int , int > > & recv,
+ int n)
+{
+ send.put( n);
+ boost::optional< std::pair< int , int > > r;
+ recv.take( r);
+ BOOST_ASSERT( r);
+ printf("fib(%d) == %d\n", r->first, r->second);
+}
+
+inline
+void calculate(
+ tsk::unbounded_buffer< int > & recv,
+ tsk::unbounded_buffer< std::pair< int , int > > & send)
+{
+ boost::optional< int > n;
+ recv.take( n);
+ BOOST_ASSERT( n);
+ int r = parallel_fib( * n, 5);
+ send.put( std::make_pair( * n, r) );
+}
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_type pool( tsk::poolsize( 3) );
+
+ tsk::unbounded_buffer< int > buf1;
+ tsk::unbounded_buffer< std::pair< int , int > > buf2;
+
+ tsk::handle< void > h1(
+ tsk::async(
+ tsk::make_task( submit, buf1, buf2, 5),
+ tsk::new_thread() ) );
+ tsk::handle< void > h2(
+ tsk::async(
+ tsk::make_task( submit, buf1, buf2, 10),
+ tsk::new_thread() ) );
+ tsk::handle< void > h3(
+ tsk::async(
+ tsk::make_task( submit, buf1, buf2, 15),
+ tsk::new_thread() ) );
+
+ tsk::async(
+ tsk::make_task( calculate, buf1, buf2),
+ pool);
+ tsk::async(
+ tsk::make_task( calculate, buf1, buf2),
+ pool);
+ tsk::async(
+ tsk::make_task( calculate, buf1, buf2),
+ pool);
+
+ h1.get();
+ h2.get();
+ h3.get();
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/buffer_multi2.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/buffer_multi2.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,120 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+#include <utility>
+
+#include <boost/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/optional.hpp>
+#include <boost/thread.hpp>
+
+#include "boost/task.hpp"
+
+namespace tsk = boost::task;
+
+typedef tsk::static_pool< tsk::unbounded_channel< tsk::fifo > > pool_type;
+
+int serial_fib( int n)
+{
+ if( n < 2)
+ return n;
+ else
+ return serial_fib( n - 1) + serial_fib( n - 2);
+}
+
+int parallel_fib( int n, int cutof)
+{
+ if ( n < cutof) return serial_fib( n);
+ else
+ {
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+ tsk::task< int > t1(
+ parallel_fib,
+ n - 1,
+ cutof);
+ tsk::task< int > t2(
+ parallel_fib,
+ n - 2,
+ cutof);
+ tsk::handle< int > h1(
+ tsk::async(
+ boost::move( t1),
+ tsk::as_sub_task() ) ) ;
+ tsk::handle< int > h2(
+ tsk::async(
+ boost::move( t2),
+ tsk::as_sub_task() ) );
+ return h1.get() + h2.get();
+ }
+}
+
+inline
+void submit(
+ tsk::unbounded_buffer< int > & send,
+ tsk::unbounded_buffer< std::pair< int , int > > & recv,
+ int n)
+{
+ send.put( n);
+ boost::optional< std::pair< int , int > > r;
+ recv.take( r);
+ BOOST_ASSERT( r);
+ printf("fib(%d) == %d\n", r->first, r->second);
+}
+
+inline
+void calculate(
+ tsk::unbounded_buffer< int > & recv,
+ tsk::unbounded_buffer< std::pair< int , int > > & send)
+{
+ boost::optional< int > n;
+ recv.take( n);
+ BOOST_ASSERT( n);
+ int r = parallel_fib( * n, 5);
+ send.put( std::make_pair( * n, r) );
+}
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_type pool( tsk::poolsize( 2) );
+
+ tsk::unbounded_buffer< int > buf1;
+ tsk::unbounded_buffer< std::pair< int , int > > buf2;
+
+ tsk::async(
+ tsk::make_task( submit, buf1, buf2, 5),
+ pool);
+ tsk::async(
+ tsk::make_task( submit, buf1, buf2, 10),
+ pool);
+ tsk::async(
+ tsk::make_task( submit, buf1, buf2, 15),
+ pool);
+
+ tsk::async(
+ tsk::make_task( calculate, buf1, buf2),
+ pool);
+ tsk::async(
+ tsk::make_task( calculate, buf1, buf2),
+ pool);
+ tsk::async(
+ tsk::make_task( calculate, buf1, buf2),
+ pool);
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/buffer_pool.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/buffer_pool.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,50 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+#include <string>
+
+#include "boost/task.hpp"
+#include "buffer.hpp"
+
+namespace tsk = boost::task;
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+ > pool( tsk::poolsize( 1) );
+
+ int n = 5;
+ tsk::unbounded_buffer< std::string > buf_ping, buf_pong;
+ tsk::async(
+ tsk::make_task(
+ & send_ping,
+ n,
+ buf_ping,
+ buf_pong),
+ pool);
+ tsk::async(
+ tsk::make_task(
+ & send_pong,
+ n,
+ buf_ping,
+ buf_pong),
+ pool);
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/buffer_pool_thread.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/buffer_pool_thread.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,51 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include "boost/task.hpp"
+#include "buffer.hpp"
+
+namespace tsk = boost::task;
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+ > pool( tsk::poolsize( 1) );
+
+ int n = 10;
+ tsk::unbounded_buffer< std::string > buf_ping, buf_pong;
+ tsk::async(
+ tsk::make_task(
+ & send_ping,
+ n,
+ buf_ping,
+ buf_pong),
+ pool);
+ tsk::handle< void > h = tsk::async(
+ tsk::make_task(
+ & send_pong,
+ n,
+ buf_ping,
+ buf_pong),
+ tsk::new_thread() );
+ h.wait();
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/buffer_thread.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/buffer_thread.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,48 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include "boost/task.hpp"
+#include "buffer.hpp"
+
+namespace tsk = boost::task;
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ int n = 10;
+ tsk::unbounded_buffer< std::string > buf_ping, buf_pong;
+ tsk::handle< void > h1 = tsk::async(
+ tsk::make_task(
+ & send_ping,
+ n,
+ buf_ping,
+ buf_pong),
+ tsk::new_thread() );
+ tsk::handle< void > h2 = tsk::async(
+ tsk::make_task(
+ & send_pong,
+ n,
+ buf_ping,
+ buf_pong),
+ tsk::new_thread() );
+ h1.wait();
+ h2.wait();
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/no_deadlock_pool.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/no_deadlock_pool.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,141 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include <boost/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/utility.hpp>
+
+#include "boost/task.hpp"
+
+namespace tsk = boost::task;
+
+typedef tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+> pool_type;
+
+class event
+{
+private:
+ class base : private boost::noncopyable
+ {
+ private:
+ tsk::semaphore sem_;
+
+ public:
+ base()
+ : sem_( 0)
+ {}
+
+ void set()
+ { sem_.post(); }
+
+ void wait()
+ {
+ if ( boost::this_task::runs_in_pool() )
+ {
+ while ( sem_.value() == 0)
+ boost::this_task::block();
+ }
+ else
+ {
+ sem_.wait();
+ sem_.post();
+ }
+ }
+ };
+
+ boost::shared_ptr< base > impl_;
+
+public:
+ event()
+ : impl_( new base)
+ {}
+
+ void set()
+ { impl_->set(); }
+
+ void wait()
+ { impl_->wait(); }
+};
+
+void sub_task(
+ int i,
+ int n,
+ event inner_ev)
+{
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+
+ fprintf( stderr, "t%d running ...\n", i);
+
+ if ( i == n - 1)
+ inner_ev.set();
+ else
+ inner_ev.wait();
+
+ fprintf( stderr, "t%d finished ...\n", i);
+}
+
+void main_task(
+ pool_type & pool,
+ int n,
+ event outer_ev)
+{
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+
+ fprintf( stderr, "main-task running %d sub-tasks\n", n);
+
+ event inner_ev;
+
+ for ( int i = 0; i < n; ++i)
+ tsk::async(
+ tsk::make_task(
+ & sub_task,
+ i,
+ n,
+ inner_ev),
+ pool);
+
+ inner_ev.wait();
+ outer_ev.set();
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_type pool(
+ tsk::poolsize(
+ boost::thread::hardware_concurrency() ) );
+
+ int n = 32;
+ event outer_ev;
+ tsk::async(
+ tsk::make_task(
+ & main_task,
+ boost::ref( pool),
+ n,
+ outer_ev),
+ pool);
+
+ fprintf( stderr, "main thread: waiting for t0 to finish\n");
+ outer_ev.wait();
+ fprintf( stderr, "main thread: t0 finished\n");
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/no_deadlock_pool2.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/no_deadlock_pool2.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,67 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+#include <vector>
+
+#include "boost/task.hpp"
+
+namespace tsk = boost::task;
+
+void sub_task( int i)
+{
+ fprintf( stderr, "t%d running ...\n", i);
+ fprintf( stderr, "t%d finished ...\n", i);
+}
+
+void main_task( int n)
+{
+ std::vector< tsk::handle< void > > vec;
+ for ( int i = 0; i < n; ++i)
+ vec.push_back(
+ tsk::async(
+ tsk::make_task(
+ & sub_task,
+ i),
+ tsk::as_sub_task() ) );
+ tsk::waitfor_all( vec.begin(), vec.end() );
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+ > pool(
+ tsk::poolsize(
+ boost::thread::hardware_concurrency() ) );
+
+ fprintf( stderr, "pool-size == %d\n", pool.size() );
+
+ int n = 32;
+ tsk::handle< void > h(
+ tsk::async(
+ tsk::make_task(
+ & main_task,
+ n),
+ pool) );
+
+ fprintf( stderr, "main thread: waiting for t0 to finish\n");
+ h.wait();
+ fprintf( stderr, "main thread: t0 finished\n");
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/no_deadlock_pool3.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/no_deadlock_pool3.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,141 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include <boost/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/utility.hpp>
+
+#include "boost/task.hpp"
+
+namespace tsk = boost::task;
+
+typedef tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+> pool_type;
+
+class event
+{
+private:
+ class base : private boost::noncopyable
+ {
+ private:
+ tsk::semaphore sem_;
+
+ public:
+ base()
+ : sem_( 0)
+ {}
+
+ void set()
+ { sem_.post(); }
+
+ void wait()
+ {
+ if ( boost::this_task::runs_in_pool() )
+ {
+ while ( sem_.value() == 0)
+ boost::this_task::block();
+ }
+ else
+ {
+ sem_.wait();
+ sem_.post();
+ }
+ }
+ };
+
+ boost::shared_ptr< base > impl_;
+
+public:
+ event()
+ : impl_( new base)
+ {}
+
+ void set()
+ { impl_->set(); }
+
+ void wait()
+ { impl_->wait(); }
+};
+
+void sub_task(
+ int i,
+ int n,
+ event inner_ev)
+{
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+
+ fprintf( stderr, "t%d running ...\n", i);
+
+ if ( i == n - 1)
+ inner_ev.set();
+ else
+ inner_ev.wait();
+
+ fprintf( stderr, "t%d finished ...\n", i);
+}
+
+void main_task(
+ pool_type & pool,
+ int n,
+ event outer_ev)
+{
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+
+ fprintf( stderr, "main-task running %d sub-tasks\n", n);
+
+ event inner_ev;
+
+ for ( int i = 0; i < n; ++i)
+ tsk::async(
+ tsk::make_task(
+ & sub_task,
+ i,
+ n,
+ inner_ev),
+ tsk::as_sub_task() );
+
+ inner_ev.wait();
+ outer_ev.set();
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_type pool(
+ tsk::poolsize(
+ boost::thread::hardware_concurrency() ) );
+
+ int n = 32;
+ event outer_ev;
+ tsk::async(
+ tsk::make_task(
+ & main_task,
+ boost::ref( pool),
+ n,
+ outer_ev),
+ pool);
+
+ fprintf( stderr, "main thread: waiting for t0 to finish\n");
+ outer_ev.wait();
+ fprintf( stderr, "main thread: t0 finished\n");
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/semaphore_pool.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/semaphore_pool.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,56 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+
+#include "boost/task.hpp"
+#include "semaphore.hpp"
+
+namespace tsk = boost::task;
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+ > pool( tsk::poolsize( 1) );
+
+ int n = 10;
+ tsk::semaphore sem_a( 0), sem_b( 0);
+ tsk::async(
+ tsk::make_task(
+ & start_with_sem_a,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ pool);
+ tsk::async(
+ tsk::make_task( & loop_fn, n),
+ pool);
+ tsk::async(
+ tsk::make_task(
+ & start_with_sem_b,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ pool);
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/semaphore_pool_thread.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/semaphore_pool_thread.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,57 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+
+#include "boost/task.hpp"
+#include "semaphore.hpp"
+
+namespace tsk = boost::task;
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tsk::static_pool<
+ tsk::unbounded_channel< tsk::fifo >
+ > pool( tsk::poolsize( 1) );
+
+ int n = 10;
+ tsk::semaphore sem_a( 0), sem_b( 0);
+ tsk::async(
+ tsk::make_task(
+ & start_with_sem_a,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ pool);
+ tsk::async(
+ tsk::make_task( & loop_fn, n),
+ pool);
+ tsk::handle< void > h = tsk::async(
+ tsk::make_task(
+ & start_with_sem_b,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ tsk::new_thread() );
+ h.get();
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}

Added: sandbox/task/libs/task/examples/semaphore_thread.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/semaphore_thread.cpp 2009-08-16 05:21:07 EDT (Sun, 16 Aug 2009)
@@ -0,0 +1,51 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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 <cstdlib>
+#include <stdexcept>
+
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+
+#include "boost/task.hpp"
+#include "semaphore.hpp"
+
+namespace tsk = boost::task;
+
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ int n = 10;
+ tsk::semaphore sem_a( 0), sem_b( 0);
+ tsk::handle< void > h1 = tsk::async(
+ tsk::make_task(
+ & start_with_sem_a,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ tsk::new_thread() );
+ tsk::handle< void > h2 = tsk::async(
+ tsk::make_task(
+ & start_with_sem_b,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ tsk::new_thread() );
+ h1.get();
+ h2.get();
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}


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