#include #include #include using namespace std; void test1() { promise p; future f = p.get_future(); thread t1([&]() { this_thread::sleep_for(chrono::milliseconds(1000)); p.set_value(12); this_thread::sleep_for(chrono::milliseconds(1000)); }); t1.detach(); std::cout << "test1:: res:: " << f.get() << endl; } void test2() { promise p; future f = p.get_future(); shared_future sf = f.share(); thread t1([&]() { this_thread::sleep_for(chrono::milliseconds(1000)); p.set_value(12); this_thread::sleep_for(chrono::milliseconds(1000)); }); t1.detach(); std::cout << "test2:: res:: " << sf.get() << sf .get() << endl; } void test3() { auto fn = []()->int { this_thread::sleep_for(chrono::milliseconds(1000)); return 12; }; auto f = async(launch::async, fn); packaged_task t(fn); t(); std::cout << "test3:: res:: " << f.get() << t.get_future().get() << endl; } int main() { test1(); test2(); test3(); //这里通过条件变量 | 信号量 + 共享变量就能实现 //用promise-future 感觉很浪费 std::cout << "promise size:: " << sizeof(promise) << " future size:: " << sizeof(future) << " shared_future size:: " << sizeof(shared_future) << endl; return 1; }