给线程传个参数
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;
}
}
给线程传个参数
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;
}
}
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
配置toolchain后,如配置qt5的toolchain后,记得把toolchain移到最上面,不然不会用这个!
#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
for (std::map
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
auto r2 = ref(x);
// 改变值。不可直接r=10
r.get() = 10;
二
int a (10), b (20), c (30);
// 一组reference
// 用&是不可以用创造这种数组的,如int&[] arr {a,b,c}
reference_wrapper
一 future可通过
1. async(myfunc, param1)
2. promise
3. packages_task
构造
二 async()中的函数,不能涉及sharing data
while (!ready) {
std::this_thread::yield();
}