Intel® ISA Extensions
Use hardware-based isolation and memory encryption to provide more code protection in your solutions.

Transactional memory idea with C++


I just had a nice transactional memory idea that might make transactional memory portable across CPUs having similar facilities like TSX on Intel-CPUs.
The idea is to encapsulate the transaction in a C++ lamda. Look at this code:

template<typename L>
int transaction( L &lambda )
    unsinged code = _xbegin();
    if( code == _XBEGIN_STARTED )
        return 1;
        return (code & (_XABORT_RETRY | _XABORT_EXPLICIT)) == _XABORT_RETRY ? 0 : -1;

   auto trans = []()
       ... do transacton
   transaction<decltype(trans)>( trans );

transaction<>() returns everything which is necessary: > 0 if the transaction is successful, 0 if it is aborted but could be retried and < 0 if it is aborted and can't retried.
Unfortunately C++ is too stupid as to decuce the template-parameter I specify with decltype(trans) from the parameter to transaction(). otherwise I could place the lamda directly into the function-call. Hopefully another upcoming C++ standard will have a template-parameter-decuction for this.

0 Kudos
0 Replies