Smithy
Smithy

Reputation: 1207

Using async and future is throwing exception on linux (gcc 4.8.4)

I have a simple piece of code that throws an exception when it shouldn't. All the appropriate headers were included and the program builds without errors. Code below. The exception occurs when executing result1.get(); Could some please tell me what I'm doing wrong? Thanks in advance for any help.

int func1 ()
{
    return 0;
}

int main()
{
    future<int> result1(async(func1));
    int result = result1.get();
    return 0;
}

The result of running the program gives me this:

terminate called after throwing an instance of 'std::system_error'
what():  Unknown error -1

Upvotes: 2

Views: 555

Answers (1)

Rudolfs Bundulis
Rudolfs Bundulis

Reputation: 11954

Add -pthread to the compiler flags. Not sure about the details, but for somewhat reason gcc 4.8 gives the Unknown error -1 if this has been left out (however, at least IMHO, it should either warn that you have not enabled threading or give some linking error instead of compiling and throwing in runtime).

Upvotes: 2

Related Questions