Created
December 19, 2022 22:08
-
-
Save tiandiao123/c546d9aa786ba63b6e39cda1c4ca64e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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