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

十年热恋 提交于 2019-12-23 02:39:13

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/38663379/is-it-enough-to-declare-a-function-as-transaction-safe-so-they-can-be-used-thre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!