锁的缺点
锁定被迫交出时间片。
锁定意味着阻塞,多个线程(进程)排队获取资源,无法充分发挥系统性能。
锁定的阻塞无法通过fd进行通知,对性能有进一步的影响(理想的服务器模型是全局一处阻塞统一等待消息)。
一些锁限制了必须使用线程的方式进行开发,而线程无法充分利用系统的内存。
pthread库在特殊情况下可能产生饥饿的情况。
无锁编程的思路
加锁的根本起因是什么?
资源竞争。
解决资源竞争的思路有哪些?
分资源:资源进一步分配,各个资源获得方不相往来。
分功能:对资源进行规划,各自处理不同功能。
做冗余:对资源进行冗余,对当前资源进行切换。
二次检查:不加锁执行后,检查是否被修改(CAS)。
无锁编程
少锁
原子操作与忙等待
CAS解法与ABA问题
seqlock
免锁
实战无锁编程
数据与进程对等的处理
单一生产者与单一消费者进程
下面让我们一个一个的来梳理无锁编程的内容吧。
版权声明:本文为博主原创文章,未经博主允许不得转载。