Skip to content
Composable monadic STM for C++ on Free monads
C++ QMake
Branch: master
Clone or download

C++ Software Transactional Memory library

Working library for Software Transactional Memory that is built using several FP techniques and modern C++17.

  • STM is monadic and combinatorial.
  • It is very robust due to purely functional design.
  • It is built on top of the custom Free monad.
  • It operates by custom ADTs.
  • It is usable despite it's experimental.


  • GCC 7.2

Tutorials and samples


  • Pass tvars to closures by copy.
  • Make retry satisfiable.


The simplest possible usage is int counter increment from different threads.


stm::STML<int> incrementCounter(const stm::TVar<int>& tCounter) {
    stm::STML<stm::Unit> modified =
            stm::modifyTVar<int>(tCounter, [](int i) { return i + 1; });

    return stm::bind<stm::Unit, int>(modified,
                     [&](const stm::Unit&){ return stm::readTVar<int>(tCounter); });


Context ctx;

stm::TVar<int> tCounter = stm::newTVarIO(ctx, 0);
int counter = stm::atomically(ctx, incrementCounter(tCounter));
std::cout << "Counter: " << counter << std::endl;

The Dining Philosopher Problem can be solved with STM elegantly. Here are the transactions for taking of forks:

stm::STML<stm::Unit> takeFork(const TFork& tFork) {
    return stm::withTVar<Fork, stm::Unit>(tFork, [=](const Fork& fork) {
       if (fork.state == ForkState::Free) {
           return stm::writeTVar<Fork>(tFork, Fork {, ForkState::Taken });
       else {
           return stm::retry<stm::Unit>();

stm::STML<stm::Unit> takeForks(const TForkPair& forks) {
    stm::STML<stm::Unit> lm = takeFork(forks.left);
    stm::STML<stm::Unit> rm = takeFork(forks.right);
    return stm::sequence(lm, rm);

Notice the retry combinator that marks some state illegal and makes the transaction to restart on case if fork was already taken.

To get more information, read the tutorial.

You can’t perform that action at this time.