This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: std::async should work with 4.7.1, but it doesn't?


On 2 June 2013 19:01, Oliver Kullmann <O.Kullmann@swansea.ac.uk> wrote:
> Hello,
>
> I can compile
>
> ---
>
> #include <iostream>
> #include <vector>
> #include <numeric>
> #include <future>
>
> template <typename RAIter>
> int parallel_sum(const RAIter beg, const RAIter end) {
>   const auto len = end-beg;
>   if(len < 1000) return std::accumulate(beg, end, 0);
>
>   const RAIter mid = beg + len/2;
>   auto handle = std::async(std::launch::async, parallel_sum<RAIter>, mid, end);
>   const int sum = parallel_sum(beg, mid);
>   return sum + handle.get();
> }
>
> int main() {
>   std::vector<int> v(10000, 1);
>   std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << ".\n";
> }
>
> ---
>
> using "g++ -Wall -std=c++11", but running it I get
>
> terminate called after throwing an instance of 'std::system_error'
>   what():  Unknown error -1
> Aborted (core dumped)

You need to compile and link using  -pthread to make use of multithreading.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]