一些经常使用的 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.javashuo.com/article/p-bzekugdm-ch.html

相关文章
相关标签/搜索