首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ std::异步遇到system_error?

C++ std::异步遇到system_error?
EN

Stack Overflow用户
提问于 2017-12-08 03:45:06
回答 1查看 347关注 0票数 0

我已经尝试过这里的std:异步示例代码http://www.cplusplus.com/reference/future/async/

然而,我遇到系统错误与未知的错误-1。有人能帮我吗?谢谢!

以下是我从网站复制的代码:

代码语言:javascript
复制
// 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;
}

以下是我的命令行:

代码语言:javascript
复制
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)

我的ubuntu版本:

代码语言:javascript
复制
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:    14.04
Codename:   trusty

和g++版本。

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-09 07:35:56

答案是在编译选项的后面添加-pthread

代码语言:javascript
复制
g++ isprime.cpp -std=c++11 -o isprime -pthread
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47707381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档