1

私のクラスのようにすべての関数を宣言するだけで十分なので、 Experimental Transactional Memory TS からtransaction_safeのトランザクションでスレッドセーフとして使用できますか?atomic_noexcept, atomic_cancel, atomic_commit

知られているように、Experimental C++ 標準ライブラリには Transactional Memory TS (ISO/IEC TS 19841:2015) があります。簡単な例はこちら: http://en.cppreference.com/w/cpp/language/transactional_memory

また、トランザクション メモリの C++ 拡張機能の技術仕様もあります: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4514.pdf

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.

つまり、トランザクショナル メモリが C++ 標準にコミットする場合、単純にこのようなことを行うことができ、それはスレッド セーフになりますか?

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

残念ながら、GCC 6.1atomic_cancel {}のキーを使用しても例を再現できません: https://godbolt.org/g/UcV4wI-fgnu-tm

そして、一部のクラスのようにすべての関数を宣言するだけで十分なtransaction_safeので、スレッドセーフとして使用できます-スコープ内で呼び出す場合: atomic_cancel { obj.func(); }?

4

1 に答える 1

1

Atomic ブロック内の複合ステートメントは、式またはステートメントを実行したり、transaction_safe ではない関数を呼び出したりすることはできません

std::map<int, int>::operator[]メソッドではないtransaction_safeため、atomic_cancel で呼び出すことはできません。コンパイル時エラーになります。

于 2016-07-29T16:31:18.817 に答える