#include <iostream>
#include <condition_variable>
#include <atomic>
#include <thread>
#include <future>
#include <vector>

using namespace std;
class A {
public:
    A(int _x): x(_x) {

    }
    A () {
        x = 0;
    }
    int x;
};
int main()
{
    unique_ptr<A> p (new A);
    cout<<p->x<<endl;
    unique_ptr<A> p2 (new A(500));
    cout<<p2->x<<endl;

    // 从一个已初始化的类,
    A a(12);
    unique_ptr<A> p3 (&a);
    cout<<p3->x<<endl;

   /*
   x unique_ptr<A> p4; x
   x p4 = new A(333) x
   */
}
void f1(unique_ptr<int> p);
unique_ptr<int> p = make_unique<int>(1);
// 这里必须move,某种compile error
f1(move(p));

1.不care类型时(、类型太长时)

std::map m;
for (std::map::const_iterator it = m.cbegin();
it != m.cend(); ++it)
{ /*…*/ }

for (auto it = m.cbegin(); it != m.cend(); ++it)
{ /*…*/ }

2. int a = new int(42)这种左右两边都写类型时,
可以直接auto a = new int(42)
// 这样改类型也方便改


int x=1, y=2;
reference_wraper r = x;
auto r2 = ref(x);

// 改变值。不可直接r=10
r.get() = 10;


int a (10), b (20), c (30);
// 一组reference
// 用&是不可以用创造这种数组的,如int&[] arr {a,b,c}
reference_wrapper refs[] = {a,b,c}

volatile int i=0;
volatile用于不让编译器优化
1.
volatile int a;
a = 10;
// 让a被立即赋值为10,而不是等到之后用a时才给她赋值。
2.
volatile bool b = false
while (!b) {
// 不改变b的值的代码
}
// 如果不加volatile,编译器可能就只从内存中读取b的值1次 到寄存器,之后就不再读取了

mutex mtx;
1 直接调用mtx.lock、mtx.unlock 不如 lock(mtx),因为前者可能死锁
2 lock 、try_lock(mtx) 可以一次锁多个、保证不死锁,但try_lock有返回值用于判断是否成功。
3 try_lock(mtx)有时不如unique_lock lck(mtx),因为后者可以在lcx销毁时自动释放锁。unique_lock<mutex> lcx(mtx)这种构造会一直阻塞到锁住。
最佳实践是
mutex m1;
unique_lock<mutex> lcx(m1,std::defer_lock);
try_lock(lcx);