给线程传个参数

std::reference_wrapper<bool> p_quit

接着线程里判断是否退出。

bool b = false;
thread t1(func1,arg1,std::ref(b));
void funct1(arg1, std::reference_wrapper<bool> p_quit) {
    while (true) {
        if (p_quit) break;
    }
}

  1. vector里面 从最佳实践的意义上 肯定是不能放智能指针的,因为vector就会管理对象的销毁,不需要智能指针。但对于qt5来说,只能存指针,因为好像复制widget之类的会因为不允许而报错。
2 声明unique_ptr再使用
p = std::make_unique<T>(construct_param1_for_T,...))
p.reset(new T(construct_param1_for_T,...))

//
#include <QObject>

class Counter : public QObject
{
    Q_OBJECT

public:
    Counter() { m_value = 0; }

    int value() const { return m_value; }

public slots:
    // 不是调用emit的都要声明为slots,这里声明是因为要让他成为signal
    // slots的参数,可以少于signal的参数。
    void setValue(int value) {
        if (value != m_value) {
            m_value= value;
            emit valueChanges(value);
        }
    }

// 定义一个signal, signal只能是void类型
// signal代码有moc自动生成,不需要自己去实现这个方法
signals:
    void valueChanged(int newValue);

private:
    int m_value;
};
// 使用示例1 不好的做法
    connect(ui->startBtn, &QPushButton::released, this,&MainWindow::startGame);
// 使用示例2 安全的做法
// 这里SIGNAL,SLOT宏,估计是给 参数 生成 函数签名,从而判断是否安全
// 出现在SLOT宏中的函数,需要 声明类成员函数时,用public signal:
connect(ui->startBtn, SIGNAL(released()), this, SLOT(startGame()));
connect(scrollBar, SIGNAL(valueChanged(int)),label,  SLOT(setNum(int)));

-----------------------------

使用Lambda:
https://blog.csdn.net/csm201314/article/details/77914281

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