|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r57021 - in sandbox/task: boost/task libs/task/examples libs/task/examples/sync
From: oliver.kowalke_at_[hidden]
Date: 2009-10-20 15:40:19
Author: olli
Date: 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
New Revision: 57021
URL: http://svn.boost.org/trac/boost/changeset/57021
Log:
- correction related to waitfor_all
- examples restructured
Added:
sandbox/task/libs/task/examples/sub_tasks.cpp (contents, props changed)
sandbox/task/libs/task/examples/sync/
sandbox/task/libs/task/examples/sync/buffer_ping_pong.cpp (contents, props changed)
sandbox/task/libs/task/examples/sync/fork_join_event.cpp (contents, props changed)
sandbox/task/libs/task/examples/sync/message_passing.cpp (contents, props changed)
sandbox/task/libs/task/examples/sync/semaphore_ping_pong.cpp (contents, props changed)
Removed:
sandbox/task/libs/task/examples/buffer.hpp
sandbox/task/libs/task/examples/buffer_multi.cpp
sandbox/task/libs/task/examples/buffer_multi2.cpp
sandbox/task/libs/task/examples/buffer_pool.cpp
sandbox/task/libs/task/examples/buffer_pool_thread.cpp
sandbox/task/libs/task/examples/buffer_thread.cpp
sandbox/task/libs/task/examples/fork_join.cpp
sandbox/task/libs/task/examples/no_deadlock_pool.cpp
sandbox/task/libs/task/examples/no_deadlock_pool2.cpp
sandbox/task/libs/task/examples/no_deadlock_pool3.cpp
sandbox/task/libs/task/examples/semaphore.hpp
sandbox/task/libs/task/examples/semaphore_pool.cpp
sandbox/task/libs/task/examples/semaphore_pool_thread.cpp
sandbox/task/libs/task/examples/semaphore_thread.cpp
Text files modified:
sandbox/task/boost/task/handle.hpp | 43 ++++++++++++++++++++++++++-------------
sandbox/task/libs/task/examples/Jamfile.v2 | 17 ++++-----------
2 files changed, 33 insertions(+), 27 deletions(-)
Modified: sandbox/task/boost/task/handle.hpp
==============================================================================
--- sandbox/task/boost/task/handle.hpp (original)
+++ sandbox/task/boost/task/handle.hpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -3,12 +3,15 @@
// 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)
+//
+// parts are based on boost.future by Anthony Williams
#ifndef BOOST_TASK_HANDLE_H
#define BOOST_TASK_HANDLE_H
#include <boost/thread.hpp>
#include <boost/thread/thread_time.hpp>
+#include <boost/utility/enable_if.hpp>
#include <boost/task/context.hpp>
#include <boost/task/future.hpp>
@@ -131,8 +134,18 @@
}
};
+template< typename T >
+struct is_handle_type
+{ BOOST_STATIC_CONSTANT( bool, value = false); };
+
+template< typename T >
+struct is_handle_type< handle< T > >
+{
+ BOOST_STATIC_CONSTANT( bool, value = true);
+};
+
template< typename Iterator >
-void waitfor_all( Iterator begin, Iterator end)
+typename disable_if< is_handle_type< Iterator >, void >::type waitfor_all( Iterator begin, Iterator end)
{
try
{
@@ -144,10 +157,10 @@
}
template< typename T1, typename T2 >
-void waitfor_all( T1 & t1, T2 & t2)
+void waitfor_all( handle< T1 > & t1, handle< T2 > & t2)
{
try
- { wait_for_all( t1.fut_, t2.fut_); }
+ { wait_for_all( t1.get_future(), t2.get_future()); }
catch ( thread_interrupted const&)
{ throw task_interrupted(); }
}
@@ -156,7 +169,7 @@
void waitfor_all( handle< T1 > & t1, handle< T2 > & t2, handle< T3 > & t3)
{
try
- { wait_for_all( t1.fut_, t2.fut_, t3.fut_); }
+ { wait_for_all( t1.get_future(), t2.get_future(), t3.get_future()); }
catch ( thread_interrupted const&)
{ throw task_interrupted(); }
}
@@ -165,7 +178,7 @@
void waitfor_all( handle< T1 > & t1, handle< T2 > & t2, handle< T3 > & t3, handle< T4 > & t4)
{
try
- { wait_for_all( t1.fut_, t2.fut_, t3.fut_, t4.fut_); }
+ { wait_for_all( t1.get_future(), t2.get_future(), t3.get_future(), t4.get_future()); }
catch ( thread_interrupted const&)
{ throw task_interrupted(); }
}
@@ -174,7 +187,16 @@
void waitfor_all( handle< T1 > & t1, handle< T2 > & t2, handle< T3 > & t3, handle< T4 > & t4, handle< T5 > & t5)
{
try
- { wait_for_all( t1.fut_, t2.fut_, t3.fut_, t4.fut_, t5.fut_); }
+ { wait_for_all( t1.get_future(), t2.get_future(), t3.get_future(), t4.get_future(), t5.get_future()); }
+ catch ( thread_interrupted const&)
+ { throw task_interrupted(); }
+}
+
+template< typename T1, typename T2 >
+unsigned int waitfor_any( handle< T1 > & t1, handle< T2 > & t2)
+{
+ try
+ { return wait_for_any( t1.get_future(), t2.get_future() ); }
catch ( thread_interrupted const&)
{ throw task_interrupted(); }
}
@@ -193,15 +215,6 @@
{ throw task_interrupted(); }
}
-template< typename T1, typename T2 >
-unsigned int waitfor_any( handle< T1 > & t1, handle< T2 > & t2)
-{
- try
- { return wait_for_any( t1.get_future(), t2.get_future() ); }
- catch ( thread_interrupted const&)
- { throw task_interrupted(); }
-}
-
template< typename T1, typename T2, typename T3 >
unsigned int waitfor_any( handle< T1 > & t1, handle< T2 > & t2, handle< T3 > & t3)
{
Modified: sandbox/task/libs/task/examples/Jamfile.v2
==============================================================================
--- sandbox/task/libs/task/examples/Jamfile.v2 (original)
+++ sandbox/task/libs/task/examples/Jamfile.v2 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -25,20 +25,13 @@
;
exe bind_to_processors : bind_to_processors.cpp ;
-exe buffer_multi : buffer_multi.cpp ;
-exe buffer_multi2 : buffer_multi2.cpp ;
-exe buffer_pool : buffer_pool.cpp ;
-exe buffer_pool_thread : buffer_pool_thread.cpp ;
-exe buffer_thread : buffer_thread.cpp ;
-exe fork_join : fork_join.cpp ;
+exe sub_tasks : sub_tasks.cpp ;
exe interrupt : interrupt.cpp ;
-exe no_deadlock_pool : no_deadlock_pool.cpp ;
-exe no_deadlock_pool2 : no_deadlock_pool2.cpp ;
-exe no_deadlock_pool3 : no_deadlock_pool3.cpp ;
exe priority : priority.cpp ;
-exe semaphore_thread : semaphore_thread.cpp ;
-exe semaphore_pool : semaphore_pool.cpp ;
-exe semaphore_pool_thread : semaphore_pool_thread.cpp ;
exe shutdown_now : shutdown_now.cpp ;
exe smart : smart.cpp ;
exe submit : submit.cpp ;
+exe sync/fork_join_event : sync/fork_join_event.cpp ;
+exe sync/buffer_ping_pong : sync/buffer_ping_pong.cpp ;
+exe sync/semaphore_ping_pong : sync/semaphore_ping_pong.cpp ;
+exe sync/message_passing : sync/message_passing.cpp ;
Deleted: sandbox/task/libs/task/examples/buffer.hpp
==============================================================================
--- sandbox/task/libs/task/examples/buffer.hpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,59 +0,0 @@
-
-// 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_EXAMPLES_BUFFER_H
-#define BOOST_TASK_EXAMPLES_BUFFER_H
-
-#include <cstdlib>
-#include <string>
-
-#include <boost/assert.hpp>
-#include <boost/lexical_cast.hpp>
-#include <boost/optional.hpp>
-
-#include "boost/task.hpp"
-
-namespace tsk = boost::task;
-
-inline
-void send_ping(
- int n,
- tsk::unbounded_buffer< std::string > & buf_ping,
- tsk::unbounded_buffer< std::string > & buf_pong)
-{
- for ( int i = 0; i < n; ++i)
- {
- printf("task 1: buf_ping.put(%d-th ping)\n", i);
- buf_ping.put(
- boost::lexical_cast< std::string >( i) + "-th ping");
- boost::optional< std::string > msg;
- buf_pong.take( msg);
- BOOST_ASSERT( msg);
- printf("task 1: buf_pong.take(%s)\n", msg->c_str() );
- }
- printf("task 1: end\n");
-}
-
-inline
-void send_pong(
- int n,
- tsk::unbounded_buffer< std::string > & buf_ping,
- tsk::unbounded_buffer< std::string > & buf_pong)
-{
- for ( int i = 0; i < n; ++i)
- {
- printf("task 2: buf_pong.put(%d-th pong)\n", i);
- buf_pong.put(
- boost::lexical_cast< std::string >( i) + "-th pong");
- boost::optional< std::string > msg;
- buf_ping.take( msg);
- BOOST_ASSERT( msg);
- printf("task 2: buf_ping.take(%s)\n", msg->c_str() );
- }
- printf("task 2: end\n");
-}
-
-#endif // BOOST_TASK_EXAMPLES_BUFFER_H
Deleted: sandbox/task/libs/task/examples/buffer_multi.cpp
==============================================================================
--- sandbox/task/libs/task/examples/buffer_multi.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,118 +0,0 @@
-
-// 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_twolock_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)
-{
- for ( int i = 0; i <= n; ++i)
- send.put( i);
- send.deactivate();
- boost::optional< std::pair< int , int > > r;
- while ( 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;
- while ( recv.take( n) )
- {
- BOOST_ASSERT( n);
- int r = parallel_fib( * n, 5);
- send.put( std::make_pair( * n, r) );
- }
- send.deactivate();
-}
-
-
-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, 15),
- tsk::new_thread() ) );
-
- tsk::async(
- tsk::make_task( calculate, buf1, buf2),
- pool);
-
- h1.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;
-}
Deleted: sandbox/task/libs/task/examples/buffer_multi2.cpp
==============================================================================
--- sandbox/task/libs/task/examples/buffer_multi2.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,113 +0,0 @@
-
-// 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_twolock_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)
-{
- for ( int i = 0; i <= n; ++i)
- send.put( i);
- send.deactivate();
- boost::optional< std::pair< int , int > > r;
- while ( 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;
- while ( recv.take( n) )
- {
- BOOST_ASSERT( n);
- int r = parallel_fib( * n, 5);
- send.put( std::make_pair( * n, r) );
- }
- send.deactivate();
-}
-
-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, 15),
- 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;
-}
Deleted: sandbox/task/libs/task/examples/buffer_pool.cpp
==============================================================================
--- sandbox/task/libs/task/examples/buffer_pool.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,48 +0,0 @@
-
-// 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_twolock_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;
-}
Deleted: sandbox/task/libs/task/examples/buffer_pool_thread.cpp
==============================================================================
--- sandbox/task/libs/task/examples/buffer_pool_thread.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,49 +0,0 @@
-
-// 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_twolock_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;
-}
Deleted: sandbox/task/libs/task/examples/buffer_thread.cpp
==============================================================================
--- sandbox/task/libs/task/examples/buffer_thread.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,48 +0,0 @@
-
-// 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;
-}
Deleted: sandbox/task/libs/task/examples/fork_join.cpp
==============================================================================
--- sandbox/task/libs/task/examples/fork_join.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,100 +0,0 @@
-
-// 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/bind.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
-
-#include "boost/task.hpp"
-
-namespace pt = boost::posix_time;
-namespace tsk = boost::task;
-
-typedef tsk::static_pool< tsk::unbounded_twolock_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();
- }
-}
-
-int main( int argc, char *argv[])
-{
- try
- {
- pool_type pool( tsk::poolsize( 5) );
-
- std::vector< tsk::handle< int > > results;
- results.reserve( 10);
-
- pt::ptime start( pt::microsec_clock::universal_time() );
-
- for ( int i = 0; i < 10; ++i)
- {
- tsk::task< int > t(
- & parallel_fib,
- i,
- 5);
- results.push_back(
- tsk::async(
- boost::move( t),
- pool) );
- }
-
- tsk::waitfor_all( results.begin(), results.end() );
-
- int k = 0;
- std::vector< tsk::handle< int > >::iterator e( results.end() );
- for (
- std::vector< tsk::handle< int > >::iterator i( results.begin() );
- i != e;
- ++i)
- std::cout << "fibonacci(" << k++ << ") == " << i->get() << std::endl;
-
- pt::ptime stop( pt::microsec_clock::universal_time() );
- std::cout << ( stop - start).total_milliseconds() << " milli seconds" << std::endl;
-
- return EXIT_SUCCESS;
- }
- catch ( std::exception const& e)
- { std::cerr << "exception: " << e.what() << std::endl; }
- catch ( ... )
- { std::cerr << "unhandled" << std::endl; }
-
- return EXIT_FAILURE;
-}
Deleted: sandbox/task/libs/task/examples/no_deadlock_pool.cpp
==============================================================================
--- sandbox/task/libs/task/examples/no_deadlock_pool.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,138 +0,0 @@
-
-// 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_twolock_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
- {
- tsk::poolsize psize( boost::thread::hardware_concurrency() );
- pool_type pool( psize);
-
- 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;
-}
Deleted: sandbox/task/libs/task/examples/no_deadlock_pool2.cpp
==============================================================================
--- sandbox/task/libs/task/examples/no_deadlock_pool2.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,65 +0,0 @@
-
-// 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/thread.hpp>
-#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::poolsize psize( boost::thread::hardware_concurrency() );
- tsk::static_pool< tsk::unbounded_twolock_fifo > pool( psize);
-
- 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;
-}
Deleted: sandbox/task/libs/task/examples/no_deadlock_pool3.cpp
==============================================================================
--- sandbox/task/libs/task/examples/no_deadlock_pool3.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,87 +0,0 @@
-
-// 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_twolock_fifo > pool_type;
-
-void sub_task( int i, int n, tsk::spin_count_down_event & ev)
-{
- BOOST_ASSERT( boost::this_task::runs_in_pool() );
-
- fprintf( stderr, "t%d running ...\n", i);
-
- ev.set();
-
- fprintf( stderr, "t%d finished ...\n", i);
-}
-
-void main_task(
- pool_type & pool,
- int n,
- tsk::spin_count_down_event & outer_ev)
-{
- BOOST_ASSERT( boost::this_task::runs_in_pool() );
-
- fprintf( stderr, "main-task running %d sub-tasks\n", n);
-
- tsk::spin_count_down_event inner_ev( n);
-
- for ( int i = 0; i < n; ++i)
- tsk::async(
- tsk::make_task(
- & sub_task,
- i,
- n,
- boost::ref( inner_ev) ),
- tsk::as_sub_task() );
-
- inner_ev.wait();
- outer_ev.set();
-}
-
-int main( int argc, char *argv[])
-{
- try
- {
- tsk::poolsize psize( boost::thread::hardware_concurrency() );
- pool_type pool( psize);
-
- int n = 32;
- tsk::spin_count_down_event ev( 1);
- tsk::async(
- tsk::make_task(
- & main_task,
- boost::ref( pool),
- n,
- boost::ref( ev) ),
- pool);
-
- fprintf( stderr, "main thread: waiting for t0 to finish\n");
- 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;
-}
Deleted: sandbox/task/libs/task/examples/semaphore.hpp
==============================================================================
--- sandbox/task/libs/task/examples/semaphore.hpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,64 +0,0 @@
-
-// 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_EXAMPLES_SEMAPHORE_H
-#define BOOST_TASK_EXAMPLES_SEMAPHORE_H
-
-#include <iostream>
-#include <cstdlib>
-#include <stdexcept>
-
-#include <boost/bind.hpp>
-#include <boost/ref.hpp>
-
-#include "boost/task.hpp"
-
-namespace tsk = boost::task;
-
-inline
-void loop_fn( int n)
-{
- for ( int i = 0; i < n; ++i)
- {
- printf("task 3: %d-th iteration\n", i);
- boost::this_task::block();
- }
- printf("task 3: end\n");
-}
-
-inline
-void start_with_sem_a(
- int n,
- tsk::semaphore & sem_a,
- tsk::semaphore & sem_b)
-{
- for ( int i = 0; i < n; ++i)
- {
- printf("task 1: %d-th sema_a.post()\n", i);
- sem_a.post();
- printf("task 1: %d-th sema_b.wait()\n", i);
- sem_b.wait();
- }
- printf("task 1: end\n");
-}
-
-inline
-void start_with_sem_b(
- int n,
- tsk::semaphore & sem_a,
- tsk::semaphore & sem_b)
-{
- for ( int i = 0; i < n; ++i)
- {
- printf("task 2: %d-th sema_b.post()\n", i);
- sem_b.post();
- printf("task 2: %d-th sema_a.wait()\n", i);
- sem_a.wait();
- }
- printf("task 2: end\n");
-}
-
-#endif // BOOST_TASK_EXAMPLES_SEMAPHORE_H
Deleted: sandbox/task/libs/task/examples/semaphore_pool.cpp
==============================================================================
--- sandbox/task/libs/task/examples/semaphore_pool.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,54 +0,0 @@
-
-// 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_twolock_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;
-}
Deleted: sandbox/task/libs/task/examples/semaphore_pool_thread.cpp
==============================================================================
--- sandbox/task/libs/task/examples/semaphore_pool_thread.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,55 +0,0 @@
-
-// 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_twolock_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;
-}
Deleted: sandbox/task/libs/task/examples/semaphore_thread.cpp
==============================================================================
--- sandbox/task/libs/task/examples/semaphore_thread.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
+++ (empty file)
@@ -1,51 +0,0 @@
-
-// 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;
-}
Added: sandbox/task/libs/task/examples/sub_tasks.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/sub_tasks.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -0,0 +1,100 @@
+
+// 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/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+#include "boost/task.hpp"
+
+namespace pt = boost::posix_time;
+namespace tsk = boost::task;
+
+typedef tsk::static_pool< tsk::unbounded_twolock_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();
+ }
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_type pool( tsk::poolsize( 5) );
+
+ std::vector< tsk::handle< int > > results;
+ results.reserve( 10);
+
+ pt::ptime start( pt::microsec_clock::universal_time() );
+
+ for ( int i = 0; i < 10; ++i)
+ {
+ tsk::task< int > t(
+ & parallel_fib,
+ i,
+ 5);
+ results.push_back(
+ tsk::async(
+ boost::move( t),
+ pool) );
+ }
+
+ tsk::waitfor_all( results.begin(), results.end() );
+
+ int k = 0;
+ std::vector< tsk::handle< int > >::iterator e( results.end() );
+ for (
+ std::vector< tsk::handle< int > >::iterator i( results.begin() );
+ i != e;
+ ++i)
+ std::cout << "fibonacci(" << k++ << ") == " << i->get() << std::endl;
+
+ pt::ptime stop( pt::microsec_clock::universal_time() );
+ std::cout << ( stop - start).total_milliseconds() << " milli seconds" << std::endl;
+
+ 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/sync/buffer_ping_pong.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/sync/buffer_ping_pong.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -0,0 +1,142 @@
+
+// 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/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/optional.hpp>
+#include <boost/ref.hpp>
+
+#include "boost/task.hpp"
+
+namespace tsk = boost::task;
+
+void send_ping(
+ int n,
+ tsk::unbounded_buffer< std::string > & buf_ping,
+ tsk::unbounded_buffer< std::string > & buf_pong)
+{
+ for ( int i = 0; i < n; ++i)
+ {
+ printf("task 1: buf_ping.put(%d-th ping)\n", i);
+ buf_ping.put(
+ boost::lexical_cast< std::string >( i) + "-th ping");
+ boost::optional< std::string > msg;
+ buf_pong.take( msg);
+ BOOST_ASSERT( msg);
+ printf("task 1: buf_pong.take(%s)\n", msg->c_str() );
+ }
+ printf("task 1: end\n");
+}
+
+void send_pong(
+ int n,
+ tsk::unbounded_buffer< std::string > & buf_ping,
+ tsk::unbounded_buffer< std::string > & buf_pong)
+{
+ for ( int i = 0; i < n; ++i)
+ {
+ printf("task 2: buf_pong.put(%d-th pong)\n", i);
+ buf_pong.put(
+ boost::lexical_cast< std::string >( i) + "-th pong");
+ boost::optional< std::string > msg;
+ buf_ping.take( msg);
+ BOOST_ASSERT( msg);
+ printf("task 2: buf_ping.take(%s)\n", msg->c_str() );
+ }
+ printf("task 2: end\n");
+}
+
+void thread_only()
+{
+ int n = 5;
+ 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() );
+
+ tsk::waitfor_all( h1, h2);
+}
+
+void pool_only()
+{
+ tsk::static_pool< tsk::unbounded_twolock_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);
+}
+
+void pool_and_thread()
+{
+ tsk::static_pool< tsk::unbounded_twolock_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::handle< void > h = tsk::async(
+ tsk::make_task(
+ & send_pong,
+ n,
+ buf_ping,
+ buf_pong),
+ tsk::new_thread() );
+
+ h.wait();
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_only();
+ thread_only();
+ pool_and_thread();
+
+ 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/sync/fork_join_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/sync/fork_join_event.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -0,0 +1,87 @@
+
+// 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_twolock_fifo > pool_type;
+
+void sub_task( int i, int n, tsk::spin_count_down_event & ev)
+{
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+
+ fprintf( stderr, "t%d running ...\n", i);
+
+ ev.set();
+
+ fprintf( stderr, "t%d finished ...\n", i);
+}
+
+void main_task(
+ pool_type & pool,
+ int n,
+ tsk::spin_count_down_event & outer_ev)
+{
+ BOOST_ASSERT( boost::this_task::runs_in_pool() );
+
+ fprintf( stderr, "main-task running %d sub-tasks\n", n);
+
+ tsk::spin_count_down_event inner_ev( n);
+
+ for ( int i = 0; i < n; ++i)
+ tsk::async(
+ tsk::make_task(
+ & sub_task,
+ i,
+ n,
+ boost::ref( inner_ev) ),
+ tsk::as_sub_task() );
+
+ inner_ev.wait();
+ outer_ev.set();
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tsk::poolsize psize( boost::thread::hardware_concurrency() );
+ pool_type pool( psize);
+
+ int n = 32;
+ tsk::spin_count_down_event ev( 1);
+ tsk::async(
+ tsk::make_task(
+ & main_task,
+ boost::ref( pool),
+ n,
+ boost::ref( ev) ),
+ pool);
+
+ fprintf( stderr, "main thread: waiting for t0 to finish\n");
+ 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/sync/message_passing.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/sync/message_passing.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -0,0 +1,113 @@
+
+// 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_twolock_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)
+{
+ for ( int i = 0; i <= n; ++i)
+ send.put( i);
+ send.deactivate();
+ boost::optional< std::pair< int , int > > r;
+ while ( 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;
+ while ( recv.take( n) )
+ {
+ BOOST_ASSERT( n);
+ int r = parallel_fib( * n, 5);
+ send.put( std::make_pair( * n, r) );
+ }
+ send.deactivate();
+}
+
+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, 15),
+ 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/sync/semaphore_ping_pong.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/examples/sync/semaphore_ping_pong.cpp 2009-10-20 15:40:16 EDT (Tue, 20 Oct 2009)
@@ -0,0 +1,131 @@
+
+// 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"
+
+namespace tsk = boost::task;
+
+void start_with_sem_a(
+ int n,
+ tsk::semaphore & sem_a,
+ tsk::semaphore & sem_b)
+{
+ for ( int i = 0; i < n; ++i)
+ {
+ printf("task 1: %d-th sema_a.post()\n", i);
+ sem_a.post();
+ printf("task 1: %d-th sema_b.wait()\n", i);
+ sem_b.wait();
+ }
+ printf("task 1: end\n");
+}
+
+void start_with_sem_b(
+ int n,
+ tsk::semaphore & sem_a,
+ tsk::semaphore & sem_b)
+{
+ for ( int i = 0; i < n; ++i)
+ {
+ printf("task 2: %d-th sema_b.post()\n", i);
+ sem_b.post();
+ printf("task 2: %d-th sema_a.wait()\n", i);
+ sem_a.wait();
+ }
+ printf("task 2: end\n");
+}
+
+void thread_only()
+{
+ 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() );
+
+ tsk::waitfor_all( h1, h2);
+}
+
+void pool_only()
+{
+ tsk::static_pool< tsk::unbounded_twolock_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(
+ & start_with_sem_b,
+ n,
+ boost::ref( sem_a),
+ boost::ref( sem_b) ),
+ pool);
+}
+
+void pool_and_thread()
+{
+ tsk::static_pool< tsk::unbounded_twolock_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::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.wait();
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ pool_only();
+ thread_only();
+ pool_and_thread();
+ 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