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

2023-11-20

#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;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

std::future、std::promise、std::packaged_task、std::async 的相关文章

随机推荐