This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: std::async should work with 4.7.1, but it doesn't?
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Oliver Kullmann <O dot Kullmann at swansea dot ac dot uk>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Sun, 2 Jun 2013 22:59:00 +0100
- Subject: Re: std::async should work with 4.7.1, but it doesn't?
- References: <20130602180145 dot GA22284 at csltok dot swansea dot ac dot uk>
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.