Le 14/10/15 19:02, Malko a écrit :
Hi, I am trying to compile the following example: http://www.boost.org/doc/libs/1_59_0/doc/html/thread/synchronization.html#th...
#include
#include int parallel_sum(int* data, int size) { int sum = 0; if ( size < 1000 ) for ( int i = 0; i < size; ++i ) sum += data[i]; else { boost::future<int> handle = boost::async(parallel_sum, data+size/2, size-size/2); The example uses auto ;-) sum += parallel_sum(data, size/2); sum += handle.get(); } return sum; }
int main() { int data[] = {1,2,3,4,5,6,7,8,9,10}; int a(parallel_sum, data, 10); return 0; }
It does not compile because future is not a type in boost and async has the wrong signature. The future error goes away by defining BOOST_THREAD_PROVIDES_FUTURE, but I can not find this mentioned anywhere in the docs, I found out only looking on stack overflow. http://www.boost.org/doc/libs/1_59_0/doc/html/thread/build.html#thread.build...
The async signature seems to require a function of arity 0. In http://www.boost.org/doc/libs/1_59_0/doc/html/thread/synchronization.html#th...
the variadic prototype is provided only on C++11 compilers supporting rvalue references, variadic templates, decltype and a standard library providing <tuple> (waiting for a boost::tuple that is move aware), and BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK is defined. http://www.boost.org/doc/libs/1_59_0/doc/html/thread/build.html#thread.build...
This is clang 3.4 output using Boost.1.59 and C++03:
<snip>
Is there more macro to be used to enable this to compile?
The easiest way, define before including any Boost.Thread file BOOST_THREAD_VERSION 4 http://www.boost.org/doc/libs/1_59_0/doc/html/thread/build.html#thread.build...
Thanks
P.S. It would be nice to have all the info about macro configuration for future (and async, if it has any) in the docs.
:) I recognize that all these macros area nightmare. They exist in order to ensure backward compatibility with version 2 and 3. An alternative approach I didn't considered when I developed it is to have multiple versions. To late for Boost.Thread versions 2,3,4. I would like to create a version 5 that coexists with version 2, 3 and 4 but I have not find the needed time. Best, Vicente