C++多线程高并发 future_status,std::shared_future(std::future只能get一次的问题)

wait_for函数

int threadEntryMethod()
{
	std::cout << "this thread id si " << std::this_thread::get_id() << std::endl;
	std::chrono::milliseconds dura(2000);
	std::this_thread::sleep_for(dura);
	std::cout << "this thread workd finished" << std::endl;
	return 666;
}
int main()
{
	
	std::future<int> myFuture = std::async(threadEntryMethod);

	//future对象根据wait_for方法返回一个枚举类型 std::future_status
	//但是有一点需要知道,主线程必须等待async创建的子线程执行完毕
	std::future_status myFutureStatus = myFuture.wait_for(std::chrono::seconds(1));
}

wait_for函数返回值 std::future_status的应用

int threadEntryMethod()
{
	std::cout << "this thread id si " << std::this_thread::get_id() << std::endl;
	std::chrono::milliseconds dura(2000);
	std::this_thread::sleep_for(dura);
	std::cout << "this thread workd finished" << std::endl;
	return 666;
}

int main()
{
	//这里设置子线程延时执行,去掉参数std::launch::deferred即可立即执行
	std::future<int> myFuture = std::async(std::launch::deferred,threadEntryMethod);

	//future对象根据wait_for方法返回一个枚举类型 std::future_status
	//但是有一点需要知道,主线程必须等待async创建的子线程执行完毕
	std::future_status myFutureStatus = myFuture.wait_for(std::chrono::seconds(1));

	//future::status future状态解释
	//timeout是超时,等待的时间结束,而子线程仍然没有结束
	if (myFutureStatus == std::future_status::timeout)
	{
		std::cout << "子线程执行超时" << std::endl;
	}
	//子线程已经结束
	else if (myFutureStatus == std::future_status::ready)
	{
		std::cout << "子线程正常执行完毕" << std::endl;
	}
	//子线程是延时执行的
	else if (myFutureStatus == std::future_status::deferred)
	{
		std::cout << "子线程被延迟执行" << std::endl;
		int result = myFuture.get();
		std::cout << "在defer中,执行结果是 " << result << std::endl;
	}
	return 0;
}

std::future只能get一次的问题=>std::shared_future

#include 
#include 
#include 
//这里使用promise的引用,将结果值传递到主线程里面
void threadEntryMethod1(std::promise<int> &myPromise,int i)
{
	std::cout << "子线程id是 " <<std::this_thread::get_id() << std::endl;
	int result = i * 100;
	std::this_thread::sleep_for(std::chrono::milliseconds(2000));
	myPromise.set_value(result);
}

//这里使用shared_future,即使多个线程同时执行这个函数,都是可以通过get得到结果值的
void threadEntryMethod2(std::shared_future<int> &myFuture)
{
	std::cout << "子线程id是 " << std::this_thread::get_id() << std::endl;
	int result = myFuture.get();
	std::cout << "执行结果时" << result << std::endl;
}

int main()
{
	std::cout << "主线程id是 " << std::this_thread::get_id() << std::endl;
	//声明promise
	std::promise<int> myPromise;
	//创建线程,执行线程入口函数
	std::thread myThread(threadEntryMethod1,std::ref(myPromise), 6);
	//通过promise得到future,
	//下面shared_future的创建方法二使用时,这句话不能执行,因为promise的
	//get_future也是只能执行一次的
	std::future<int> myFuture = myPromise.get_future();
	//一个判断future是否有值的方法
	//bool ifHaveValue = myFuture.valid();

	//shared_future创建方法一,值用future转为右值,进行构造
	std::shared_future<int> mySharedFuture(std::move(myFuture));
	//shared_future创建方法二,直接通过future的返回值进行构造
	//std::shared_future mySharedFuture2(myPromise.get_future());
	int result1 = mySharedFuture.get();
	int result2 = mySharedFuture.get();
	std::cout << result1 << std::endl;
	std::cout << result2 << std::endl;
	myThread.join();

	std::thread myThread2(threadEntryMethod2,std::ref(mySharedFuture));
	myThread2.join();
	std::cout << "the main thread is finished" << std::endl;
}

你可能感兴趣的:(C++)