代码如下:
///////////////////////////////////////////////// // // // C++11 async test // // #include "pch.h" #include <iostream> #include <future> struct Task { int operator () (int i) { std::cout << "task run" << std::endl; return i + 10; } }; int main(int argc, char * const argv[]) { Task t; std::future<int> result = std::async(std::launch::async, t, 16); for (int i = 0; i < 100; ++i) { std::cout << "main thread" << std::endl; } std::cout << result.get() << std::endl; return 0; }