转自:https://www.youtube.com/watch?v=IBu5ka1MQ7w
unique_lock
// https://www.youtube.com/watch?v=IBu5ka1MQ7w
//Lazy Initialization
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class LogFile
{
public:
LogFile() {
//_f.open("log.txt");
};
~LogFile() {};
void share_print(string id,int val) {
//unique_lock locker2(_mu_open);//_f只打开一次,但每个进程都要lock一下再判断,浪费时间!
//if (!_f.is_open()) {
// _f.open("log.txt");//Lazy Initialization
//}
call_once(_flag, [&]() {_f.open("log.txt"); });//只打开一次
//lock_guard guard(_mu);
//unique_lock比lock_guard更加灵活,可以随时 lock()或者 unlock()
//unique_lock可以移动(move)!!
unique_lock locker(_mu,defer_lock);//defer_lock 延迟上锁!
//do somthing else
locker.lock();
_f << "From " < 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
#include
#include
#include
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 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 &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 p;
future f = p.get_future();
future 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 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 p;
future f = p.get_future();
shared_future sf = f.share();//shared_future cna be copied !!!!
//future fu = std::async(std::launch::async,factorial, ref(f));//需要10个promise,因为每个future只能使用一次 get()!!!!
//future fu1 = std::async(std::launch::async, factorial, ref(f));//----》使用 share_future
//future fu2 = std::async(std::launch::async, factorial, ref(f));
//10 threads....
future fu = std::async(std::launch::async, factorial, sf);
future fu1 = std::async(std::launch::async, factorial, sf);
future fu2 = std::async(std::launch::async, factorial, sf);
//shared_future 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;
}