|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r52366 - sandbox/threadpool/libs/tp/examples
From: oliver.kowalke_at_[hidden]
Date: 2009-04-13 03:39:34
Author: olli
Date: 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
New Revision: 52366
URL: http://svn.boost.org/trac/boost/changeset/52366
Log:
* some examples
Text files modified:
sandbox/threadpool/libs/tp/examples/fork_join.cpp | 2
sandbox/threadpool/libs/tp/examples/interrupt.cpp | 20 +--
sandbox/threadpool/libs/tp/examples/parallel_sort.cpp | 4
sandbox/threadpool/libs/tp/examples/pending.cpp | 16 --
sandbox/threadpool/libs/tp/examples/priority.cpp | 5
sandbox/threadpool/libs/tp/examples/reschedule_until.cpp | 204 ++++++++++++++++++++++-----------------
sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp | 7
sandbox/threadpool/libs/tp/examples/sleep.cpp | 4
sandbox/threadpool/libs/tp/examples/smart.cpp | 5
sandbox/threadpool/libs/tp/examples/submit.cpp | 12 -
sandbox/threadpool/libs/tp/examples/yield.cpp | 4
11 files changed, 140 insertions(+), 143 deletions(-)
Modified: sandbox/threadpool/libs/tp/examples/fork_join.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/fork_join.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/fork_join.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -69,7 +69,7 @@
pt::ptime start( pt::microsec_clock::universal_time() );
- for ( int i = 0; i < 26; ++i)
+ for ( int i = 0; i < 15; ++i)
results.push_back(
tp::get_default_pool().submit(
boost::bind(
Modified: sandbox/threadpool/libs/tp/examples/interrupt.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/interrupt.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/interrupt.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -6,10 +6,7 @@
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
-#include "boost/tp/fifo.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace pt = boost::posix_time;
namespace tp = boost::tp;
@@ -39,22 +36,19 @@
{
try
{
- tp::pool<
- tp::unbounded_channel< tp::fifo >
- > pool( tp::poolsize( 1) );
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
long_running_fn) );
- std::cout << "poolsize == " << pool.size() << std::endl;
- std::cout << "idle threads == " << pool.idle() << std::endl;
- std::cout << "active threads == " << pool.active() << std::endl;
+ std::cout << "poolsize == " << tp::get_default_pool().size() << std::endl;
+ std::cout << "idle threads == " << tp::get_default_pool().idle() << std::endl;
+ std::cout << "active threads == " << tp::get_default_pool().active() << std::endl;
tp::task< int > t(
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
fibonacci_fn,
10) ) );
t.interrupt();
- std::cout << t.result().get() << std::endl;
+ std::cout << t.get() << std::endl;
return EXIT_SUCCESS;
}
Modified: sandbox/threadpool/libs/tp/examples/parallel_sort.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/parallel_sort.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/parallel_sort.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -17,9 +17,7 @@
#include <boost/progress.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
-#include <boost/tp/pool.hpp>
-#include <boost/tp/unbounded_channel.hpp>
-#include <boost/tp/fifo.hpp>
+#include "boost/tp.hpp"
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/algorithm/inplace_merge.hpp>
#include <boost/range/sub_range.hpp>
Modified: sandbox/threadpool/libs/tp/examples/pending.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/pending.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/pending.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -7,10 +7,7 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/ref.hpp>
-#include "boost/tp/fifo.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace pt = boost::posix_time;
namespace tp = boost::tp;
@@ -40,19 +37,16 @@
{
try
{
- tp::pool<
- tp::unbounded_channel< tp::fifo >
- > pool( tp::poolsize( 1) );
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
long_running_fn) );
tp::task< int > t(
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
fibonacci_fn,
10) ) );
- std::cout << "pending tasks == " << pool.pending() << std::endl;
- std::cout << t.result().get() << std::endl;
+ std::cout << "pending tasks == " << tp::get_default_pool().pending() << std::endl;
+ std::cout << t.get() << std::endl;
return EXIT_SUCCESS;
}
Modified: sandbox/threadpool/libs/tp/examples/priority.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/priority.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/priority.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -6,10 +6,7 @@
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
-#include "boost/tp/priority.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace pt = boost::posix_time;
namespace tp = boost::tp;
Modified: sandbox/threadpool/libs/tp/examples/reschedule_until.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/reschedule_until.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/reschedule_until.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -1,126 +1,156 @@
+#include <cerrno>
+#include <cstddef>
+#include <cstring>
#include <iostream>
#include <cstdlib>
#include <stdexcept>
-#include <vector>
+#include <string>
+
+extern "C"
+{
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+}
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
-#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/thread.hpp>
-#include "boost/tp/fifo.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace pt = boost::posix_time;
namespace tp = boost::tp;
-typedef tp::pool< tp::unbounded_channel< tp::fifo > > pool_type;
+typedef tp::default_pool pool_type;
+
+long serial_fib( long n)
+{
+ if( n < 2)
+ return n;
+ else
+ return serial_fib( n - 1) + serial_fib( n - 2);
+}
-class fibo
+class fib_task
{
private:
- boost::shared_future< void > f_;
- int offset_;
+ long cutof_;
- class holder
+public:
+ fib_task( long cutof)
+ : cutof_( cutof)
+ {}
+
+ long execute( long n)
{
- private:
- boost::shared_future< void > f_;
+ if ( n < cutof_) return serial_fib( n);
+ else
+ {
+ tp::task< long > t1(
+ boost::this_task::get_thread_pool< pool_type >().submit(
+ boost::bind(
+ & fib_task::execute,
+ boost::ref( * this),
+ n - 1) ) );
+ tp::task< long > t2(
+ boost::this_task::get_thread_pool< pool_type >().submit(
+ boost::bind(
+ & fib_task::execute,
+ boost::ref( * this),
+ n - 2) ) );
+ return t1.get() + t2.get();
+ }
+ }
+};
- public:
- holder( boost::shared_future< void > const& f)
- : f_( f)
- {}
+void parallel_fib( long n)
+{
+ fib_task a( 5);
+ printf("%d -> %d\n", n, a.execute( n) );
+}
- bool operator()()
- { return f_.is_ready(); }
- };
+bool has_bytes( int fd)
+{
+ char buffer[1];
- int seq_( int n)
+ int n = ::recv(
+ fd,
+ & buffer,
+ sizeof buffer,
+ MSG_PEEK | MSG_DONTWAIT);
+ if ( n == -1 && errno != EWOULDBLOCK)
{
- if ( n <= 1) return n;
- else return seq_( n - 2) + seq_( n - 1);
+ printf("::recv() failed: %s(%d)\n", std::strerror( errno), errno);
+ ::exit( 1);
}
-
- int par_( int n)
+
+ return n > 0;
+}
+
+void read( int fd)
+{
+ int nread = 0;
+ do
{
- if ( n <= offset_) return seq_( n);
- else
+ boost::this_task::reschedule_until(
+ boost::bind(
+ has_bytes,
+ fd) );
+
+ char buffer[4096];
+ int n = ::read( fd, buffer, sizeof( buffer) );
+ if ( n < 0)
{
- if ( n == 7)
- {
- holder hldr( f_);
- boost::this_task::reschedule_until( hldr);
- }
-
- boost::function< int() > fn1 = boost::bind(
- & fibo::par_,
- * this,
- n - 1);
- tp::task< int > t1(
- boost::this_task::get_thread_pool< pool_type >().submit(
- fn1) );
- boost::function< int() > fn2 = boost::bind(
- & fibo::par_,
- * this,
- n - 2);
- tp::task< int > t2(
- boost::this_task::get_thread_pool< pool_type >().submit(
- fn2) );
-
- return t1.result().get() + t2.result().get();
+ printf("::read() failed: %s(%d)\n", std::strerror( errno), errno);
+ ::exit( 1);
}
+ nread += n;
+ printf("%s\n", std::string( buffer, n).c_str() );
}
+ while ( nread < 12);
+}
-public:
- fibo(
- boost::shared_future< void > f,
- int offset)
- : f_( f), offset_( offset)
- {}
-
- int execute( int n)
+void write( int fd, std::string const& msg)
+{
+ if ( ::write( fd, msg.c_str(), msg.size() ) < 0)
{
- int result( par_( n) );
- return result;
+ printf("::write() failed: %s(%d)\n", std::strerror( errno), errno);
+ ::exit( 1);
}
-};
+}
-void f() {}
+void create_sockets( int fd[2])
+{
+ if ( ::socketpair( PF_LOCAL, SOCK_STREAM, 0, fd) < 0)
+ {
+ printf("::pipe() failed: %s(%d)\n", std::strerror( errno), errno);
+ ::exit( 1);
+ }
+}
int main( int argc, char *argv[])
{
try
{
- pool_type pool( tp::poolsize( 1) );
- boost::packaged_task< void > tsk( boost::bind( f) );
- boost::shared_future< void > f( tsk.get_future() );
- fibo fib( f, 3);
- std::vector< tp::task< int > > results;
- results.reserve( 40);
-
- pt::ptime start( pt::microsec_clock::universal_time() );
-
- for ( int i = 0; i < 10; ++i)
- results.push_back(
- pool.submit(
- boost::bind(
- & fibo::execute,
- boost::ref( fib),
- i) ) );
-
- int k = 0;
- std::vector< tp::task< int > >::iterator e( results.end() );
- for (
- std::vector< tp::task< int > >::iterator i( results.begin() );
- i != e;
- ++i)
- std::cout << "fibonacci " << k++ << " == " << i->result().get() << std::endl;
+ int fd[2];
+ create_sockets( fd);
+
+ tp::get_default_pool().submit(
+ boost::bind(
+ & read,
+ fd[0]) );
+
+ write( fd[1], "Hello ");
+ boost::this_thread::sleep( pt::seconds( 1) );
+
+ for ( int i = 0; i < 15; ++i)
+ tp::get_default_pool().submit(
+ boost::bind(
+ & parallel_fib,
+ i) );
- pt::ptime stop( pt::microsec_clock::universal_time() );
- std::cout << ( stop - start).total_milliseconds() << " milli seconds" << std::endl;
+ write( fd[1], "World!");
return EXIT_SUCCESS;
}
Modified: sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -7,10 +7,7 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/ref.hpp>
-#include "boost/tp/fifo.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace pt = boost::posix_time;
namespace tp = boost::tp;
@@ -47,7 +44,7 @@
10) ) );
boost::this_thread::sleep( pt::milliseconds( 250) );
pool.shutdown_now();
- std::cout << t.result().get() << std::endl;
+ std::cout << t.get() << std::endl;
return EXIT_SUCCESS;
}
Modified: sandbox/threadpool/libs/tp/examples/sleep.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/sleep.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/sleep.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -70,13 +70,11 @@
{
try
{
- pool_type pool( tp::poolsize( 1) );
for ( int i = 0; i < 10; ++i)
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
& parallel_fib,
i) );
- pool.shutdown();
return EXIT_SUCCESS;
}
Modified: sandbox/threadpool/libs/tp/examples/smart.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/smart.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/smart.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -6,10 +6,7 @@
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
-#include "boost/tp/smart.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace pt = boost::posix_time;
namespace tp = boost::tp;
Modified: sandbox/threadpool/libs/tp/examples/submit.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/submit.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/submit.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -4,10 +4,7 @@
#include <boost/bind.hpp>
-#include "boost/tp/fifo.hpp"
-#include "boost/tp/pool.hpp"
-#include "boost/tp/poolsize.hpp"
-#include "boost/tp/unbounded_channel.hpp"
+#include "boost/tp.hpp"
namespace tp = boost::tp;
@@ -32,15 +29,12 @@
{
try
{
- tp::pool<
- tp::unbounded_channel< tp::fifo >
- > pool( tp::poolsize( 1) );
tp::task< int > t(
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
fibonacci_fn,
10) ) );
- std::cout << t.result().get() << std::endl;
+ std::cout << t.get() << std::endl;
return EXIT_SUCCESS;
}
Modified: sandbox/threadpool/libs/tp/examples/yield.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/yield.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/yield.cpp 2009-04-13 03:39:33 EDT (Mon, 13 Apr 2009)
@@ -69,13 +69,11 @@
{
try
{
- pool_type pool( tp::poolsize( 1) );
for ( int i = 0; i < 10; ++i)
- pool.submit(
+ tp::get_default_pool().submit(
boost::bind(
& parallel_fib,
i) );
- pool.shutdown();
return EXIT_SUCCESS;
}
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