C++的promise和future

#include 
#include 
#include 
// https://en.cppreference.com/w/cpp/thread/promise
// https://zh.cppreference.com/w/cpp/thread/promise

void add(std::promise accumulate_promise, int x, int y)
{
	int sum = x + y;
	accumulate_promise.set_value(sum);  // Notify future
}


int main()
{
	std::promise add_promise;
	std::future add_future = add_promise.get_future();
	std::thread work_thread(add, std::move(add_promise), 100, 200);
	add_future.wait();  // wait for result
	std::cout << "result=" << add_future.get() << '\n';
	work_thread.join();  // wait for thread completion

    std::cout << "End!\n"; 
}

 

你可能感兴趣的:(C++FAQ,知识库)