std::future、std::promise、std::packaged_task、std::async

#include <iostream>
#include <string>
#include <future>
#include <thread>
#include <chrono>
#include <condition_variable>
#include <hash_map>
#include <unordered_map>

using namespace std;

// 使用std::promise 共享对象,线程函数传入变量指针
void ThreadProc_Promise_Ptr(std::promise<string>* promise)
{
	cout << __FUNCTION__ << ":" << std::this_thread::get_id() << ", " << endl;
	if (promise != nullptr)
	{
		promise->set_value("2345");		// 线程中设置共享变量值
	}
}

void Test_ThreadProc_Promise_Ptr()
{
	std::thread th1;
	std::future<string> future;		// 在创建线程取出共享态变量
	{
		std::promise<string> promise_str;	// 许诺,将来会在内部共享变量中赋值
		future = promise_str.get_future();
		th1 = std::thread(ThreadProc_Promise_Ptr, &promise_str); // 传递引用

		cout << "th1:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
		th1.join();
	}

	// 放在这里用future.get()会崩溃,因为promise_str已经销毁了
	//cout << "th1:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
	//th1.join();
}

// 使用std::promise 共享对象,线程函数传入变量引用
void ThreadProc_Promise_Ref(std::promise<string>& promise)
{
	cout << __FUNCTION__ << ":" << std::this_thread::get_id() << ", " << endl;
	promise.set_value("123");	// 线程中设置共享变量值
}

void Test_ThreadProc_Promise_Ref()
{
	std::thread th1;
	std::future<string> future;		// 在创建线程取出共享态变量
	{
		std::promise<string> promise_str;	// 许诺,将来会在内部共享变量中赋值
		future = promise_str.get_future();
		th1 = std::thread(ThreadProc_Promise_Ref, std::ref(promise_str)); // 传递引用

		cout << "th1:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
		th1.join();
	}

	// 放在这里用future.get()会崩溃,因为promise_str已经销毁了
	//cout << "th1:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
	//th1.join();
}

// 使用std::packaged_task打包任务,返回任务处理结果
string ThreadProc_Packaged_Task(string str)
{
	cout << __FUNCTION__ << ":" << std::this_thread::get_id() << ", arg:" << str << ", " << endl;
	return "456";
}


void Test_ThreadProc_Packaged_Task()
{
	// 封装一个线程任务
	std::packaged_task<string(string)> task(ThreadProc_Packaged_Task); // packaged_task只可以移动,不可以复制
	std::future<string> future = task.get_future();
	std::thread th1(std::move(task), "packaged_task");
	cout << "th1:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
	th1.join();
}

// 使用std::packaged_task打包任务,返回任务处理结果
string ThreadProc_Async(string str)
{
	for (int i = 0; i < 4; ++i)
	{
		cout << __FUNCTION__ << ":" << std::this_thread::get_id() << ", arg:" << str << ", " << (i+1) << endl;
		std::this_thread::sleep_for(std::chrono::seconds(1));
	}
	return "789";
}

// 测试std::async的std::launch::async模式,调用async函数的时候就开始创建新线程
void Test_ThreadProc_Async_Launch_Async()
{
	std::future<string> future = std::async(std::launch::async, ThreadProc_Async, "async");
	if (future.wait_for(std::chrono::seconds(2)) == std::future_status::ready)
	{
		cout << "launch async:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
	}
	else
	{
		cout << "launch async:" /*<< th1.get_id()*/ << ", future:" << "time out" << endl;
	}
}

// 测试std::async的std::launch::deferred模式,是延迟调用,调用async函数时并未创建线程,必须调用future.get()或future.wait()相关函数,才会触发线程创建和执行
void Test_ThreadProc_Async_Launch_Deferred()
{
	std::future<string> future = std::async(std::launch::deferred, ThreadProc_Async, "deferred");
	future.wait(); // 1. future.wait()相关函数,才会触发线程创建
	//cout << "launch deferred:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl; // 2.future.get() 才会触发线程创建

	// 3.future.wait()相关函数,才会触发线程创建
	//if (future.wait_for(std::chrono::seconds(20)) == std::future_status::ready)
	//{
	//	cout << "launch deferred:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
	//}
	//else
	//{
	//	cout << "launch deferred:" /*<< th1.get_id()*/ << ", future:" << "time out" << endl; // 这里会直接进入超时,因为没有准备好数据
	//}
}

// 测试std::async默认是launch::async | launch::deferred 组合模式,调用async函数的时可能创建新线程,也可能延迟调用。系统会根据当前的资源情况选择合适的方式
void Test_ThreadProc_Async_Default()
{
	std::future<string> future = std::async(ThreadProc_Async, "async and deferred");
	if (future.wait_for(std::chrono::seconds(3)) == std::future_status::ready)
	{
		cout << "launch async:" /*<< th1.get_id()*/ << ", future:" << future.get() << endl;
	}
	else
	{
		cout << "launch async:" /*<< th1.get_id()*/ << ", future:" << "time out" << endl;
	}
}

void Test()
{
	std::hash_map<string, string> m_hash_map;
	std::unordered_map<string, string> m_unordered_map;
}

int main()
{
	Test_ThreadProc_Promise_Ptr();
	Test_ThreadProc_Promise_Ref();

	Test_ThreadProc_Packaged_Task();

	Test_ThreadProc_Async_Launch_Async();
	Test_ThreadProc_Async_Launch_Deferred();
	Test_ThreadProc_Async_Default();


	getchar();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值