一、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使用,進行一些觀察動作,比如查看計數等,賦值不會增加計數。