Skip to content

Transactional memory

在阅读 C++ reference时,无意中发现了 "Transactional Memory (TM TS)",看了一下,发现它非常类似于Java的写法,遂决定将transactional memory的内容进行整理。

Transactional memory VS low-level thread synchronization

相比于low-level thread synchronization,transactional memory 的优势包括:

High-level abstraction

相比于low-level thread synchronization,transaction是一个 high-level abstraction,显然它能够隐藏非常多的底层细节,更容易理解,让programmer能够更好地编写线程安全的程序,在 wikipedia Software transactional memory # Conceptual advantages and disadvantages章节中,也有关于此的讨论。

关于transaction是一个非常好的抽象,参见Distributed-computing\Theory\Abstraction章节。

Higher concurrency

相比于low-level thread synchronization,transactional memory能够提供higher concurrency,这在:

1) wikipedia Transactional memory # Motivation

2) wikipedia Software transactional memory # Performance

中都有描述。

Pessimistic VS optimistic

Transactional memory是optimistic concurrency control,而lock是pessimistic。

wikipedia Transactional memory

In computer science and engineering, transactional memory attempts to simplify concurrent programming by allowing a group of load and store instructions to execute in an atomic way. It is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. Transactional memory systems provide high-level abstraction as an alternative to low-level thread synchronization. This abstraction allows for coordination between concurrent reads and writes of shared data in parallel systems.[1]

NOTE: 在语言层支持transactional memory的programming language,往往会提供语法糖来让programmer轻松地使用transactional memory;

Motivation

Low level thread synchronization constructs such as locks are pessimistic. Transactional memory provides optimistic concurrency control by allowing threads to run in parallel with minimal interference. The goal of transactional memory systems is to transparently support regions of code marked as transactions by enforcing atomicity, consistency and isolation.

NOTE: 原文第一段主要从optimistic和pessimistic的角度来进行说明。

Transactional memory意味着ACI(没有D,在DBMS中D是指durability)。

NOTE: 原文第二段主要介绍的transaction的原理。

With these constructs in place, transactional memory provides a high level programming abstraction by allowing programmers to enclose their methods within transactional blocks. Correct implementations ensure that data cannot be shared between threads without going through a transaction and produce a serializable outcome. For example, code can be written as:

def transfer_money(from_account, to_account, amount):
    """Transfer money from one account to another."""
    with transaction():
        from_account -= amount
        to_account   += amount

In the code, the block defined by "transaction" is guaranteed atomicity, consistency and isolation by the underlying transactional memory implementation and is transparent to the programmer.

Although transactional memory programs cannot produce a deadlock, programs may still suffer from a livelock or resource starvation. For example, longer transactions may repeatedly revert(重复) in response to multiple smaller transactions, wasting both time and energy.[2]

Hardware vs. software

The abstraction of atomicity in transactional memory requires a hardware mechanism to detect conflicts and undo any changes made to shared data.[3]

Software transactional memory provides transactional memory semantics in a software runtime library or the programming language,[9] and requires minimal hardware support (typically an atomic compare and swap operation, or equivalent). As the downside, software implementations usually come with a performance penalty, when compared to hardware solutions.

History

As of GCC 4.7, an experimental library for transactional memory is available which utilizes a hybrid implementation. The PyPy variant of Python also introduces transactional memory to the language.

NOTE: 关于C++对transactional memory的支持,参见C++\Language-reference\Basic-concept\Abstract-machine\Memory-model\Transactional-memory章节。

wikipedia Software transactional memory

In computer science, software transactional memory (STM) is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It is an alternative to lock-based synchronization. STM is a strategy implemented in software, rather than as a hardware component.

Performance

Unlike the locking techniques used in most modern multithreaded applications, STM is often very optimistic :

Conceptual advantages and disadvantages

In addition to their performance benefits[citation needed], STM greatly simplifies conceptual understanding of multithreaded programs and helps make programs more maintainable by working in harmony with existing high-level abstractions such as objects and modules. Lock-based programming has a number of well-known problems that frequently arise in practice

Implementations

C/C++

  • G++ 4.7 now supports STM for C/C++ directly in the compiler. The feature is still listed as "experimental", but may still provide the necessary functionality for testing.