分类: 计科
兼容ie
- 不强制https,http和https共存
- 前端请求后端时,url用//example.com, 此称为自适应https
c++ 智能指针 unique_ptr
#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));
c++ auto 使用场景
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)
// 这样改类型也方便改
c++ ref
一
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
c++ Communication Tasks
一 future可通过
1. async(myfunc, param1)
2. promise
3. packages_task
构造
二 async()中的函数,不能涉及sharing data
c++ yield适用场景
while (!ready) {
std::this_thread::yield();
}
volatile使用场景
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次 到寄存器,之后就不再读取了
c++ 线程间的sharing data
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);
wsl编译+windos编辑的最佳实践
以jetbranins系的clion为例,setting中的toolchain那里,选用wsl。
详见jetbrains官网的关于这个的教程连接https://www.jetbrains.com/help/clion/how-to-use-wsl-development-environment-in-clion.html