西维蜀黍

【Java】关键字 - volatile

背景

volatile是Java提供的一种轻量级的同步机制。与 synchronized 块相比(synchronized通常称为重量级锁),volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized 的一部分。

  ...


【Operating System】文件描述符(File Descriptor)

文件描述符(File Descriptor)

In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.

File descriptors typically have non-negative integer values, with negative values being reserved to indicate “no value” or error conditions.

File descriptors are a part of the POSIX API. Each Unix process (except perhaps daemons) should have three standard POSIX file descriptors, corresponding to the three standard streams - STDIN (standard input) , STDOUT (standard output) and STDERR (standard error) .

Integer value Name <unistd.h> symbolic constant <stdio.h> file stream
0 Standard input STDIN_FILENO stdin
1 Standard output STDOUT_FILENO stdout
2 Standard error STDERR_FILENO stderr
  ...


【Database】Transactions - 两阶段锁(Two-phase Locking)

注意两阶段锁(Two-phase locking,2PL)需要和两阶段提交(Two-phase commit)区别开来。 两阶段锁(Two-pha   ...


【Programming】并发编程(Concurrent Programming)

背景

一直以来,硬件的发展极其迅速,也有一个很著名的"摩尔定律",可能会奇怪明明讨论的是并发编程为什么会扯到了硬件的发展,这其中的关系应该是多核CPU的发展为并发编程提供的硬件基础。摩尔定律并不是一种自然法则或者是物理定律,它只是基于认为观测数据后,对未来的一种预测。按照所预测的速度,我们的计算能力会按照指数级别的速度增长,不久以后会拥有超强的计算能力,正是在畅想未来的时候,2004年,Intel宣布4GHz芯片的计划推迟到2005年,然后在2004年秋季,Intel宣布彻底取消4GHz的计划,也就是说摩尔定律的有效性超过了半个世纪戛然而止。但是,聪明的硬件工程师并没有停止研发的脚步,他们为了进一步提升计算速度,而不是再追求单独的计算单元,而是将多个计算单元整合到了一起,也就是形成了多核CPU。短短十几年的时间,家用型CPU,比如Intel i7就可以达到4核心甚至8核心。而专业服务器则通常可以达到几个独立的CPU,每一个CPU甚至拥有多达8个以上的内核。因此,摩尔定律似乎在CPU核心扩展上继续得到体验。因此,多核的CPU的背景下,催生了并发编程的趋势,通过并发编程的形式可以将多核CPU的计算能力发挥到极致,性能得到提升

  ...


【Concurrent Control】乐观并发控制(Optimistic Concurrency Control)与悲观并发控制(Pessimistic Concurrency Control)

Update

2020.6.1 update/ 2021.5 update:

今天和室友讨论,发现大家都看的是 乐观锁、悲观锁,这一篇就够了!来学习乐观并发控制和悲观并发控制。

然而,这篇文章中提到了“乐观锁”的概念,这个词这可能会带来confusion

  • 因为“乐观并发控制”本身 imply 使用到了锁(lock),而锁本来就是悲观的(pessimistic),这与乐观(optimistic)相互矛盾
  • 另外,乐观并发控制其实描述的是一种进行并发控制(concurrent control)的思想,在这种思想中并不存在锁
  • 因而,就可能导致 confusion
  • 如果称为乐观并发控制(optimistic concurrent control),就会更准确。因而在本文中,不会使用“乐观锁”这个词,而是使用乐观并发控制。
  • 也可能称为无锁并发控制(lock-free concurrent control)

其实,搜索一下"optimistic locking",发现仍然还是有很多的结果,这说明这个confusion其实不是因为是翻译错误的原因。

今天突然意识到了这个问题,于是重新更新了这篇 blog。

Background

并发控制(Concurrency Control)

需要并发控制(Concurrency Control),其实是为了实现某些一致性规则(consistency rules)。

需要并发控制的一些场景:

  • 实现数据库的 ACID 中的一致性(consistency)
  • 编程中的实现互斥(mutual exclusion),比如Java中的 synchrinzed 方法

对应地,如果需要并发控制机制,但又没有应用任何并发控制机制,自然会产生某些问题,典型的包括:

  • Read-copy-update
  • The lost update problem: A second transaction writes a second value of a data-item (datum) on top of a first value written by a first concurrent transaction, and the first value is lost to other transactions running concurrently which need, by their precedence, to read the first value. The transactions that have read the wrong value end with incorrect results.
  • The dirty read problem: Transactions read a value written by a transaction that has been later aborted. This value disappears from the database upon abort, and should not have been read by any transaction (“dirty read”). The reading transactions end with incorrect results.
  • The incorrect summary problem: While one transaction takes a summary over the values of all the instances of a repeated data-item, a second transaction updates some instances of that data-item. The resulting summary does not reflect a correct result for any (usually needed for correctness) precedence order between the two transactions (if one is executed before the other), but rather some random result, depending on the timing of the updates, and whether certain update results have been included in the summary or not.

悲观并发控制(Pessimistic Concurrency Control)与乐观并发控制(Optimistic Concurrency Control)

乐观并发控制(Optimistic Concurrency Control)对应于生活中乐观的人总是想着事情往好的方向发展,悲观并发控制(Pessimistic Concurrency Control)对应于生活中悲观的人总是想着事情往坏的方向发展。这两种人各有优缺点,不能不以场景而定说一种人好于另外一种人。

  ...