我如何在这里滥用C ++承诺和期货?

时间:2018-12-12 17:26:44

标签: c++ promise future

这是我的代码, PomEquippedResolveStage pomEquippedResolveStage = Maven.resolver().loadPomFromFile("pom.xml"); File[] hibernateLibs = pomEquippedResolveStage.resolve( "org.hibernate:hibernate-entitymanager" ).withTransitivity().asFile(); final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war") .addAsLibraries(hibernateLibs) .addClass(org.hibernate.tool.schema.internal.SchemaDropperImpl.class)

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

1 个答案:

答案 0 :(得分:5)

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

因此,您必须使用编译器选项-pthread

clang++ -pthread main.cpp