ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

C++无锁数据结构:CAS(Compare-and-Swap)

2026/8/28 0:59:54 拓冰建站 浏览量
C++无锁数据结构:CAS(Compare-and-Swap)

在高并发场景下,传统锁机制带来的线程阻塞和上下文切换开销成为性能瓶颈。无锁数据结构通过原子操作实现线程安全,避免了锁的使用,成为高性能系统的关键技术。本文将深入探讨C++中基于CAS(Compare-and-Swap)的无锁数据结构实现原理、应用场景及实战技巧。

一、CAS原理解析

1.1 什么是CAS?

CAS是一种原子操作,包含三个参数:内存地址(V)、旧值(A)和新值(B)。操作逻辑如下:

  • 如果内存地址V中的值等于旧值A,则将该位置的值更新为新值B
  • 否则返回当前内存中的实际值

CAS操作通常由CPU硬件直接支持,是无锁编程的核心原语。

1.2 C++中的CAS实现

C++11在<atomic>头文件中提供了原子操作支持,其中compare_exchange_weakcompare_exchange_strong对应CAS操作:

#include <atomic>template<typename T>
bool atomic_compare_and_swap(std::atomic<T>& atom, T& expected, T desired) {return atom.compare_exchange_weak(expected, desired);
}
  • compare_exchange_weak:可能会虚假失败(即使值相等也可能返回false),但性能更高
  • compare_exchange_strong:保证不会虚假失败,但可能需要更多次重试

1.3 CAS的ABA问题

CAS操作存在ABA问题:当值从A变为B再变回A时,CAS会误认为值没有变化。解决方法通常是使用带版本号的原子变量:

template<typename T>
struct VersionedValue {T value;uint64_t version;bool operator==(const VersionedValue& other) const {return value == other.value && version == other.version;}
};std::atomic<VersionedValue<int>> atom;// 更新操作
void update(int new_value) {VersionedValue<int> expected = atom.load();VersionedValue<int> desired;do {desired.value = new_value;desired.version = expected.version + 1;} while (!atom.compare_exchange_weak(expected, desired));
}

二、基于CAS的无锁栈实现

2.1 单链表节点结构

template<typename T>
struct Node {T data;Node* next;Node(const T& value) : data(value), next(nullptr) {}
};

2.2 无锁栈核心实现

template<typename T>
class LockFreeStack {
private:std::atomic<Node<T>*> head;public:LockFreeStack() : head(nullptr) {}// 入栈操作void push(const T& value) {Node<T>* new_node = new Node<T>(value);new_node->next = head.load();while (!head.compare_exchange_weak(new_node->next, new_node)) {// CAS失败,说明有其他线程修改了head// 重新加载head并尝试}}// 出栈操作bool pop(T& value) {Node<T>* old_head = head.load();while (old_head != nullptr) {// 尝试将head指向下一个节点if (head.compare_exchange_weak(old_head, old_head->next)) {value = old_head->data;delete old_head;return true;}}return false; // 栈为空}
};

2.3 ABA问题处理

上述实现存在ABA问题,改进版使用带标记的指针:

template<typename T>
class LockFreeStack {
private:struct TaggedPointer {Node<T>* ptr;uint64_t tag;bool operator==(const TaggedPointer& other) const {return ptr == other.ptr && tag == other.tag;}};std::atomic<TaggedPointer> head;public:void push(const T& value) {Node<T>* new_node = new Node<T>(value);TaggedPointer old_head = head.load();do {new_node->next = old_head.ptr;TaggedPointer new_head = {new_node, old_head.tag + 1};} while (!head.compare_exchange_weak(old_head, new_head));}bool pop(T& value) {TaggedPointer old_head = head.load();while (old_head.ptr != nullptr) {TaggedPointer new_head = {old_head.ptr->next, old_head.tag + 1};if (head.compare_exchange_weak(old_head, new_head)) {value = old_head.ptr->data;delete old_head.ptr;return true;}}return false;}
};

三、无锁队列实现

3.1 Michael-Scott无锁队列

