Boost logo

Boost :

Subject: Re: [boost] [Boost.Function] How about parallel functions?
From: Kasra (kasra_n500_at_[hidden])
Date: 2009-09-07 09:33:15


>> Hi,
>>
>> I required to call few functions is separate threads and use the
>> results. However, the code for this looks really ugly, you have to create
>> threads, then join the threads when ever you require the result of the >functions.
>>
>> So I have created a boost.parallel_function library that does this
>> seamlessly just like invoking a normal function and it only joins the
>> thread when the value of the function result type is required. Is this
>> library of any interest to any one?
>>
>> Here is the a hello world example:
>>
>> #include <cstdlib>
>> #include <limits>
>> #include <boost/parallel_function.hpp>
>> using namespace boost;
>>
>> int F0()
>> {
>> volatile int x = 0;
>> for(int i=0; i<std::numeric_limits<int>::max()/1024; i++)
>> x ^= std::rand();
>> return (int)x;
>> }
>>
>>
>> int F2(int y)
>> {
>> volatile int x = 0;
>> for(int i=0; i<std::numeric_limits<int>::max()/512; i++)
>> x ^= std::rand();
>> return (int)x ^ y;
>> }
>>
>> int main()
>> {
>> typedef boost::parallel_function< void ( ) > f0_type;
>> typedef boost::parallel_function< void (int) > f1_type;
>>
>> f0_type f0( &::F0 );
>> f1_type f1( &::F1 );
>>
>> f0_type::result_type r0 = f0();
>> f1_type::result_type r1 = f1(32);
>>
>> // use r0 and r1 as if they are int
>> r0 += 34;
>> int value = r1 *= r0 + 452;
>>
>> std::cout << "Hello world " << value << std::endl;
>>
>> return 0;
>> }
>
>The Boost.Task library should provides the features you need.
>
>int main() {
>
> boost::task::handle<int> f0 = boost::task:async(boost:task(F0),boost::task::new_thread());
> boost::task::handle<int> f1 = boost::task:async(boost:task(F1,32),boost::task::new_thread());
> int r0=f0.get();
> int r1=f1.get();
>
> r0 += 34;
> int value = r1 *= r0 + 452;
>
> std::cout << "Hello world " << value << std::endl;
>
> return 0;
>}
>
>This library is under the review schedule and will be reviewed soon. I'm >interested on the details of your implementation to compare with >Boost.Task. It will be also useful if you can take a look to Boost.task and >try so see it provides every thing you need.
>
>Best,
>Vicente

I have taken a partial look at Boost.task, however, what I don't like about the syntax is the following:

> int r0=f0.get();
> int r1=f1.get();

Current implementation (hack) would make 'f0' and 'f1' the same type 'int' (in this example). So the .get() part could be omitted altogether; which seems a lot like thread.join() call! Which is accomplished by a simple implicit conversion operator to the reference of the type.

A little out of topic discussion, why is there an atomic library within task? because boost.thread has one (I think). Atomic library should be written so it becomes more coherent and reduces the code size.

With Best Regards

Mr. Kasra Nassiri
Department of Mathematics &
Computer Science
Imperial College London

      


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