一、thread.joinable(),用于判断线程是否在运行,可结束状态。一般用于上一个线程的回收。
文档:http://www.cplusplus.com/reference/thread/thread/joinable/
示例:
if(t.joinable())
t.join();
二、lock_guard和unique_lock,前者只是简单的构造获得锁,析构释放锁。后者增加一些try_类别的api。
三、自动锁在函数中需要释放,用{}括起来,代码块结束会释放局部变量
例:
{
std::lock_guard<std::mutex> lck (mtx);
加锁逻辑代码1
}
//其他逻辑
{
std::lock_guard<std::mutex> lck (mtx);
加锁逻辑代码2
}
四、push_back和emplace_back,emplace和push等,c++11增加了移动拷贝构造函数,emplace相对push会减少对于对象类型元素的拷贝。
五、std::chrono::time_point,标准库对绝对时间点的封装。
六、枚举类用法
enum class : type //这里的type可以是任意整数类型,比如int、long、unsigned int等
{
name = val,
};
举例说明一下使用场景,c++98中的用法如下,值全默认转为整数,会造成不同的类型之间认为相等,if判断成立
enum E1
{
E1_1 = 1,
};
enum E2
{
E2_1 = 1,
};
int main()
{
if(E1_1 == E2_1)
cout << "ok" << endl;
}
枚举类用法如下,会报类型不匹配错误,以此来避免隐式类型转换,error: no match for ‘operator==’ (operand types are ‘E1’ and ‘E2’)
enum class E1 : int
{
E1_1 = 1,
};
enum class E2
{
E2_1 = 1,
};
int main()
{
auto e1 = E1::E1_1;
auto e2 = E2::E2_1;
if(e1 == e2)
cout << "ok" << endl;
}
七、类型重命名
1、typedef 原类型 新类型;
2、using 亲类型 = 原类型;
例:
using Int = int;
using Vint = vector<int>;
template <typename T>
using VT = vector<T>;
八、auto_ptr、shared_ptr、unique_ptr、weak_ptr,4种智能指针
auto_ptr简单实现,有缺陷,废弃。
shared_ptr共享指针,可赋值等。
unique_ptr独享指针,多用于局部代码。
weak_ptr配合unique_ptr使用,进行一些观察动作,比如查看计数等,赋值不会增加计数。