2016-07-29 1 views
1

私のクラスでは、すべての関数をtransaction_safeとして宣言するだけで十分です。そのため、トランザクションでスレッドセーフとして使用することができますatomic_noexcept, atomic_cancel, atomic_commit Experimental Transactional Memory TS?関数をtransaction_safeとして宣言すれば足りるので、スレッドセーフで使用できますか?

既知のように、Experimental C++標準ライブラリにはトランザクションメモリ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++標準にコミットすれば、このようなことを単純に行うことができ、スレッドセーフなのでしょうか? https://godbolt.org/g/UcV4wI

を、十分なだけでいくつかの私のクラスではtransaction_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; 
} 

残念ながら、私もGCC 6.1のキー-fgnu-tmatomic_cancel {}に例を再現することはできませんスコープ内で呼び出す場合はスレッドセーフとして使用してください:atomic_cancel { obj.func(); }

答えて

1

アトミック・ブロック中の化合物文は に任意の式またはステートメントを実行したり transaction_safe

std::map<int, int>::operator[]そう、transaction_safe方法ではないではない任意の関数を呼び出すことが許可されていませんatomic_cancelで呼び出すことはできませんでした。それはコンパイル時エラーです。

+0

I.e. 'std :: unordered_multisap'、' std :: unordered_multimap'、 'std :: unordered_set'、' std :: unordered_multiset'、 'std :: unordered_multiset'、' std :: unordered_multiset'の ' std :: deque'を実行すると、そのコンテナだけが 'atomic_cancel {...}'でスレッドセーフを使用できますか? – Alex

+1

@Alex私が理解している限り、はい – David

関連する問題