- 新增现代 C++ 教程的 Preface 章节,包括英文和中文版本 - 添加 C++ Primer 练习代码 - 新增 Learn C++ 教程的 C++ 开发简介章节 - 添加头文件解析文档 - 更新 mkdocs.yml,包含新教程的目录结构 - 修改项目设置,使用 Python 3.10环境
25 lines
739 B
C++
25 lines
739 B
C++
//
|
|
// 7.4.futures.cpp
|
|
// chapter 7 parallelism and concurrency
|
|
// modern c++ tutorial
|
|
//
|
|
// created by changkun at changkun.de
|
|
// https://github.com/changkun/modern-cpp-tutorial
|
|
//
|
|
|
|
#include <iostream>
|
|
#include <thread>
|
|
#include <future>
|
|
|
|
int main() {
|
|
// pack a lambda expression that returns 7 into a std::packaged_task
|
|
std::packaged_task<int()> task([](){return 7;});
|
|
// get the future of task
|
|
std::future<int> result = task.get_future(); // run task in a thread
|
|
std::thread(std::move(task)).detach();
|
|
std::cout << "waiting...";
|
|
result.wait(); // block until future has arrived
|
|
// output result
|
|
std::cout << "done!" << std:: endl << "future result is " << result.get() << std::endl;
|
|
return 0;
|
|
} |