2017-12-08 13 views
0

가 나는 표준을 시도 : 예제 코드 비동기 여기 http://www.cplusplus.com/reference/future/async/C++ std :: async에서 system_error가 발생합니까?

에 그러나, 나는 알 수없는 오류와 시스템 오류가 발생할 -1. 누구든지 나를 도울 수 있습니까? 감사!

다음

웹 사이트에서 복사됩니다 내 코드입니다 :

// async example 
#include <iostream>  // std::cout 
#include <future>   // std::async, std::future 

// a non-optimized way of checking for prime numbers: 
bool is_prime (int x) { 
    std::cout << "Calculating. Please, wait...\n"; 
    for (int i=2; i<x; ++i) if (x%i==0) return false; 
    return true; 
} 

int main() 
{ 
    // call is_prime(313222313) asynchronously: 
    std::future<bool> fut = std::async (is_prime,313222313); 

    std::cout << "Checking whether 313222313 is prime.\n"; 
    // ... 

    bool ret = fut.get();  // waits for is_prime to return 

    if (ret) std::cout << "It is prime!\n"; 
    else std::cout << "It is not prime.\n"; 

    return 0; 
} 

그리고 다음 내 명령 줄입니다 :

ubuntu:~/cpp_dynamic_invoke_success/stdasync_test$ g++ isprime.cpp -std=c++11 -o isprime 
ubuntu:~/cpp_dynamic_invoke_success/stdasync_test$ ./isprime 
Checking whether 313222313 is prime. 
terminate called after throwing an instance of 'std::system_error' 
    what(): Unknown error -1 
Aborted (core dumped) 

내 우분투 버전 :

No LSB modules are available. 
Distributor ID: Ubuntu 
Description: Ubuntu 14.04.3 LTS 
Release: 14.04 
Codename: trusty 

그리고 g ++ 버전 .

g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 
Copyright (C) 2013 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
+0

,이 https://stackoverflow.com/questions/46634629/c-async-sometimes-resulting-in-stdsystem-error [이 질문] (의 속는 사람입니다 -and-sometimes-not) – bnaecker

+1

이것은 꽤 오래된 GCC 버전이므로 컴파일 명령의 끝에'-pthread' 플래그를 추가해야 할 수도 있습니다. – bnaecker

+0

감사! '-pthread'를 추가하면 정말 도움이됩니다! – desword

답변

0

답변은 -pthread 컴파일 옵션을 추가하는 것입니다. 이 답을 가지고 있지 않지만

g++ isprime.cpp -std=c++11 -o isprime -pthread