1

并发编程中锁的种类和概念

 3 years ago
source link: https://www.wencst.com/archives/402
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

并发编程中锁的种类和概念

作者: wencst 分类: Uncategorized,程序设计 发布时间: 2016-11-17 17:47 阅读: 1,854 次

悲观锁 – Pressimistic locking

  1. 一个线性在执行一个操作时持有对一个资源的独占锁。(互斥)
  2. 一般用在冲突比较可能发生的场景下

乐观锁 – Optimistic locking

  1. 尝试采用原子操作,而不需要持有锁;冲突可被检测,如果发生冲突,具有相应的重试逻辑
  2. 通常用在冲突较少发生的场景下

非阻塞算法 – Non-blocking algorithm

  1. 算法确保对线程间竞争共享资源时候,不会因为互斥而使任一线程的执行无限延迟;

无锁算法 – Lock-free algorithm

  1. 如果系统整个流程的执行是无阻塞的(系统某一部分可能被短暂阻塞),这种非阻塞算法就是无锁的。
  2. 无锁算法比传统的基于锁的算法对系统的开销更小,且更容易在多核多CPU处理器上扩展;
  3. 在实时系统中可以避免锁带来的延迟;
  4. CAS (compare and swap)或LL/SC(load linked/store conditional),以及内存屏障相关的指令经常被用在算法实现中。

无等待算法 – Wait-free algorithm

  1. 如果每个线程的执行都是无阻塞的,这种非阻塞算法就是无等待的(比无锁算法更好)

如果文章对您有用,扫一下支付宝的红包,不胜感激!

欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK