无锁栈与无锁队列

it2022-06-26  92

互斥的硬件支持

中断禁用

单处理器机器中,并发进程不能重叠只能交替,因此保证互斥,主要保证进程不被中断就可以了

禁用中断 临界区 启用中断 其他部分

专用机器指令

test_and_set

#include <thread> #include <vector> #include <iostream> #include <atomic> std::atomic_flag lock = ATOMIC_FLAG_INIT; void f(int n){ for (int cnt = 0; cnt < 100; ++cnt) { while (lock.test_and_set(std::memory_order_acquire)) // acquire lock ; // spin std::cout << "Output from thread " << n << '\n'; lock.clear(std::memory_order_release); // release lock }} int main(){ std::vector<std::thread> v; for (int n = 0; n < 10; ++n) { v.emplace_back(f, n); } for (auto& t : v) { t.join(); }} 在所有并发进程之前将flag设置为false,每次test_and_set都会设置flag是true,然后自动返回原始值 原始值为false,则还未锁,可以上锁并进入临界区,需要离开临界区时重新将flag设置为false原始值为true,则已上锁 详见C++:test_and_set

compare_and_swap

template< class T >bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj,  typename std::atomic<T>::value_type* expected,  typename std::atomic<T>::value_type desired, std::memory_order succ,                                               std::memory_order fail ) noexcept; compare_and_swap(obj, expected, desire)简称CAS,是一种乐观锁。 先检查obj所指的值是否和expected所指的值相同 若相同则将obj所指的值更新为desire,返回true若不同则将expectd所指的值更新为obj所指的值,返回false

详见C++:compare_and_swap

无锁栈与无锁队列的实现

参考

cppreference.comoperating system internals and design principles

转载于:https://www.cnblogs.com/qbits/p/11206306.html

相关资源:数据结构—成绩单生成器

最新回复(0)