std::promise<int> pro;//pro.get_future.get()的返回值为int类型
std::promise<int> pro;
pro.set_value(10);
void thread1(std::promise<int> p, int val){
std::cout << "in thread1" << std::endl;
p.set_value(val);
}
std::promise<int> pro;
std::future<int> ft1 = pro.get_future();
std::thread t1(thread1, std::move(pro), 10);
t1.detach();
std::cout << ft1.get() << std::endl;
#include <future>
#include <thread>
#include <iostream>
void thread1(std::promise<int> p, int val){
std::cout << "in thread1" << std::endl;
p.set_value(val);
}
int main(){
std::promise<int> p;
std::future<int> f = p.get_future();
std::thread t1(thread1, std::move(p), 10);
t1.detach();
std::cout << f.get() << std::endl;
pthread_exit(NULL);
}
c/c++ 多线程 等待一次性事件 std::promise用法
原文:https://www.cnblogs.com/xiaoshiwang/p/10016225.html