c++concurrencyc++14transactional-memory

Is it enough to declare a function as transaction_safe, so they can be used thread-safe?


Is enough just to declare all of the functions as transaction_safe in some my class, so its can be used as thread-safe in transactions atomic_noexcept, atomic_cancel, atomic_commit from Experimental Transactional Memory TS?

As known there are Transactional Memory TS (ISO/IEC TS 19841:2015) in the Experimental C++ standard libraries. Simple examples are here: http://en.cppreference.com/w/cpp/language/transactional_memory

Also there is Technical Specification for C++ Extensions for Transactional Memory: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4514.pdf

Page 34:

23.4 Associative containers [associative]
23.4.4 Class template map [map]
23.4.4.1 Class template map overview [map.overview]

In 23.4.4.1 [map.overview], add "transaction_safe" to the declarations of all
variants of the begin and end member functions and to
the declarations of size, max_size, and empty.

I.e. if Transactional Memory will commit to the C++ Standard, then can we simply do something like this and will it thread-safe?

#include<map>
#include<thread>

std::map<int, int> m;

int main() {

 std::thread t1([&m]() {
  atomic_cancel
  {
   m[1] = 1;    // thread-safe
  }
 } );

 t1.join();

 return 0;
}

Unfortunately, I can't reproduce example with atomic_cancel {} even with key -fgnu-tm on GCC 6.1: https://godbolt.org/g/UcV4wI

And will enough just to declare all of the functions as transaction_safe in some my class, so its can be used as thread-safe - if I will call its in scope: atomic_cancel { obj.func(); }?


Solution

  • The compound-statement in an atomic block is not allowed to execute any expression or statement or call any function that isn't transaction_safe

    std::map<int, int>::operator[] wouldn't be a transaction_safe method, so you couldn't call it in atomic_cancel. It would be a compile time error.