Skip to content

Transactional Memory

Alfonso J. Ramos edited this page Nov 10, 2015 · 2 revisions

A mechanism for Transactional Memory is included in the FAT builds of the library.

The transactions work over Theraot.Threading.Needles.Transact.Needle. You can create an instance as follows:

using Theraot.Threading.Needles;

class Program
{
    public void Main()
    {
        var needle = Transact.CreateNeedle(123);
        // ...
    }
}

You can then execute transactional code over the needles by creating instances of Transact:

using Theraot.Threading.Needles;

class Program
{
    public void Main()
    {
        var needle = Transact.CreateNeedle(123);
        using (var transaction = new Transact())
        {
            // You can read or write the value of the needle
            needle.Value = 5;
            // And then commit the changes
            transaction .Commit();
        }
    }
}

The changes done to the needles are only visible to the current thread until the changes are commited. Commit returns true if the operation was successful. partial modifications of the needles used in the trnasaction will never be visible to other threads.

Nested transactions are supported. When nesting transaction, the changes commited in the inner transaction are not made visible to other threads, but to the outer transaction.

In case you want to reverse the chnages done so far in a transaction, you can call Rollback. This is equivalent to a failed commit. Disposing a transaction that has not been commited will also discard the changes done within it.


The internal implementation of the transactions will never cause a thread to wait or starve.

This was developed as an alternative to ReaderWriterLockSlim and a possible backend for Concurrent classes. Although ReaderWriterLockSlim was ported from Mono and Concurrent classes has been implemented using Mapper... which is why there is no need for transactions to be available in the default builds.