IT评测·应用市场-qidao123.com技术社区

标题: C++手撕共享指针、多线程交替、LRU缓存 [打印本页]

作者: 伤心客    时间: 2025-3-26 06:46
标题: C++手撕共享指针、多线程交替、LRU缓存
 1. 共享指针

  1. #include <atomic>
  2. #include <iostream>
  3. template <typename T> class sharedptr {
  4. private:
  5.   T *ptr;
  6.   std::atomic<size_t> *count;
  7. public:
  8.   sharedptr(T *p) : ptr(p), count(new std::atomic<size_t>(1)) {}
  9.   sharedptr(const sharedptr &other) : ptr(other.ptr), count(other.count) {
  10.     (*count)++;
  11.   }
  12.   sharedptr(sharedptr &&other) : ptr(other.ptr), count(other.count) {
  13.     other.ptr = nullptr;
  14.     other.count = nullptr;
  15.   }
  16.   // 拷贝赋值运算符
  17.   // 何时调用?当用一个 ​已存在的对象(左值)​
  18.   // 赋值给另一个已存在的对象时: Myclass a,b;
  19.   sharedptr &operator=(const sharedptr &other) {
  20.     if (this != &other) {
  21.       release();
  22.       ptr = other.ptr;
  23.       count = other.count;
  24.       (*count)++;
  25.     }
  26.     return *this;
  27.   }
  28.   // 移动赋值运算符
  29.   sharedptr &operator=(sharedptr &&other) {
  30.     if (this != &other) {
  31.       ptr = other.ptr;
  32.       count = other.count;
  33.       other.ptr = nullptr;
  34.       other.count = nullptr;
  35.     }
  36.     return *this;
  37.   }
  38.   void release() {
  39.     if (count != nullptr && (--(*count)) == 0) {
  40.       delete ptr;
  41.       delete count;
  42.     }
  43.   }
  44.   ~sharedptr() { release(); }
  45.   T *operator->() { return ptr; }
  46.   T &operator*() { return *ptr; }
  47. };
  48. int main() {}
复制代码
1. 快速影象

2. 多线程交替打印

代码:

  1. #include <condition_variable>
  2. #include <iostream>
  3. #include <mutex>
  4. #include <thread>
  5. int m;
  6. std::mutex mtx;
  7. std::condition_variable cv;
  8. void print(int target) {
  9.   for (int i = 0; i < 10; i++) {
  10.     // 这行代码相当重要
  11.     std::unique_lock<std::mutex> lock(mtx);
  12.     // 条件谓词不接受参数
  13.     cv.wait(lock, [&]() { return m == target; });
  14.     std::cout << m << std::endl;
  15.     m = (m + 1) % 3;
  16.     cv.notify_all();
  17.   }
  18. }
  19. int main() {
  20.   m = 0;
  21.   std::thread t1(print, 0);
  22.   std::thread t2(print, 1);
  23.   std::thread t3(print, 2);
  24.   t1.join();
  25.   t2.join();
  26.   t3.join();
  27. }
复制代码
1. std::unique_lock<std::mutex>对象lock和std::mutex对象mtx是什么关系?

std::unique_lock<std::mutex> 是一个管理互斥量(std::mutex)的 RAII(资源获取即初始化)包装器。两者的关系如下:

2. 简述一下cv.wait和notify做了什么?


3. LRU缓存

  1. class LRUCache {
  2. private:
  3.     int capacity;
  4.     list<pair<int,int>> mylist;
  5.     unordered_map<int,list<pair<int,int>>::iterator> umap;
  6. public:
  7.     LRUCache(int capacity):capacity(capacity) {
  8.     }
  9.    
  10.     int get(int key) {
  11.         if(umap.find(key)==umap.end())  {return -1;}
  12.         int value=umap[key]->second;
  13.         mylist.erase(umap[key]);
  14.         mylist.push_front({key,value});
  15.         umap[key]=mylist.begin();
  16.         return value;
  17.     }
  18.    
  19.     void put(int key, int value) {
  20.         if(umap.find(key)!=umap.end()){
  21.             mylist.erase(umap[key]);
  22.             mylist.push_front({key,value});
  23.             umap[key]=mylist.begin();
  24.         }   else{
  25.                 if(umap.size()==capacity){
  26.                     //  不能直接删除end()
  27.                     // umap.erase(mylist.end());
  28.                     int old_key=mylist.back().first;
  29.                     umap.erase(old_key);
  30.                     mylist.pop_back();
  31.                 }
  32.                 mylist.push_front({key,value});
  33.                 umap[key]=mylist.begin();
  34.         }
  35.     }
  36. };  
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4