All Projects → graninas → cpp_stm_free

graninas / cpp_stm_free

Licence: BSD-3-Clause License
Composable monadic STM for C++ on Free monads

Programming Languages

C++
36643 projects - #6 most used programming language
QMake
1090 projects

Projects that are alternatives of or similar to cpp stm free

freAST
Fast, simple Free Monads using ScalaMeta macro annotations. Port of Freasy-Monad.
Stars: ✭ 14 (-69.57%)
Mutual labels:  monad, free-monads
MtacAR
Mtac in Agda
Stars: ✭ 29 (-36.96%)
Mutual labels:  monad
ShowcaseView
ShowcaseView library for Android
Stars: ✭ 196 (+326.09%)
Mutual labels:  showcase
harmony
C++ Monadologie
Stars: ✭ 26 (-43.48%)
Mutual labels:  monad
sphinx-themes.org
A showcase for Sphinx documentation themes
Stars: ✭ 114 (+147.83%)
Mutual labels:  showcase
cpsfy
🚀 Tiny goodies for Continuation-Passing-Style functions, fully tested
Stars: ✭ 58 (+26.09%)
Mutual labels:  monad
showcase
Showcasing what can be done with Bastion
Stars: ✭ 19 (-58.7%)
Mutual labels:  showcase
talks
Let's talk about ..
Stars: ✭ 13 (-71.74%)
Mutual labels:  showcase
covid-19
COVID-19 World is yet another Project to build a Dashboard like app to showcase the data related to the COVID-19(Corona Virus).
Stars: ✭ 28 (-39.13%)
Mutual labels:  showcase
hystrix-examples
Showcase for Netflix' Hystrix
Stars: ✭ 45 (-2.17%)
Mutual labels:  showcase
php-slang
The place where PHP meets Functional Programming
Stars: ✭ 107 (+132.61%)
Mutual labels:  monad
f
a library to write async vert.x code similar as using java syntax
Stars: ✭ 22 (-52.17%)
Mutual labels:  monad
tagless-final-example
An example of how to create services using tagless final
Stars: ✭ 25 (-45.65%)
Mutual labels:  monad
learning-stm
Learning structural topic modeling using the stm R package.
Stars: ✭ 103 (+123.91%)
Mutual labels:  stm
widgets playground
Showcase example for https://github.com/therecipe/qt
Stars: ✭ 50 (+8.7%)
Mutual labels:  showcase
yiipowered
Yii powered websites showcase
Stars: ✭ 88 (+91.3%)
Mutual labels:  showcase
ts-belt
🔧 Fast, modern, and practical utility library for FP in TypeScript.
Stars: ✭ 439 (+854.35%)
Mutual labels:  monad
showcase-app-react-native
Aplicativos nacionais feitos com React Native
Stars: ✭ 114 (+147.83%)
Mutual labels:  showcase
cefal
(Concepts-enabled) Functional Abstraction Layer for C++
Stars: ✭ 52 (+13.04%)
Mutual labels:  monad
hkts
Functional programming tools: option, either, task, state, optics, etc.
Stars: ✭ 20 (-56.52%)
Mutual labels:  monad

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.

Additional materials

Requirements

  • GCC 7.2

Troubleshooting

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

Examples

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

Transaction:

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); });
}

Evaluation:

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 { fork.name, 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].