一些經常使用的 std 類型

std::allocatorhtml

  標準庫中包含一個名爲allocator的類,容許咱們將分配和初始化分離。使用allocator一般會提供更好的性能和更靈活的內存管理能力。多線程

  標準庫allocator類定義在頭文件memory中,它幫助咱們將內存分配和對象構造分離開來。它提供一種類型感知的內存分配方法,它分配的內存是原始的、未構造的。函數

  allocator支持的操做,以下:佈局

  

  下面是一段標準用法:性能

int test_allocator_1()
{
    std::allocator<std::string> alloc; // 能夠分配string的allocator對象
    int n{ 5 };
    auto const p = alloc.allocate(n); // 分配n個未初始化的string
 
    auto q = p; // q指向最後構造的元素以後的位置
    alloc.construct(q++); // *q爲空字符串
    alloc.construct(q++, 10, 'c'); // *q爲cccccccccc
    alloc.construct(q++, "hi"); // *q爲hi
 
    std::cout << *p << std::endl; // 正確:使用string的輸出運算符
    //std::cout << *q << std::endl; // 災難:q指向未構造的內存
    std::cout << p[0] << std::endl;
    std::cout << p[1] << std::endl;
    std::cout << p[2] << std::endl;
 
    while (q != p) {
        alloc.destroy(--q); // 釋放咱們真正構造的string
    }
 
    alloc.deallocate(p, n);
 
    return 0;
}

參考:this

一、https://blog.csdn.net/fengbingchun/article/details/78943527spa

std::recursive_mutex.net

  定義於頭文件 <mutex>。線程

class recursive_mutex;  // C++11 起

  

recursive_mutex 類是同步原語,能用於保護共享數據免受從個多線程同時訪問。3d

recursive_mutex 提供排他性遞歸全部權語義:

  • 調用方線程在從它成功調用 lock 或 try_lock 開始的時期裏佔有 recursive_mutex 。此時期間,線程能夠進行對 lock 或 try_lock 的附加調用。全部權的時期在線程調用 unlock 匹配次數時結束。
  • 線程佔有 recursive_mutex 時,若其餘全部線程試圖要求 recursive_mutex 的全部權,則它們將阻塞(對於調用 lock )或收到 false 返回值(對於調用 try_lock )。
  • 可鎖定 recursive_mutex 次數的最大值是未指定的,但抵達該數後,對 lock 的調用將拋出 std::system_error 而對 try_lock 的調用將返回 false 。

若 recursive_mutex 在仍爲某線程佔有時被銷燬,則程序行爲未定義。 recursive_mutex 類知足互斥 (Mutex) 和標準佈局類型(StandardLayoutType) 的全部要求。

  

  

參考:

一、https://zh.cppreference.com/w/cpp/thread/recursive_mutex

std::condition_variable

#include <condition_variable>    // std::condition_variable

std::mutex mtx; // 全局互斥鎖.
std::condition_variable cv; // 全局條件變量.
bool ready = false; // 全局標誌位.

void do_print_id(int id)
{
    std::unique_lock <std::mutex> lck(mtx);
    while (!ready) // 若是標誌位不爲 true, 則等待...
        cv.wait(lck); // 當前線程被阻塞, 當全局標誌位變爲 true 以後,
// 線程被喚醒, 繼續往下執行打印線程編號id. std::cout << "thread " << id << '\n'; } void go() { std::unique_lock <std::mutex> lck(mtx); ready = true; // 設置全局標誌位爲 true. cv.notify_all(); // 喚醒全部線程. } int main() { std::thread threads[10]; // spawn 10 threads: for (int i = 0; i < 10; ++i) threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n"; go(); // go! for (auto & th:threads) th.join(); return 0; }

  

  第一種狀況下,在線程被阻塞時,該函數會自動調用 lck.unlock() 釋放鎖,使得其餘被阻塞在鎖競爭上的線程得以繼續執行。另外,一旦當前線程得到通知(notified,一般是另外某個線程調用 notify_* 喚醒了當前線程),wait() 函數也是自動調用 lck.lock(),使得 lck 的狀態和 wait 函數被調用時相同。

  在第二種狀況下(即設置了 Predicate),pred 至關於資源數量。只有當 pred 條件爲 false 時調用 wait() 纔會阻塞當前線程,而且在收到其餘線程的通知後只有當 pred 爲 true 時纔會被解除阻塞。

#include <condition_variable>    // std::condition_variable

std::mutex mtx;
std::condition_variable cv;

int cargo = 0;
bool shipment_available()
{
    return cargo != 0;
}

// 消費者線程.
void consume(int n)
{
    for (int i = 0; i < n; ++i) {
        std::unique_lock <std::mutex> lck(mtx);
        cv.wait(lck, shipment_available);
        std::cout << cargo << '\n';
        cargo = 0;
    }
}

int main()
{
    std::thread consumer_thread(consume, 10); // 消費者線程.

    // 主線程爲生產者線程, 生產 10 個物品.
    for (int i = 0; i < 10; ++i) {
        while (shipment_available())
            std::this_thread::yield();
        std::unique_lock <std::mutex> lck(mtx);
        cargo = i + 1;
        cv.notify_one();
    }

    consumer_thread.join();

    return 0;
}

std::condition_variable_any 介紹

  與 std::condition_variable 相似,只不過 std::condition_variable_any 的 wait 函數能夠接受任何 lockable 參數,而 std::condition_variable 只能接受 std::unique_lock<std::mutex> 類型的參數,除此之外,和 std::condition_variable 幾乎徹底同樣。

 

參考:

一、http://www.noobyard.com/article/p-fvmhcgph-ch.html