Every computer you own runs dozens of threads at once. Each thread reads and writes shared memory â a bank balance, a cache entry, a game state â and if two threads collide on the same location at the same time, the result can be a corrupted value that no programmer intended.
The classic solution is the lock: before touching shared data, grab a mutex; when done, release it. Locks work, but they are pessimistic. They assume a conflict is about to happen and make every thread wait, even when conflicts are rare.
Transactional memory (TM) takes the opposite bet. You mark a block of code as a transaction â an atomic region â and simply run it, reading and writing speculatively. The hardware or runtime watches every memory access. If it detects that another thread has touched the same location, the transaction aborts and retries from scratch. If no conflict happened, the transaction commits and all its writes become visible at once.
The analogy is a database transaction: you get atomicity (all or nothing), isolation (invisible mid-flight), and consistency (no partial updates). Unlike a database, transactional memory works at the granularity of individual machine words, in nanoseconds, without a query language in sight.
Comments
Loading comments...