Boost logo

Boost :

Subject: [boost] [task] - boost.task (replacement for boost.threadpool)
From: k-oli_at_[hidden]
Date: 2009-06-01 15:09:44


Hi,
boost vault (section 'Concurrent Programming' -
http://www.boostpro.com/vault/) contains boost.task.

It replaces boost.threadpool and provides a framework for parallel execution
of tasks (launching task in the current thread, in a new thread, as sub-task
or in a pool of threads).

The code looks someting like this:

std::string echo( std::string const& msg)
{ return msg; }

void main()
{
        boost::task::handle< std::string > h(
                boost::task::async(
                        boost::task::new_thread(),
                        boost::task::make_task(
                                echo,
                                "Hello World!") ) );
        std::cout << h.get() << std::endl;
}

or

long fibonacci_fn( long n)
{
        if ( n == 0) return 0;
        if ( n == 1) return 1;
        long k1( 1), k2( 0);
        for ( int i( 2); i <= n; ++i)
        {
                long tmp( k1);
                k1 = k1 + k2;
                k2 = tmp;
        }
        return k1;
}

void main()
{
        std::cout << "worker-threads running in default-pool == ";
        std::cout << boost::task::default_pool().size() << "\n";

        boost::task::handle< long > h1(
                boost::task::async(
                        boost::task::default_pool(),
                        boost::task::make_task( fibonacci_fn, 10) ) );

        boost::task::handle< long > h2(
                boost::task::async(
                        boost::task::default_pool(),
                        boost::task::make_task( fibonacci_fn, 5) ) );

        std::cout << "fibonacci(10) == " << h1.get() << "\n";
        std::cout << "fibonacci(5) == " << h2.get() << std::endl;
}

best regards,
Oliver


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk