Singleflight 机制 介绍一句话介绍 singleflight: 当有多个 go-routine 同时执行相同的请求时 (e.g. 调用相同的函数), 只有第一个到达的 go-routine 能够去执行这个请求, 其他 go-routine 会保持阻塞, 等待执行请求的协程共享请求返回的结果. singleflight 可以用来避免缓存击穿 (当某一个热点数据 (key) 过期之后, 大量的请求在缓存无法得到结果, 从 2024-03-14 Golang #缓存优化, C++
C++ 并发编程总结 本篇文章对 C++ 并发编程中多线程的同步方式做一个总结. 互斥量 (Mutex) 保证在多线程环境下的同一个时间段内只会有一个线程访问共享资源/临界区 (critical section) <mutex> 头文件定义了四种主要的 mutex std::mutex std::recursive_mutex std::time_mutex std::recursive_time 2024-02-23 C++
Grep 常见命令选项 grep 命令常用选项 -i: 忽略大小写进行匹配, 然后显示符合的行 $ cat test.txt This is a random text file* AAA()* bbb aaa BBB aabbcc ccc**& CCC hello world?/* $ grep -i aaa test.txt AAA()* aaa -v: 反向查找, 只打印不匹配的行 (下面例子打 2023-12-09 Linux #Tool
C++ 类型转换 static_castint main() { double d = 3.124; int i = static_cast<int>(d); std::cout << i << std::endl; // 打印 3, 转换涉及到值的截断 Base* base = new Derived(); std: 2023-11-18 C++
Lua 闭包的定义和应用 函数定义在了解闭包之前,我们先看一下 function 在 Lua 中的定义: Functions as first-class values: Function is a value with the same rights as more conventional values like numbers and strings. A program can store functions i 2023-11-13 Lua
LSM 树介绍和优化 这篇文章的设计思路来源于此: B 站视频 不同于 B+ 树适用于读多写少的场景, LSM 树更适合用于写多读少的场景. 像是 LevelDB 和 RocksDB 都是基于 LSM 树创建的. 整篇文章的大致思路如图所示. LSM 树的构建思路推演 就地写 v.s. 追加写LSM 树是基于追加写操作开始构建的. 我们首先可以看看追加写和就地写之间的区别 就地写: 首先在文件中找出需要修改的数据 2023-10-27 Database #Data Structure
常用 Git 相关命令 记录一些我需要常用的 git 命令行 Link to a remote repoIf you want to push you local stuff to a remote repository in Github, do the following commands First go the the target directory, and commit the updates. git 2023-10-27 Git
左值引用 & 右值引用 1. 左值引用 & 常引用说起左值引用, 我们需要先讨论一下左值是什么. 左值可以简单概括为可以取地址的, 有名字的非匿名对象, 在一个表达式结束之后还是可以存活; 而右值刚好相反, 右值不能取地址, 也没有名字, 比如立即数或者是函数的返回值 (非引用值). 下面是左值引用的一些例子 int origin = 10; int &ref = origin; ref = 20; / 2023-10-27 C++
智能指针 std::unique_ptr 的定义和应用 Unique poiter 和原始指针一样快, 而且她不能复制, 只能转移所有权 (move-only). unique_ptr 定义和使用unique_ptr 的创建有两种方法 #include <stdio.h> #include <iostream> #include <memory> #include <string.h> struct P 2023-10-27 C++ #Notes of Effective Modern C++
论 std::weak_ptr 打破 std::shared_ptr 的循环引用 根据 weak_ptr 的定义, 他可以作为观察者 (Observer) 来检测 shared_ptr 指向的对象是否存在 (也就是指针悬空); 除此之外, weak_ptr 还可以用于打破 shared_ptr 的环状结构. 这篇文章会通过一个例子展示为什么 weak_ptr 在这个场景下的应用. shared_ptr 的环状结构struct Person; struct Team { 2023-10-27 C++