方法一针对一写多读场景,可以使用智能指针代替锁 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include <iostream>#include <vector>#include <map>#include <memory>#include <atomic>#include <thread>void LoadMap(std::shared_ptr<std::map<int, int>> map_ptr) { std::cout << "LoadMap" << std::endl; for (int i = 0; i < 10000; i++) { (*map_ptr)[i] = (*map_ptr)[i] + 1; }}class DoubleBuffer { private: std::vector<std::shared_ptr<std::map<int, int>>> buffers_; std::atomic_size_t curr_; public: DoubleBuffer() : curr_(0) { buffers_.push_back(std::make_shared<std::map<int, int>>()); buffers_.push_back(std::make_shared<std::map<int, int>>()); for (int i = 0; i < 10000; i++) { (*buffers_[0])[i] = 0; (*buffers_[1])[i] = 0; } } void set() { size_t prepare = 1 - curr_.load(); while(buffers_[prepare].use_count() > 1) { continue; } LoadMap(buffers_[prepare]); curr_ = prepare; } int get(int key) { return (*buffers_[curr_.load()])[key]; }};int main() { DoubleBuffer db; // 定义写线程,每隔1s写一次 std::thread t1([&db](){ for(int i = 0; i < 3; i++) { db.set(); } }); // 定义读线程1 std::thread t2([&db](){ for(int i = 0; i < 10000; i++) { std::cout << db.get(i) << std::endl; } }); // 定义读线程2 std::thread t3([&db](){ for(int i = 0; i < 10000; i++) { std::cout << db.get(i) << std::endl; } }); t1.join(); t2.join(); t3.join(); return 0;}