C++日记——Day45:future其他成员函数、shared_future、atomic

std::future 的成员函数

1、wait_for():std::future_status::timeout \ std::future_status::ready \ std::future_status::deferred

#include <thread>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <future>
using namespace std;

int mythread() {
	cout << "mythread() start" << "threadid = " << std::this_thread::get_id() << endl;
	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	cout << "mythread() end" << "threadid = " << std::this_thread::get_id() << endl;
	return 5;
}


int main() {
	A a;
	int tmp = 12;
	cout << "main" << "threadid = " << std::this_thread::get_id() << endl;
	std::future<int> result = std::async(mythread);
	cout << "continue........" << endl;
	//cout << result1.get() << endl; //卡在这里等待mythread()执行完毕,拿到结果
	std::future_status status = result.wait_for(std::chrono::seconds(1));
	if (status == std::future_status::timeout) {
		//超时:表示线程还没有执行完
		cout << "超时了,线程还没有执行完" << endl;
	}
	//类成员函数
	return 0;
}

#include <thread>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <future>
using namespace std;

int mythread() {
	cout << "mythread() start" << "threadid = " << std::this_thread::get_id() << endl;
	//std::chrono::milliseconds dura(5000);
	//std::this_thread::sleep_for(dura);
	cout << "mythread() end" << "threadid = " << std::this_thread::get_id() << endl;
	return 5;
}

int main() {
	A a;
	int tmp = 12;
	cout << "main" << "threadid = " << std::this_thread::get_id() << endl;
	std::future<int> result = std::async(std::launch::deferred, mythread);
	//std::future<int> result = std::async(mythread);
	cout << "continue........" << endl;
	//cout << result1.get() << endl; //卡在这里等待mythread()执行完毕,拿到结果
	std::future_status status = result.wait_for(std::chrono::seconds(6));
	if (status == std::future_status::timeout) {
		//超时:表示线程还没有执行完
		cout << "超时了,线程还没有执行完" << endl;
	}
	else if (status == std::future_status::ready) {
		//表示线程成功放回
		cout << "线程执行成功,返回" << endl;
		cout << result.get() << endl;
	}
	else if (status == std::future_status::deferred) {
		//如果设置 std::future<int> result = std::async(std::launch::deferred, mythread);,则本条件成立
		cout << "线程延迟执行" << endl;
		cout << result.get() << endl;
	}

	//类成员函数
	cout << "good luck" << endl;
	return 0;
}

get()只能使用一次,再次使用会报异常;

因为get()函数的设计是一个移动语义;

 

std::shared_future:也是个类模板

std::future的 get() 成员函数是转移数据

std::shared_future 的 get() 函数是赋值数据

#include <thread>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <future>
using namespace std;

int mythread() {
	cout << "mythread() start" << "threadid = " << std::this_thread::get_id() << endl;
	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	cout << "mythread() end" << "threadid = " << std::this_thread::get_id() << endl;
	return 5;
}

int mythread2(std::shared_future<int> &res) {
	cout << "mythread2() start" << "threadid = " << std::this_thread::get_id() << endl;
	auto myresult3 = res.get();
	cout << "mythread2() end" << "threadid = " << std::this_thread::get_id() << endl;
	return 5;
}

int main() {
	int tmp = 12;
	cout << "main" << "threadid = " << std::this_thread::get_id() << endl;
	std::packaged_task<int()> mypt(mythread);
	std::thread t1(std::ref(mypt));
	std::future<int> result = mypt.get_future();
	//std::shared_future<int> result_s(std::move(result));
	bool ifcanget = result.valid(); //判断future 中的值是不是一个有效值
	std::shared_future<int> result_s(result.share()); //执行完毕后result_s里有值,而result里空了
	t1.join();
	
	auto myresult1 = result_s.get();
	auto myresult2 = result_s.get();

	std::thread t2(std::ref(mythread2), std::ref(result_s));
	t2.join();
	cout << "good luck" << endl;
	return 0;
}

std::shared_future<int> result_s(mypt.get_future()); //通过get_future返回值直接构造一个shared_future对象

 

std::atomic原子操作

原子操作概念引出范例:

互斥量:多线程编程中 用于保护共享数据:先锁住, 操作共享数据, 解锁。

有两个线程,对一个变量进行操作,一个线程读这个变量的值,一个线程往这个变量中写值。

即使是一个简单变量的读取和写入操作,如果不加锁,也有可能会导致读写值混乱(一条C语句会被拆成3、4条汇编语句来执行,所以仍然有可能混乱)

using namespace std;
int g_count = 0;

void mythread1() {
	for (int i = 0; i < 1000000; i++) {
		g_count++;
	}
}

int main() {
	std::thread t1(mythread1);
	std::thread t2(mythread1);
	t1.join();
	t2.join();
	cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

 

使用mutex解决这个问题

using namespace std;
int g_count = 0;
std::mutex mymutex;

void mythread1() {
	for (int i = 0; i < 1000000; i++) {
		std::unique_lock<std::mutex> u1(mymutex);
		g_count++;
	}
}


int main() {
	std::thread t1(mythread1);
	std::thread t2(mythread1);
	t1.join();
	t2.join();
	cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

 大家可以把原子操作理解成一种:不需要用到互斥量加锁(无锁)计数多线程并发变成方式。

原子操作:在多线程中不会被打断的程序执行片段。

从效率上来说,原子操作要比互斥量的方式效率要高。

互斥量的加锁一般是针对一个代码段,而原子操作针对的一般都是一个变量,而不是一个代码段。

原子操作,一般都是指“不可分割的操作”;也就是说这种操作状态要么是完成的,要么是没完成的,不可能出现半完成状态。

std::atomic来代表原子操作,是个类模板。其实std::atomic使用来封装某个类型的值得

范例:

using namespace std;
std::atomic<int> g_count = 0; //封装了一个类型为int的 对象(值)

void mythread1() {
	for (int i = 0; i < 1000000; i++) {
		g_count++;
	}
}


int main() {
	std::thread t1(mythread1);
	std::thread t2(mythread1);
	t1.join();
	t2.join();
	cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

 

using namespace std;
std::atomic<bool> g_ifend = false; //封装了一个类型为int的 对象(值)

void mythread() {
	std::chrono::milliseconds dura(1000);
	while (g_ifend == false) {
		cout << "thread id = " << std::this_thread::get_id() << "运行中" << endl;
		std::this_thread::sleep_for(dura);
	}
	cout << "thread id = " << std::this_thread::get_id() << "运行结束" << endl;
}

int main() {
	std::thread t1(mythread);
	std::thread t2(mythread);
	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	g_ifend = true;
	cout << "程序执行完毕" << endl;
	t1.join();
	t2.join();
}

 

总结:

1、原子操作一般用于计数或者统计(累计发送多少个数据包,累计接收到了多少个数据包),多个线程一起统计,这种情况如果不使用原子操作会导致统计发生混乱。

2、写商业代码时,如果不确定结果的影响,最好自己先写一小段代码调试。或者不要使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值