C++多线程编程(二)

转自:https://www.youtube.com/watch?v=IBu5ka1MQ7w

unique_lock<mutex>、once_flag、call_once

// https://www.youtube.com/watch?v=IBu5ka1MQ7w
//Lazy Initialization
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<thread>
#include<mutex>
#include<string>


using namespace std;

class LogFile
{
public:
	LogFile() {
		//_f.open("log.txt");
	};
	~LogFile() {};

	void share_print(string id,int val) {

		//unique_lock<mutex> locker2(_mu_open);//_f只打开一次,但每个进程都要lock一下再判断,浪费时间!
		//if (!_f.is_open()) {
		//	_f.open("log.txt");//Lazy Initialization 
		//}

		call_once(_flag, [&]() {_f.open("log.txt"); });//只打开一次



		//lock_guard<mutex> guard(_mu);   
		//unique_lock<mutex>比lock_guard<mutex>更加灵活,可以随时 lock()或者 unlock()
		//unique_lock<mutex>可以移动(move)!!
		unique_lock<mutex> locker(_mu,defer_lock);//defer_lock 延迟上锁!
		//do somthing else

		locker.lock();
		_f << "From " <<id << ":" << val << endl;
		locker.unlock();

		/*locker.lock();
		_f << "......:)..." << endl;
		locker.unlock();*/

		//unique_lock<mutex> locker2 = move(locker);


	}
private:
	mutex _mu;
	mutex _mu_open;
	ofstream _f;
	once_flag _flag;

};

void thread_function(LogFile &log,string id,int val) {

	log.share_print(id, val);
}
int main() {

	LogFile log;
	ofstream f;
	f.open("log.txt");
	f << "dsdads" << endl;
	//log.share_print("main thread", 1);

	thread t1(&thread_function ,ref(log), " thread 1", 1);
	thread t2(&thread_function, ref(log), " thread 2", 2);
	thread t3(&thread_function, ref(log), " thread 3", 3);
	t1.join();
	t2.join();
	t3.join();
	//getchar();
	return 0;

}

future 

//https://www.youtube.com/watch?v=SZQ6-pf-5Us
//线程间通信

/*

future--> pass the value from the child thread to the parent thread

promise--> pass the value from the parent thread to the child thread,some time in the future

*/



#include <future>
#include <iostream>
#include <mutex>
#include<condition_variable>

using namespace std;

long long factorial(int N) {
	long long res = 1;
	for (int i = 2; i <= N; i++)
	{
		res *= i;
	}

	cout << "result is " << res << endl;

	return res;
}

int main() {

	long long x;//x is a shared valuable between the child thread and the parent thread ---> first set the x in child thread, the the parent's thread get x value!!-->所以要互斥、同步 -->很麻烦!
	//thread t1(factorial, 4, ref(x));//I want to return the result from the child thread to the parent thread! 

	future<long long> fu = std::async(std::launch::async,factorial, 44);

	
	x = fu.get();

	//t1.join();

	cout << "main thread is " << x << endl;
	getchar();
	return 0;


}

promise

long long factorial(future<int> &f) {
	long long res = 1;

	int N = f.get();//等待主线程给值
	for (int i = 2; i <= N; i++)
	{
		res *= i;
	}

	cout << "result is " << res << endl;

	return res;
}

int main() {

	long long x;//x is a shared valuable between the child thread and the parent thread ---> first set the x in child thread, the the parent's thread get x value!!-->所以要互斥、同步 -->很麻烦!
	//thread t1(factorial, 4, ref(x));//I want to return the result from the child thread to the parent thread! 
	promise<int> p;
	future<int> f = p.get_future();
	future<long long> fu = std::async(std::launch::async,factorial, ref(f));

	
	p.set_value(4);//主线程在未来某个时候会给子线程一个值


	x = fu.get();

	//t1.join();

	cout << "main thread is " << x << endl;
	getchar();
	return 0;


}

share_future

long long factorial(shared_future<int> sf) {
	long long res = 1;

	int N = sf.get();//等待主线程给值,----每个future只能使用一次 get()!!!!
	for (int i = 2; i <= N; i++)
	{
		res *= i;
	}

	cout << "result is " << res << endl;

	return res;
}

int main() {

	long long x;//x is a shared valuable between the child thread and the parent thread ---> first set the x in child thread, the the parent's thread get x value!!-->所以要互斥、同步 -->很麻烦!
	//thread t1(factorial, 4, ref(x));//I want to return the result from the child thread to the parent thread! 
	promise<int> p;
	future<int> f = p.get_future();
	shared_future<int> sf = f.share();//shared_future cna be copied !!!!


	//future<long long> fu = std::async(std::launch::async,factorial, ref(f));//需要10个promise,因为每个future只能使用一次 get()!!!!
	//future<long long> fu1 = std::async(std::launch::async, factorial, ref(f));//----》使用 share_future
	//future<long long> fu2 = std::async(std::launch::async, factorial, ref(f));
	//10 threads....

	future<long long> fu = std::async(std::launch::async, factorial, sf);
	future<long long> fu1 = std::async(std::launch::async, factorial, sf);
	future<long long> fu2 = std::async(std::launch::async, factorial, sf);



	//shared_future<int> sf = 
	
	p.set_value(4);//主线程在未来某个时候会给子线程一个值
	//shared_future--->when the parent set value of 4,all the child threads will get the same value when they call the sf.get()

	x = fu.get();

	//t1.join();

	cout << "main thread is " << x << endl;
	getchar();
	return 0;


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值