What is Transactional memory?

A mechanism that runs critical sections speculatively, committing them as a whole if there is no conflict and rolling back and retrying if there is.

Field: Multicore & Synchronization

Japanese name: トランザクショナルメモリ

The idea

It applies the idea of database transactions to memory reads and writes. The program simply states that a region is to be executed as a single unit. A lock passes threads through one at a time merely because they might conflict. Transactional memory lets several threads execute at once in the expectation that they will not conflict, and cancels and retries one of them only when they actually touch the same data and at least one of them writes it. If conflicts are rare, more work proceeds concurrently than with a lock.

Hardware implementation

A processor implements this using its cache. Lines read or written inside the transaction are marked, and the results of writes are kept inside the cache, invisible from outside. If another core tries to write a line that was read, or to read or write a line that was written, a conflict is detected. On a conflict, the writes are discarded and the registers are restored to their values at the start. If no conflict occurs up to the end, the writes are committed all at once. The transaction is also cancelled, however, if the written data does not fit in the cache or if an interrupt arrives. Success is never guaranteed. An alternative path that takes an ordinary lock and executes when the transaction fails is therefore always provided.

Current status

The feature introduced in x86 processors was found to have defects and security problems and has been disabled in many products. Other processors have features based on the same idea. There are also implementations that do not use processor features and instead record reads and writes in a library or as a language feature; these carry a larger recording overhead. In either approach, operations that cannot be undone, such as output to a screen or communication, cannot be placed inside a transaction.

Try it: questions about Transactional memory

Which describes the idea of hardware transactional memory?

  1. ACritical sections run speculatively; with no conflict with other threads they commit as a whole, and on conflict they abort and retry
  2. BAll memory reads and writes are logged to disk like bank transactions
  3. CMemory writes are forbidden and only reads are allowed
Show answer and explanation

Answer: A. Critical sections run speculatively; with no conflict with other threads they commit as a whole, and on conflict they abort and retry

In transactional memory, a critical section runs as a transaction whose writes stay tentative and invisible to others. If no other thread touched the same data, all writes commit at once at the end; on conflict, everything is rolled back and retried or the code falls back to an ordinary lock. When conflicts are rare, even coarse locks can achieve parallelism close to fine-grained locking.

FAQ

Is it always faster than a lock?

It is faster when conflicts are few. When they are frequent, cancellations and retries increase and it becomes slower than a lock.

Can deadlock occur?

A transaction itself takes no lock, so it cannot deadlock. The alternative path used on failure does use a lock, however, and that part needs the same care as any lock.

Books on Multicore & Synchronization Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary