c++ - 我在这里如何滥用 C++ promises 和 futures?

标签 c++ promise future

这是我的代码,main.cpp:

#include <string>
#include <iostream>
#include <future>

int main() {
  using namespace std;

  auto p = promise<string>();

  p.set_value("Hello, world. ");

  auto f = p.get_future();

  cout << f.get() << endl;

  return 0;
}

这里是错误:

./a.out    
terminate called after throwing an instance of 'std::system_error'              
  what():     
    Unknown error -1  [1]  

15195 abort (core dumped)  ./a.out

我的编译器版本:

$ clang++ --version 
clang version 7.0.0-3 (tags/RELEASE_700/final)  
Target: x86_64-pc-linux-gnu 
Thread model: posix InstalledDir: /usr/bin

我的编译命令:

$ clang++ ./main.cpp && ./a.out

最佳答案

std::promise 是 C++ 线程支持的一部分,即使您以非线程方式使用它也是如此。

所以你必须使用编译器选项-pthread:

clang++ -pthread main.cpp

关于c++ - 我在这里如何滥用 C++ promises 和 futures?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53748331/

相关文章:

c++ - 字符输入问题

javascript - typescript: error TS2693: 'Promise' 只引用了一个类型,但在这里被用作一个值

javascript - 我可以在不使用await的情况下捕获异步错误吗?

scala - Akka : the proper use of `ask` pattern?

scala - 为什么 future 的例子不起作用?

c++ - std::initializer_list 私有(private)构造函数是否从编译器中得到非常特殊的对待?

c++ - 当 const 引用绑定(bind)到临时引用时,堆栈中会发生什么?

c++ - 使用 ArrayFire 中的 OpenGL

scala - 为什么编译器会错误地推断函数中 Future.sequence 声明的返回类型?

javascript - 从 Angular 2 中 Chartist 的 API 获取数据