Skip to content

Instantly share code, notes, and snippets.

@tiandiao123
Created December 19, 2022 22:08
Show Gist options
  • Select an option

  • Save tiandiao123/c546d9aa786ba63b6e39cda1c4ca64e2 to your computer and use it in GitHub Desktop.

Select an option

Save tiandiao123/c546d9aa786ba63b6e39cda1c4ca64e2 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
#include <future>
#include <thread>
using namespace std;
void TestFuture(promise<string> p){
this_thread::sleep_for(3s);
cout << "begin setting value ... " << endl;
p.set_value("TestFuture value Hello World");
this_thread::sleep_for(3s);
cout <<"end Test Future ..." << endl;
}
int main(int argc, char* argv[]){
//异步传输变量存储
promise<string> p;
// 用来获取线程写入值
auto future = p.get_future();
auto th = thread(TestFuture, std::move(p));
cout <<"begin future get ... " << endl;
cout << "future get(): " << future.get() << endl;
th.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment