
Am Samstag, 1. November 2008 00:53:11 schrieb Michael Marcin:
k-oli@gmx.de wrote:
Hello,
the new version of Boost.Threadpool depends on Boost.Fiber.
Forgive my ignorance but http://tinyurl.com/6r3l6u claims that "fibers do not provide advantages over a well-designed multithreaded application".
What are the benefits of using them in a threadpool?
Thanks,
Hello Michael, using fibers in a threadpool enables fork/join semantics (algorithms which recursively splitt an action into smaller sub-actions; forking the sub-actions into separate worker threads, so that they run in parallel on multiple cores) For instance the recursive calculation of fibonacci numbers (I know it is not the best algorithm for fibonacci calculation): class fibo { private: pool_type & pool_; int offset_; int seq_( int n) { if ( n <= 1) return n; else return seq_( n - 2) + seq_( n - 1); } int par_( int n) { if ( n <= offset_) return seq_( n); else { tp::task< int > t1( pool_.submit( boost::bind( & fibo::par_, boost::ref( * this), n - 1) ) ); tp::task< int > t2( pool_.submit( boost::bind( & fibo::par_, boost::ref( * this), n - 2) ) ); // without fibers this code line would block until t1 and t2 are executed // if all worker-threads of the pool are waiting in this line the app // blocks forever return t1.get() + t2.get(); } } public: fibo( pool_type & pool, int offset) : pool_( pool), offset_( offset) {} int execute( int n) { return par_( n); } }; int main( int argc, char *argv[]) { try { pool_type pool( tp::poolsize( 2) ); fibo fib( pool, 1); // calulates the fibonacci numbers from 0 - 5 for( int i = 0; i <= 5; ++i) { tp::task< int > t( pool.submit( boost::bind( & fibo::execute, boost::ref( fib), i) ) ); std::cout << "fibonacci of " << i << " == " << t.get() << std::endl; } pool.shutdown(); } catch ( boost::thread_interrupted const& ) { std::cerr << "thread_interrupted: thread was interrupted" << std::endl; } catch ( std::exception const& e) { std::cerr << "exception: " << e.what() << std::endl; } catch ( ... ) { std::cerr << "unhandled" << std::endl; } return EXIT_SUCCESS; } regards, Oliver