蓝桥ROS机器人之现代C++学习笔记7.5 内存模型

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥ROS机器人之现代C++学习笔记7.5 内存模型相关的知识,希望对你有一定的参考价值。

学习代码如下:

#include <atomic>
#include <thread>
#include <vector>
#include <iostream>

using namespace std;
using namespace std::chrono;

const int N = 10000;

void relaxed_order() 
    cout << "relaxed_order: " << endl;

    atomic<int> counter = 0;
    vector<thread> vt;
    for (int i = 0; i < N; ++i) 
        vt.emplace_back([&]()
            counter.fetch_add(1, memory_order_relaxed);
        );
    
    auto t1 = high_resolution_clock::now();
    for (auto& t : vt) 
        t.join();
    
    auto t2 = high_resolution_clock::now();
    auto duration = ( t2 - t1 ).count();
    cout << "relaxed order speed: " << duration / N << "ns" << endl;


void release_consume_order() 
    cout << "release_consume_order: " << endl;

    atomic<int*> ptr;
    int v;
    thread producer([&]() 
        int* p = new int(42);
        v = 1024;
        ptr.store(p, memory_order_release);
    );
    thread consumer([&]() 
        int* p;
        while(!(p = ptr.load(memory_order_consume)));

        cout << "p: " << *p << endl;
        cout << "v: " << v << endl;
    );
    producer.join();
    consumer.join();


void release_acquire_order() 
    cout << "release_acquire_order: " << endl;

    int v;
    atomic<int> flag = 0;
    thread release([&]() 
        v = 42;
        flag.store(1, memory_order_release);
    );
    thread acqrel([&]() 
        int expected = 1; // must before compare_exchange_strong
        while(!flag.compare_exchange_strong(expected, 2, memory_order_acq_rel)) 
            expected = 1; // must after compare_exchange_strong
        
        // flag has changed to 2
    );
    thread acquire([&]() 
        while(flag.load(memory_order_acquire) < 2);

        cout << "v: " << v << endl; // must be 42
    );
    release.join();
    acqrel.join();
    acquire.join();


void sequential_consistent_order() 
    cout << "sequential_consistent_order: " << endl;

    atomic<int> counter = 0;
    vector<thread> vt;
    for (int i = 0; i < N; ++i) 
        vt.emplace_back([&]()
            counter.fetch_add(1, memory_order_seq_cst);
        );
    
    auto t1 = high_resolution_clock::now();
    for (auto& t : vt) 
        t.join();
    
    auto t2 = high_resolution_clock::now();
    auto duration = ( t2 - t1 ).count();
    cout << "sequential consistent speed: " << duration / N << "ns" << endl;


int main() 
    relaxed_order();
    release_consume_order();
    release_acquire_order();
    sequential_consistent_order();
    return 0;


复习一下参考文献中提过的一些概念: 

模型一致性

  1. 线性
  2. 顺序
  3. 因果
  4. 最终

内存顺序

  1. 宽松
  2. 释放/消费
  3. 释放/获取
  4. 顺序一致

以上是关于蓝桥ROS机器人之现代C++学习笔记7.5 内存模型的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥ROS机器人之现代C++学习笔记支持C++17(已完成)

蓝桥ROS机器人之现代C++学习笔记之路径规划

蓝桥ROS机器人之现代C++学习笔记2.5 模板

蓝桥ROS机器人之现代C++学习笔记7.3 期物

蓝桥ROS机器人之现代C++学习笔记资料

蓝桥ROS机器人之现代C++学习笔记3.1 Lambda 表达式