template<typename T>
class LockFreeQueue {
private:struct Node {T data;std::atomic<Node*> next;Node() : next(nullptr) {}Node(const T& value) : data(value), next(nullptr) {}};std::atomic<Node*> head;std::atomic<Node*> tail;public:LockFreeQueue() {Node* dummy = new Node();head.store(dummy);tail.store(dummy);}~LockFreeQueue() {T value;while (pop(value));delete head.load();}// 入队操作void enqueue(const T& value) {Node* new_node = new Node(value);Node* old_tail = tail.load();while (true) {// 尝试将新节点连接到队尾if (old_tail->next.compare_exchange_weak(nullptr, new_node)) {// 成功连接,尝试更新tail指针tail.compare_exchange_strong(old_tail, new_node);return;} else {// 连接失败,说明有其他线程已经更新了tailtail.compare_exchange_strong(old_tail, old_tail->next);old_tail = tail.load();}}}// 出队操作bool dequeue(T& value) {Node* old_head = head.load();while (true) {Node* next = old_head->next.load();if (next == nullptr) {return false; // 队列为空}// 尝试更新head指针if (head.compare_exchange_weak(old_head, next)) {value = next->data;delete old_head; // 释放原头节点(dummy或前一个节点)return true;}}}
};

四、CAS无锁算法的性能考量

4.1 优势

  • 无阻塞:线程不会因竞争而阻塞,减少上下文切换开销
  • 细粒度并发:允许多个线程同时操作不同部分的数据结构
  • 避免死锁:无需获取锁,从根本上消除死锁风险

4.2 劣势

  • 高竞争下性能下降:大量CAS失败导致频繁重试
  • 实现复杂度高:需要处理ABA问题、内存回收等复杂问题
  • 调试困难:非确定性的执行顺序增加调试难度

4.3 适用场景

  • 高并发低竞争:线程间冲突较少的场景
  • 实时系统:不允许长时间阻塞的场景
  • 高性能核心组件:如网络框架、数据库引擎

五、内存回收方案

无锁数据结构的一个关键挑战是安全地回收内存,避免"悬垂指针"问题。常见方案有:

5.1 Hazard Pointer

// 简化版Hazard Pointer实现
template<typename T>
class HazardPointer {
private:static constexpr int MAX_THREADS = 64;std::atomic<void*> hazard_pointers[MAX_THREADS];std::atomic<int> thread_count;thread_local static int thread_id;public:void* get_hazard_pointer() {if (thread_id == -1) {thread_id = thread_count.fetch_add(1);}return hazard_pointers[thread_id].load();}void set_hazard_pointer(void* ptr) {hazard_pointers[thread_id].store(ptr);}void clear_hazard_pointer() {hazard_pointers[thread_id].store(nullptr);}bool can_reclaim(Node<T>* node) {for (int i = 0; i < thread_count; ++i) {if (hazard_pointers[i].load() == node) {return false;}}return true;}
};

5.2 延迟删除

// 简化版延迟删除实现
template<typename T>
class LockFreeStack {
private:std::atomic<Node<T>*> head;std::atomic<int> delete_count;std::vector<Node<T>*> to_be_deleted;const int BATCH_SIZE = 100;public:bool pop(T& value) {Node<T>* old_head = head.load();while (old_head != nullptr) {if (head.compare_exchange_weak(old_head, old_head->next)) {value = old_head->data;// 延迟删除to_be_deleted.push_back(old_head);if (++delete_count >= BATCH_SIZE) {perform_deletion();}return true;}}return false;}void perform_deletion() {std::vector<Node<T>*> local;local.swap(to_be_deleted);delete_count = 0;for (auto node : local) {delete node;}}
};

六、总结与最佳实践

  1. 优先使用标准库实现:C++标准库提供了许多无锁容器,如std::atomicstd::queue的并发版本
  2. 谨慎使用无锁结构:仅在性能关键且锁竞争激烈的场景使用
  3. 解决ABA问题:使用带版本号的指针或Hazard Pointer
  4. 注意内存回收:避免使用裸指针,采用安全的内存回收机制
  5. 充分测试:无锁算法的正确性依赖于原子操作的顺序,需进行充分测试

无锁数据结构是一把双刃剑,正确使用能带来显著性能提升,但实现难度较大。开发者需根据具体场景权衡利弊,选择合适的实现方案。随着硬件和编程语言的发展,无锁编程将在高性能领域发挥越来越重要的作用。